diff options
Diffstat (limited to 'hw2/assignment_lists_sp23.py')
| -rw-r--r-- | hw2/assignment_lists_sp23.py | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/hw2/assignment_lists_sp23.py b/hw2/assignment_lists_sp23.py index a339f1f..4aeca33 100644 --- a/hw2/assignment_lists_sp23.py +++ b/hw2/assignment_lists_sp23.py @@ -72,12 +72,12 @@ class DoublyLinkedList(object): def __next__(self): """Standard python iterator method""" - if self.__current is self.__trailer: - raise StopIteration - if self.__current is None: self.__current = self.__header + if self.__current.next_node is self.__trailer: + raise StopIteration + self.__current = self.__current.next_node return self.__current @@ -263,6 +263,9 @@ each element of a list. #Test your implementation here ### USE THE MAP FUNCTION HERE +g = lambda a: a ** 2 +dL.map(g) + print(dL) assert str(dL) == "((0), (1764), (1), (4), (1156), (9), (1))" @@ -292,10 +295,16 @@ it might be used a few times. """ # WRITE YOUR CODE HERE +twenties = DoublyLinkedList() +for i in range(20,30): + n = Node(i) + twenties.add_last(n) + print(twenties) assert str(twenties) == "((20), (21), (22), (23), (24), (25), (26), (27), (28), (29))" ##Write more code here to change the DLL +twenties.map(lambda x: x + 10) print(twenties) assert str(twenties) =="((30), (31), (32), (33), (34), (35), (36), (37), (38), (39))" @@ -304,10 +313,12 @@ assert str(twenties) =="((30), (31), (32), (33), (34), (35), (36), (37), (38), ( **Task 12 (5 points)**: Multiply each element of the list of integers in the twenties by 5. _Hint_: Use the methods `map`, `get_previous`, and `set_element`.""" +# Subtracting 10 to undo the last question +twenties.map(lambda x: (x - 10) * 5) # your code here -print(twentyBy5) -assert str(twentyBy5) == "((100), (105), (110), (115), (120), (125), (130), (135), (140), (145))" +print(twenties) +assert str(twenties) == "((100), (105), (110), (115), (120), (125), (130), (135), (140), (145))" """ **Task 13 (5 points)**: Remove elements that are multiples of 4. @@ -315,16 +326,31 @@ assert str(twentyBy5) == "((100), (105), (110), (115), (120), (125), (130), (135 _Hint_: Use the methods `next` and `remove`.""" # Your code here +cur = twenties.get_first() +end = twenties.get_last().next_node # idk why but python is saying there is no attribute __trailer fora dll +while cur != end: + next = cur.next_node + if cur.element % 4 == 0: + twenties.remove(cur) + cur = next + -assert str(twentyBy5) == '((105), (110), (115), (125), (130), (135), (145))' +assert str(twenties) == '((105), (110), (115), (125), (130), (135), (145))' """ **Task 14 (5 points)**: Remove elements from the list that are odd numbers. _Hint_: Use the methods `get_previous` and `remove`.""" +cur = twenties.get_first() +end = twenties.get_last().next_node # idk why but python is saying there is no attribute __trailer fora dll +while cur != end: + next = cur.next_node + if not (cur.element % 2 == 0): + twenties.remove(cur) + cur = next # Your code here -assert str(twentyBy5) == '((110), (130))' +assert str(twenties) == '((110), (130))' """## Proving performance properties |
