from tester import _assert_equal from linkedlist import linkedlist_from_list # delete_middle_node deletes a node, given that the node has # both a parent, and a child. def delete_middle_node(node): # Your code here pass tests = [ [[1, 2, 3, 4, 5], 2, [1, 2, 4, 5]], [[1, 3, 5], 1, [1, 5]], [[1, 2, 3, 4, 5], 3, [1, 2, 3, 5]], ] for test in tests: first_ll = linkedlist_from_list(test[0]) pointer = first_ll for i in range(0, test[1]): pointer = pointer.n delete_middle_node(pointer) _assert_equal(linkedlist_from_list(test[2]), first_ll)
from linkedlist import linkedlist_from_list from tester import runtests # The partition function should take a linked list and a value. # the linked list should be modified inplace such that all values # less than the value end up in the first half of the list, and the # values greater than or equal to the passed value end up in the # second half of the list. def partition(ll, val): # Your code here pass tests = [ [[linkedlist_from_list([1, 2, 3, 3, 4, 5]), 3], linkedlist_from_list([1, 2, 3, 3, 4, 5])], [[linkedlist_from_list([3, 3, 4, 5, 1, 2]), 3], linkedlist_from_list([1, 2, 3, 3, 4, 5])], [[linkedlist_from_list([6, 2, 33, 3, 44, 5]), 6], linkedlist_from_list([2, 3, 5, 6, 33, 44])], [[linkedlist_from_list([]), 3], linkedlist_from_list([])], [[linkedlist_from_list([6]), 3], linkedlist_from_list([6])], [[linkedlist_from_list([3, 2]), 3], linkedlist_from_list([2, 3])] ] runtests(tests, partition, inplace=True)
from linkedlist import linkedlist_from_list from tester import runtests # is_loop should return True if there is a loop in the linked list. # That is, if any node in the linked list points to a node earlier in the list (or itself). def is_loop(ll): # Your code here pass test1 = linkedlist_from_list([1]) test1.n = test1 test2 = linkedlist_from_list([1, 2, 3, 4, 5]) test2.n.n.n.n = test2 test3 = linkedlist_from_list([1, 2, 3, 4]) test3.n.n.n = test3 test4 = linkedlist_from_list([1, 2, 3, 4]) test4.n.n.n = test4.n.n tests = [ [[test1], True], [[test2], True], [[test3], True], [[test4], True], [[linkedlist_from_list([1, 2, 3, 3, 4, 5])], False], [[linkedlist_from_list([1])], False], [[linkedlist_from_list([])], False], ]
from linkedlist import linkedlist_from_list from tester import runtests # kth_from_last should return the k-th-from-last element in the linked list ll. def kth_from_last(k, ll): # Your code here pass tests = [ [[5, linkedlist_from_list([1, 2, 3, 3, 4, 5])], 1], [[0, linkedlist_from_list([5, 4, 3, 2, 5 ,1])], 1], [[1, linkedlist_from_list([1, 1])], 1], [[0, linkedlist_from_list([1])], 1], [[2, linkedlist_from_list(['always', 'always', 'remove', 'duplicates'])], 'always'], [[1, linkedlist_from_list([1, 2, 3, 1, 3, 0])], 3] ] runtests(tests, kth_from_last)
from linkedlist import linkedlist_from_list from tester import runtests # remove_dups should modify ll inplace to remove any nodes of the linked list # with duplicate values. def remove_dups(ll): # Your code here pass tests = [ [[linkedlist_from_list([1, 2, 3, 3, 4, 5])], linkedlist_from_list([1, 2, 3, 4, 5])], [[linkedlist_from_list([5, 4, 3, 2, 5 ,1])], linkedlist_from_list([5, 4, 3, 2, 1])], [[linkedlist_from_list([1, 1])], linkedlist_from_list([1])], [[linkedlist_from_list([1])], linkedlist_from_list([1])], [[linkedlist_from_list(['always', 'always', 'remove', 'duplicates'])], linkedlist_from_list(['always', 'remove', 'duplicates'])], [[linkedlist_from_list([])], linkedlist_from_list([])], [[linkedlist_from_list([1, 2, 3, 1, 3, 0])], linkedlist_from_list([1, 2, 3, 0])] ] runtests(tests, remove_dups, inplace=True)
from linkedlist import linkedlist_from_list from tester import runtests # palindromically_reorder should modify the passed linked list inplace such that # the i'th node of the list will be followed by the N-1-i'th node. # Sounds confusing. Is a little. Have a look at the test cases. def palindromically_reorder(ll): # Your code here pass tests = [ [[linkedlist_from_list([1, 2, 3, 4, 5])], linkedlist_from_list([1, 5, 2, 4, 3])], [[linkedlist_from_list([1, 2, 3, 3, 4, 5])], linkedlist_from_list([1, 5, 2, 4, 3, 3])], [[linkedlist_from_list([])], linkedlist_from_list([])], [[linkedlist_from_list([1])], linkedlist_from_list([1])], [[linkedlist_from_list([1, 2])], linkedlist_from_list([1, 2])], [[linkedlist_from_list([1, 2, 3])], linkedlist_from_list([1, 3, 2])], ] runtests(tests, palindromically_reorder, inplace=True)
from linkedlist import linkedlist_from_list from tester import runtests # palindrome should return True if ll represents a palindrome. def palindrome(ll): # Your code here pass tests = [ [[linkedlist_from_list([1, 2, 3, 2, 1])], True], [[linkedlist_from_list([1, 2, 3, 3, 2, 1])], True], [[linkedlist_from_list([1, 2, 3, 4, 2, 1])], False], [[linkedlist_from_list([1, 2, 3, 3, 2, 2])], False], [[linkedlist_from_list([1, 2])], False], [[linkedlist_from_list([1])], True], [[linkedlist_from_list([])], True], ] runtests(tests, palindrome)
from linkedlist import linkedlist_from_list from tester import runtests # sum_lists should take two linked lists, treat them as if they were decimal numbers, # and return a list that represents the sum of those numbers. (see tests for examples) def sum_lists(l1, l2): # Your code here pass tests = [ [[linkedlist_from_list([1, 2, 3, 3, 4]), linkedlist_from_list([1, 2, 3, 3, 4])], linkedlist_from_list([2, 4, 6, 6, 8])], [[linkedlist_from_list([1, 5, 0]), linkedlist_from_list([1, 5, 0])], linkedlist_from_list([3, 0, 0])], [[linkedlist_from_list([9, 9]), linkedlist_from_list([1])], linkedlist_from_list([1, 0, 0])], [[linkedlist_from_list([9]), linkedlist_from_list([9])], linkedlist_from_list([1, 8])], [[linkedlist_from_list([]), linkedlist_from_list([])], linkedlist_from_list([])], [[linkedlist_from_list([9, 9, 9, 9, 9]), linkedlist_from_list([2])], linkedlist_from_list([1, 0, 0, 0, 0, 1])], [[linkedlist_from_list([1, 0, 0, 0, 0]), linkedlist_from_list([2])], linkedlist_from_list([1, 0, 0, 0, 0, 2])], ] runtests(tests, sum_lists)