-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractise1.py
More file actions
327 lines (286 loc) · 9.22 KB
/
practise1.py
File metadata and controls
327 lines (286 loc) · 9.22 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#############################################################
#
# CMPSC 132: Homework 4
#
#
# For each function, minimal test cases are provided.
# - You may test individual functions separately
# or test all functions at once.
# - Read the comments at the end of this file
# to learn how to do it
# - These test cases are not meant to be comprehensive.
# They are provided simply to demonstrate the use cases
# of each method.
# - Testing and debugging is your responsibility
#############################################################
student_name = "Tife Akintan"
student_email = "taa5555@psu.edu"
student_section = "003"
# DO NOT modify this class
class Node:
def __init__(self, value):
self.value = value
self.next = None
def __str__(self):
return "Node({})".format(self.value)
__repr__ = __str__
class SortedLinkedList:
# DO NOT modify this method
def __init__(self):
self.head = None
self.tail = None
# DO NOT modify this method
def __str__(self):
temp = self.head
out = []
while temp:
out.append(str(temp.value))
temp = temp.next
out = ' -> '.join(out)
return f'Head:{self.head}\nTail:{self.tail}\nList:{out}'
# DO NOT modify this method
__repr__ = __str__
def isEmpty(self):
"""
>>> x=SortedLinkedList(); x.isEmpty()
True
>>> x.head = Node(10); x.isEmpty()
False
"""
if self.head is None:
return True
else:
return False
pass
def __len__(self):
"""
>>> x=SortedLinkedList(); len(x)
0
>>> x.head = Node(10); len(x)
1
>>> x.head.next = Node(20); x.head.next.next = Node(20); len(x)
3
"""
curr = self.head
count = 0
while curr is not None:
curr = curr.next
count += 1
return count
pass
def add(self, value):
"""
>>> x=SortedLinkedList(); x.add(10)
Head:Node(10)
Tail:Node(10)
List:10
>>> x.add(20)
Head:Node(10)
Tail:Node(20)
List:10 -> 20
>>> x.add(5)
Head:Node(5)
Tail:Node(20)
List:5 -> 10 -> 20
>>> x.add(7).add(15)
Head:Node(5)
Tail:Node(20)
List:5 -> 7 -> 10 -> 15 -> 20
>>> x.add(25).add(13).add(9.54).add(-10.3)
Head:Node(-10.3)
Tail:Node(25)
List:-10.3 -> 5 -> 7 -> 9.54 -> 10 -> 13 -> 15 -> 20 -> 25
>>> x=SortedLinkedList(); x.add()
Traceback (most recent call last):
TypeError: SortedLinkedList.add() missing 1 required positional argument: 'value'
"""
new_node = Node(value)
# if the list is empty, set the new node as the head and the tail
if self.head is None:
self.head = new_node
self.tail = self.head
return self
# if the new node value is smaller than the head value, insert at the beginning
if self.head.value > value:
new_node.next = self.head
self.head = new_node
return self
# if the new node value is greater than the tail value, insert at the end
if self.tail.value <= value:
self.tail.next = new_node
self.tail = new_node
return self
# find the node after which to insert the new node
current_node = self.head
while current_node.next is not None and current_node.next.value < value:
current_node = current_node.next
# insert the new node after the current node
new_node.next = current_node.next
current_node.next = new_node
return self
pass
# To pass the tests, you must implement add() first
def remove(self, value):
"""
>>> x=SortedLinkedList(); x.remove(4)
Head:None
Tail:None
List:
>>> x.add(4)
Head:Node(4)
Tail:Node(4)
List:4
>>> x.remove(4)
Head:None
Tail:None
List:
>>> x.add(1).add(2).add(3).add(4).add(5)
Head:Node(1)
Tail:Node(5)
List:1 -> 2 -> 3 -> 4 -> 5
>>> x.remove(1)
Head:Node(2)
Tail:Node(5)
List:2 -> 3 -> 4 -> 5
>>> x.remove(5)
Head:Node(2)
Tail:Node(4)
List:2 -> 3 -> 4
>>> x.remove(3)
Head:Node(2)
Tail:Node(4)
List:2 -> 4
>>> x.add(1).add(3).add(5)
Head:Node(1)
Tail:Node(5)
List:1 -> 2 -> 3 -> 4 -> 5
>>> x.remove(3).remove(1).remove(5).remove(2).remove(4)
Head:None
Tail:None
List:
>>> x.add(1).add(3).add(5).remove(2).remove(3).add(4)
Head:Node(1)
Tail:Node(5)
List:1 -> 4 -> 5
"""
if self.head is None:
return self
# if the head node is the node to remove, update the head
if self.head.value == value:
self.head = self.head.next
if self.head is None:
self.tail = None
return self
# find the node before the node to remove
current_node = self.head
while current_node.next is not None and current_node.next.value != value:
current_node = current_node.next
# if the node to remove is not found, return
if current_node.next is None:
return self
# if the tail node is the node to remove, update the tail
if current_node.next == self.tail:
self.tail = current_node
# remove the node
current_node.next = current_node.next.next
return self
# To pass the tests, you must implemente add() first
def duplicate(self):
"""
>>> x=SortedLinkedList(); x.duplicate()
Head:None
Tail:None
List:
>>> x.add(2).duplicate()
Head:Node(2)
Tail:Node(2)
List:2 -> 2
>>> x.add(3).duplicate()
Head:Node(2)
Tail:Node(3)
List:2 -> 2 -> 3 -> 3 -> 3
>>> x.add(1).duplicate()
Head:Node(1)
Tail:Node(3)
List:1 -> 2 -> 2 -> 3 -> 3 -> 3
>>> x.add(0).duplicate()
Head:Node(0)
Tail:Node(3)
List:0 -> 1 -> 2 -> 2 -> 3 -> 3 -> 3
>>> x.add(-5).duplicate()
Head:Node(-5)
Tail:Node(3)
List:-5 -> -5 -> 0 -> 1 -> 2 -> 2 -> 3 -> 3 -> 3
>>> x.add(4.5).duplicate()
Head:Node(-5)
Tail:Node(4.5)
List:-5 -> -5 -> 0 -> 1 -> 2 -> 2 -> 3 -> 3 -> 3 -> 4.5 -> 4.5
>>> x
Head:Node(-5)
Tail:Node(4.5)
List:-5 -> 0 -> 1 -> 2 -> 3 -> 4.5
"""
a = []
current = self.head
# Traverse the original linked list and add each value to the new list
while current is not None:
a.append(current.value)
current = current.next
result = []
for num in a:
if isinstance(num, float):
result += [num] * 2
elif num > 0:
result += [num] * num
elif num < 0:
result += [num] * 2
elif num == 0:
result += [num] * 1
else:
result.append(num)
v = SortedLinkedList()
for i in result:
v.add(i)
return v
# To pass the tests, you must implement add() first
def removeDuplicates(self):
"""
>>> x=SortedLinkedList(); x.removeDuplicates(); x
Head:None
Tail:None
List:
>>> x=SortedLinkedList(); x.add(2).add(2).removeDuplicates(); x
Head:Node(2)
Tail:Node(2)
List:2
>>> x=SortedLinkedList(); x.add(1).add(2).add(2).removeDuplicates(); x
Head:Node(1)
Tail:Node(2)
List:1 -> 2
>>> x=SortedLinkedList(); x.add(1).add(2).add(3).add(3).removeDuplicates(); x
Head:Node(1)
Tail:Node(3)
List:1 -> 2 -> 3
>>> x.add(1).add(2).add(2).add(4).add(4).add(5).add(5).removeDuplicates(); x
Head:Node(1)
Tail:Node(5)
List:1 -> 2 -> 3 -> 4 -> 5
"""
current = self.head
# Traverse the linked list and remove duplicate nodes
while current is not None and current.next is not None:
if current.value == current.next.value:
current.next = current.next.next
if current.next is None:
self.tail = current
else:
current = current.next
pass
if __name__ == '__main__':
import doctest
## Uncomment this line if you want to run doctest by function.
## Replace isEmpty with the name of the function you want to run
# doctest.run_docstring_examples(SortedLinkedList.add, globals(), verbose=True, name='hw4')
## Uncomment this line if you want to run the docstring
## in all functions
# doctest.testmod()