-
Notifications
You must be signed in to change notification settings - Fork 0
/
List1.py
executable file
·149 lines (131 loc) · 3.36 KB
/
List1.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
class List1():
def __init__(self, items):
self.list1 = [0] + items
@property
def __class__(self):
return List1
def __getitem__(self, item):
"""
:param item: list1 index
:return: item at list index item(+1)
>>> l = List1([1, 2, 3])
>>> l[1]
1
>>> l[3]
3
"""
return self.list1[item]
def __setitem__(self, key, value):
"""
:param key: a postion where to insert value
:param value: value
>>> l = List1([1, 2, 3])
>>> l[2] = 5
>>> l == List1([1, 5, 3])
True
"""
self.list1[key] = value
def __eq__(self, other):
"""
:param other: a List1 instance to compare
:return:True/False
>>> l1 = List1([1, 2, 3])
>>> l2 = List1([1, 2, 3])
>>> l1 == l2
True
>>> l3 = List1([1, 4, 3])
>>> l1 == l3
False
"""
for i in range(len(self.list1)):
if self.list1[i] != other.list1[i]:
return False
return True
def __iter__(self):
return self.list1[1:].__iter__()
def __len__(self):
"""
:return: int length of the list
>>> l = List1([1, 2, 3])
>>> len(l)
3
"""
return len(self.list1)-1
def __repr__(self):
return '<%s>' % ', '.join([str(a) for a in self.list1[1:]])
def __add__(self, other):
self.list1 += other
def __contains__(self, item):
"""
:param item: an item
:return: is the item in list
>>> l = List1([1, 2, 3])
>>> 1 in l
True
>>> 5 in l
False
"""
return item in self.list1[1:]
def append(self, item):
self.list1.append(item)
def pop(self, item):
"""
:param item: item to pos
:return: the item in item, remove item from list
>>> l = List1([1, 2, 3])
>>> l.pop(1)
1
>>> l == List1([2, 3])
True
"""
return self.list1.pop(item)
def count(self, item):
return self.list1[1:].count(item)
def remove(self, item):
self.list1.remove(item)
def index(self, item):
"""
:param item: item
:return: index if item
>>> l = List1([1, 2, 3])
>>> l.index(2) == 2
True
"""
return self.list1.index(item)
def enumerate(self):
"""
:return: index, item of List1
"""
for i in range(1, len(self.list1)):
yield i, self.list1[i]
def reverse(self):
"""
>>> l = List1([1, 2, 3])
>>> l.reverse()
>>> l == List1([3, 2, 1])
True
"""
self.list1.pop(0)
self.list1.append(0)
self.list1.reverse()
def insert(self, key, value):
"""
:param key: index
:param value: value
:return: List1 with value inserted at key
>>> l = List1([1, 2, 3])
>>> l.insert(2, 10)
>>> l == List1([1, 10, 2, 3])
True
"""
self.list1.insert(key, value)
def sort(self, *kwargs):
"""
>>> l = List1([1, 2, 4, 3])
>>> l.sort()
>>> l == List1([1, 2, 3, 4])
True
"""
tmp = self.list1[1:][:]
tmp.sort(*kwargs)
self.list1 = [0] + tmp