class TStr:
def __init__(self, chars: str = '') -> None:
self.chars = tuple(chars)
def __len__(self) -> int:
return len(self.chars)
def __str__(self) -> str:
return ''.join(self.chars)
def __repr__(self) -> str:
return f'{type(self).__name__}("{self}")'
def __add__(self, s) -> 'TStr':
return TStr(self.chars + s.chars)
def __mul__(self, n: int) -> 'TStr':
return TStr(n * self.chars)
def __rmul__(self, n: int) -> 'TStr':
return TStr(n * self.chars)
def __eq__(self, x: object) -> bool:
return self.chars == x.chars
def __lt__(self, x: 'TStr') -> bool:
return self.chars < x.chars
def __le__(self, x: 'TStr') -> bool:
return self == x or self < x
def __hash__(self) -> int:
return hash(self.chars)
def __getitem__(self, i: int) -> 'TStr':
return TStr(self.chars[i])
s = TStr('Hello World!')
print(len(s))
s = TStr()
print(len(s))
s = TStr('Hello')
print(s)
print(s.chars)
hello = TStr('Hello')
world = TStr('World!')
space = TStr(' ')
print([hello, space, world])
print(hello + space + world)
print(world*3)
print(3*world)
words = [world, hello]
words.sort()
print(words)
print(set(words))
print([hello[0], hello[-1], hello[1:3], hello[:-1], hello[:], hello[::-1]])
print(list(hello))
print(list(reversed(hello)))
for ch in hello:
print(repr(ch))
import functools
res = functools.reduce(lambda x,y: x+y, hello, TStr())
print(repr(res))