-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
275 lines (231 loc) · 7.26 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
class Element:
def __init__(self, value):
self.value = value
self.next = None
self.previous = None
def Print(self, times=0):
""" Print Element and continue for number of times
Parameters:
times: int, number of element to print, 0 would print all
"""
if type(times) is not int or times < 0:
raise TypeError("times must be an int larger or equal to 0.")
output = []
node = self
all = 0 == times
while node and (all or times > 0):
output.append(node.value)
node = node.next
times -= 1
print(" -> ".join(output))
def Next(self):
""" Return next element of self"""
return self.next
def Previous(self):
""" Return previous element of self"""
return self.previous
def First(self):
""" Return the first element of self """
node = self
while node.previous:
node = node.previous
return node
def Last(self):
""" Return the last element of self """
node = self
while node.next:
node = node.next
return node
def Nth(self, n):
""" Get the N-th element relative to self
Parameters:
n: int, the N-th element to return
Returns:
node: Element, the N-th element
"""
if type(n) is not int or n < 0:
raise ValueError("n must be an int larger or equal to 0.")
node = self
while node and n > 0:
node = node.next
n -= 1
if not node:
raise ValueError("n is larger than the size of LinkedList.")
return node
def Append(self, item):
""" Append item to the end of the linked list
Parameters:
item: Element or str, item to append
"""
node = self.Last()
node.next = item if isinstance(item, Element) else Element(item)
node.next.previous = node
def Insert(self, item, index=0):
""" Insert item at index 0 relative to self
Parameters:
item: Element or str: item to insert
index: index to insert relative to self
"""
nth = self.Nth(index)
previous = nth.previous
node = item if isinstance(item, Element) else Element(item)
# update next and previous of the tail of inserted node
last = node.Last()
last.next = nth
nth.previous = last
# update next and previous of the head of inserted node
if previous:
node.previous = previous
previous.next = node
def Remove(self):
""" Remove pointers to self"""
if self.next: self.next.previous = self.previous
if self.previous: self.previous.next = self.next
def TearDown(self):
""" Remove pointers to self and after self"""
node = self
if node.previous: node.previous.next = None
while node:
temp = node.next
del node
node = temp
def BruteSearch(self, value):
""" Brute force search value in linked list
Parameters:
value: str, value to search
"""
node = self
while node:
if node.value == value:
return node
node = node.next
def GetMiddle(self, last=None):
""" Get the middle element in an linked list relatively to self
Parameters:
last (optional): Element, the last element to search for middle
Returns:
slow: Element, the middle element
"""
slow = fast = self
while fast and fast.next and (not last or last is not fast):
fast = fast.next
if fast and (not last or last is not fast):
fast = fast.next
slow = slow.next
return slow
def Merge(self, left, right):
""" Merge and sort two linked lists
Parameters:
left: Element, head Element of the first linked list
right: Element, head Element of the second linked list
Returns:
sorted: Element, head Element of the sorted linked list
"""
if not left and not right:
return None
sorted = node = Element('temp')
while left and right:
if left.value < right.value:
node.next = left
left.previous = node
left = left.next
else:
node.next = right
right.previous = node
right = right.next
node = node.next
if not left:
node.next = right
right.previous = node
if not right:
node.next = left
left.previous = node
sorted.next.previous = None
return sorted.next
def Sort(self, head=None):
""" Sort linked list from head
Parameters:
head: Element, element to sort, None to start sorting from root
Returns:
sorted: Element, root of the sorted linked list
"""
# get root of LinkedList
root = head if head else self.First()
if not root or not root.next:
return root
# get middle element
mid = root.GetMiddle()
mid.previous.next = None
mid.previous = None
left = mid.Sort(head=root)
right = mid.Sort(head=mid)
sorted = root.Merge(left, right)
return sorted
def BinarySearch(self, value):
""" Binary search in linked list for value
Parameters:
value: str, value to search
Returns:
node: Element, the elemenet with value
"""
if self.value == value:
return self
start = self
end = None
while not end or start.next is not end:
mid = start.GetMiddle(last=end)
if mid.value == value:
return mid
elif mid.value < value:
start = mid
else:
end = mid
return None
def main():
# construct list
root = Element("Hello")
root.Append(Element("World"))
root.Append(Element("This"))
root.Append(Element("Is"))
root.Append(Element("a"))
root.Append(Element("Linked"))
root.Append(Element("List"))
root.Append(Element("of"))
root.Append(Element("Strings"))
root.Print(0)
# test finding nodes
nth = root.Nth(3)
nth.Print(1)
nth.Previous().Print(1)
nth.Next().Print(1)
nth.First().Print(1)
nth.Last().Print(1)
# test appending and inserting
nth.Append(Element("."))
nth.Last().Print()
nth.Insert(Element("Definitely"))
root.Print(0)
secondList = Element("Second")
secondList.Append(Element("List"))
nth.Insert(secondList.Last())
root.Print(0)
nth.Remove()
del nth
root.Print(0)
# check brute, sort and binary search
found = root.BruteSearch("of")
found.Print(1)
found = root.BinarySearch("List")
if found:
found.Print(1)
root = root.Sort()
root.Print(0)
found = root.BinarySearch("List")
found.Print(1)
# tear down
found.TearDown()
root.Print(0)
root.TearDown()
del root
if __name__ == "__main__":
main()