def basic_tests(): sll = SLL() try: sll.pop_back() except UnderFlowError as e: print "Tried popping from an empty SLL. Error:", e sll.push_back(1) assert(sll.size == 1) assert(sll.head.value == 1) assert(sll.tail.value == 1) sll.push_front(0) assert(sll.size == 2) assert(sll.head.value == 0) assert(sll.tail.value == 1) sll.push_back(2) assert(sll.size == 3) assert(sll.head.value == 0) assert(sll.tail.value == 2) sll.push_back(3) assert(sll.size == 4) assert(sll.head.value == 0) assert(sll.tail.value == 3) sll.push_back(4) assert(sll.size == 5) assert(sll.head.value == 0) assert(sll.tail.value == 4) assert(4 == sll.pop_back()) assert(sll.size == 4) assert(sll.head.value == 0) assert(sll.tail.value == 3) assert(0 == sll.pop_front()) assert(sll.size == 3) assert(sll.head.value == 1) assert(sll.tail.value == 3) assert(1 == sll.pop_front()) assert(sll.size == 2) assert(sll.head.value == 2) assert(sll.tail.value == 3) assert(3 == sll.pop_back()) assert(sll.size == 1) assert(sll.head.value == 2) assert(sll.tail.value == 2) assert(2 == sll.pop_back()) assert(sll.size == 0) assert(sll.head == None) assert(sll.tail == None) # fromList and toList assert(SLL.toList(SLL.fromList(range(9, 0, -2))) == range(9, 0, -2))
def test_findMatchingNode(): s = SLL() for x in [6,4,2]: s.place(x) for x in [5, 1]: s.place(x) s.place(0) s.place(7) assert s.findMatchingNode(0) == s.head assert s.findMatchingNode(7) == s.tail assert s.findMatchingNode(1) == s.head.next assert s.findMatchingNode(2) == s.head.next.next assert s.findMatchingNode(4) == s.head.next.next.next assert s.findMatchingNode(3) == None assert s.findMatchingNode(10) == None assert s.findMatchingNode(-1) == None # Test custom comparator s2 = SLL.fromList([(2,1), (1, 'a'), (2, 3), (0, 'c'), (4,1), (3,0)]) assert s2.findMatchingNode((2,1)) == s2.head assert s2.findMatchingNode((2,3), comparatorfn = lambda (a,b),(c,d): cmp(b,d)) == s2.head.next.next # compare only the first item in the pair between two nodes assert s2.findMatchingNode((2,100), comparatorfn = lambda (a,b),(c,d): cmp(a,c)) == s2.head node = s2.findMatchingNode((0,100), comparatorfn = lambda (a,b),(c,d): cmp(a,c)) # change node's value node.value = (0, 'e') assert node == s2.head.next.next.next assert node.value == (0, 'e') assert SLL.toList(s2) == [(2,1), (1, 'a'), (2, 3), (0, 'e'), (4,1), (3,0)]
def basic_tests(): sll = SLL() try: sll.pop_back() except UnderFlowError as e: print "Tried popping from an empty SLL. Error:", e sll.push_back(1) assert (sll.size == 1) assert (sll.head.value == 1) assert (sll.tail.value == 1) sll.push_front(0) assert (sll.size == 2) assert (sll.head.value == 0) assert (sll.tail.value == 1) sll.push_back(2) assert (sll.size == 3) assert (sll.head.value == 0) assert (sll.tail.value == 2) sll.push_back(3) assert (sll.size == 4) assert (sll.head.value == 0) assert (sll.tail.value == 3) sll.push_back(4) assert (sll.size == 5) assert (sll.head.value == 0) assert (sll.tail.value == 4) assert (4 == sll.pop_back()) assert (sll.size == 4) assert (sll.head.value == 0) assert (sll.tail.value == 3) assert (0 == sll.pop_front()) assert (sll.size == 3) assert (sll.head.value == 1) assert (sll.tail.value == 3) assert (1 == sll.pop_front()) assert (sll.size == 2) assert (sll.head.value == 2) assert (sll.tail.value == 3) assert (3 == sll.pop_back()) assert (sll.size == 1) assert (sll.head.value == 2) assert (sll.tail.value == 2) assert (2 == sll.pop_back()) assert (sll.size == 0) assert (sll.head == None) assert (sll.tail == None) # fromList and toList assert (SLL.toList(SLL.fromList(range(9, 0, -2))) == range(9, 0, -2))
def test_findMatchingNode(): s = SLL() for x in [6, 4, 2]: s.place(x) for x in [5, 1]: s.place(x) s.place(0) s.place(7) assert s.findMatchingNode(0) == s.head assert s.findMatchingNode(7) == s.tail assert s.findMatchingNode(1) == s.head.next assert s.findMatchingNode(2) == s.head.next.next assert s.findMatchingNode(4) == s.head.next.next.next assert s.findMatchingNode(3) == None assert s.findMatchingNode(10) == None assert s.findMatchingNode(-1) == None # Test custom comparator s2 = SLL.fromList([(2, 1), (1, 'a'), (2, 3), (0, 'c'), (4, 1), (3, 0)]) assert s2.findMatchingNode((2, 1)) == s2.head assert s2.findMatchingNode((2, 3), comparatorfn=lambda (a, b), (c, d): cmp(b, d)) == s2.head.next.next # compare only the first item in the pair between two nodes assert s2.findMatchingNode((2, 100), comparatorfn=lambda (a, b), (c, d): cmp(a, c)) == s2.head node = s2.findMatchingNode((0, 100), comparatorfn=lambda (a, b), (c, d): cmp(a, c)) # change node's value node.value = (0, 'e') assert node == s2.head.next.next.next assert node.value == (0, 'e') assert SLL.toList(s2) == [(2, 1), (1, 'a'), (2, 3), (0, 'e'), (4, 1), (3, 0)]
# if reverse_k_group() returned None, we are left with fewer than k nodes # at the end, return immediately if tmp: last.next = tmp if __name__ == '__main__': sll = SLL.fromList("abcde") assert(lookahead(sll.head, 4) == True) assert(lookahead(sll.head, 5) == True) assert(lookahead(sll.head, 6) == False) tmp = reverse_k_group(sll.head, 4) sll.head = tmp assert(SLL.toList(sll) == map (lambda x: x, "dcbae")) sll2 = SLL.fromList("abcde") tmp = reverse_k_group(sll2.head, 6) assert(tmp == None) assert(SLL.toList(sll2) == map (lambda x: x, "abcde")) sll = SLL.fromList("abcde") reverse(sll, 2) assert(SLL.toList(sll) == map (lambda x: x, "badce")) sll = SLL.fromList("abcde") reverse(sll, 3) assert(SLL.toList(sll) == map (lambda x: x, "cbade")) sll = SLL.fromList("abcde")