-
Notifications
You must be signed in to change notification settings - Fork 0
/
super_string.py
87 lines (72 loc) · 2.29 KB
/
super_string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class SuperString:
"""
Class to provide the type of a mutable string with some
additional params
"""
def __init__(self, s):
self.__s = list(s)
def __iter__(self):
self.__i = 0
return self
def __next__(self):
if self.__i > len(self.__s) - 1:
raise StopIteration
else:
a = self.__s[self.__i]
self.__i = self.__i + 1
return a
def __len__(self):
return len(self.__s)
def __str__(self):
return "".join(self.__s)
def __iscorrectindex(self, i):
if type(i) == int or type(i) == slice:
if type(i) == int and i > self.__len__() - 1:
raise IndexError
else:
raise TypeError
def __iscorrectindextoset(self, i):
if type(i) == int:
if i > self.__len__() - 1:
return False
else:
return True
else:
raise TypeError
def __getitem__(self, i):
self.__iscorrectindex(i)
return self.__s[i]
def __setitem__(self, i, v):
if type(v) == int:
v = str(abs(v))
if not self.__iscorrectindextoset(i):
self.__s += list(v)
return
if len(v) > 1:
self.__s = self.__s[0:i] + [i for i in v] + self.__s[i:]
return
self.__s[i] = v
def __delitem__(self, i):
self.__iscorrectindex(i)
del self.__s[i]
def __contains__(self, v):
return v in self.__s
def __addition(self, s, m):
if type(s) == int or \
type(s) == float or \
type(s) == SuperString:
s = str(s)
if type(s) == str:
if m == 'l':
return "".join(self.__s) + s
if m == 'r':
return s + "".join(self.__s)
raise TypeError("Types of data must be only digit or string")
def __add__(self, s):
return self.__addition(s, 'l')
def __radd__(self, s):
return self.__addition(s, 'r')
s1 = SuperString("Practiceperfect")
s1[8] = " make "
print(s1)
print(s1 + 3)