def test_ex2(self): self.assertEqual( ex2.k_last_element(linked_list.from_list([1, 2, 3, 4, 5]), 1), 5 ) self.assertEqual( ex2.k_last_element(linked_list.from_list([1, 2, 1, 4, 5]), 3), 1 )
def test_ex3(self): expected = [1, 2, 3, 5] L = linked_list.from_list([1, 2, 3, 4, 5]) ex3.delete_value(L, 4) self.assertEqual(linked_list.to_list(L), expected) expected = [1, 2, 3, 4, 5] L = linked_list.from_list([1, 2, 3, 4, 5]) ex3.delete_value(L, 6) self.assertEqual(linked_list.to_list(L), expected)
def test_ex1(self): expected = [1, 2, 3, 4, 5] result = linked_list.to_list( ex1.remove_dups(linked_list.from_list([1, 2, 3, 4, 5])) ) self.assertEqual(result, expected) expected = [1, 2, 4, 5] result = ex1.linked_list.to_list( ex1.remove_dups(linked_list.from_list([1, 2, 1, 4, 5])) ) self.assertEqual(result, expected)
def test_reverse_works(): for _ in xrange(100): lst = [ri(1, 20) for _2 in xrange(20)] head = ll.from_list(lst) rhead = ll.reverse(head) rlst = ll.to_list(rhead) assert rlst == lst[::-1]
def test_doubly_from_list(): lst = [1, 2, 3] head = ll.from_list(lst, True) assert type(head) == ll.DLL assert head.data == 1 and head.prev is None assert head.nxt.data == 2 and head.nxt.prev == head assert head.nxt.nxt.data == 3 and head.nxt.nxt.prev == head.nxt assert head.nxt.nxt.nxt is None
def test_from_list(): lst = [1, 2, 3] head = ll.from_list(lst) assert type(head) == ll.LL assert head.data == 1 assert head.nxt.data == 2 assert head.nxt.nxt.data == 3 assert head.nxt.nxt.nxt is None
def test_len_works(): for _ in xrange(10): length = ri(1, 100) curr = head = ll.from_list(range(length), True) steps = ri(0, length - 1) while steps > 0: curr, steps = curr.nxt, steps - 1 assert len(curr) == length
def test_backward_iteration_works(): lst = range(3) head = ll.from_list(lst, True) last = head.nxt.nxt for i, el in enumerate(ll.iter_list(last, True)): assert el.data == lst[2 - i]
def test_iteration_works(): lst = range(3) head = ll.from_list(lst, True) for i, el in enumerate(ll.iter_list(head)): assert el.data == lst[i]
def test_ex8(self): L = linked_list.from_list([1, 2, 3, 4]) L.next.next.next.next = L.next self.assertTrue(ex8.find_loop(L) is L.next) self.assertIsNone(ex8.find_loop(linked_list.from_list([1, 2, 3])))
def test_ex7(self): l1 = linked_list.from_list([1, 2, 3, 4, 5]) l2 = linked_list.from_list([1, 2, 3, 4, 5]) l3 = l1.next.next self.assertIsNone(ex7.intersection_node(l1, l2)) self.assertEqual(ex7.intersection_node(l1, l3), l3)
def test_ex6(self): self.assertTrue(ex6.is_palindrome(linked_list.from_list([7, 1, 5, 1, 7]))) self.assertFalse(ex6.is_palindrome(linked_list.from_list([7, 1, 5, 1, 8])))
def test_ex5(self): x1 = linked_list.from_list([7, 1, 6]) x2 = linked_list.from_list([5, 9, 2]) expected = linked_list.from_list([2, 1, 9]) self.assertEqual(ex5.sum_lists(x1, x2), expected)
first_half = Stack() slow_runner = head fast_runner = head while fast_runner: fast_runner = fast_runner.next if fast_runner: fast_runner = fast_runner.next first_half.push(slow_runner.value) slow_runner = slow_runner.next while slow_runner: if slow_runner.value != first_half.pop(): return False slow_runner = slow_runner.next return True print(is_palindrome2(linked_list.from_list([7, 1, 5, 1, 7]))) print(is_palindrome2(linked_list.from_list([7, 1, 5, 1, 8]))) def _reverse_list(L): r = None while L is not None: r = linked_list.Node(L.value, r) L = L.next return r def is_palindrome(L): return _reverse_list(L) == L
import linked_list def partition(head, x): first = head.value second = head.next.value if first > second: first, second = second, first high = linked_list.Node(second, None) low = linked_list.Node(first, high) head = head.next.next while head: if head.value >= x: high.next = linked_list.Node(head.value, None) high = high.next else: low = linked_list.Node(head.value, low) head = head.next return low L = linked_list.from_list([1, 5, 10, 3, 4, 6]) l = partition(L, 5) print(l)
def test_representation_works(): head = ll.from_list(range(3), True) assert repr(head) == "0<->1<->2"
def test_ex4(self): # expected_low = Counter([1, 3, 4]) # expected_high = Counter([5, 6, 10]) L = linked_list.from_list([1, 5, 10, 3, 4, 6]) l = ex4.partition(L, 5) print(l)
def test_len_works(): for _ in xrange(10): length = ri(1, 100) head = ll.from_list(range(length)) assert len(head) == length