Ejemplo n.º 1
0
 def test_node_repr(self):
     a = sllist([None]).first
     self.assertEqual(repr(a), "<sllistnode(None)>")
     b = sllist([1, None]).first
     self.assertEqual(repr(b), "<sllistnode(1)>")
     c = sllist(["abc", None]).first
     self.assertEqual(repr(c), "<sllistnode('abc')>")
Ejemplo n.º 2
0
 def test_node_str(self):
     a = sllist([None, None]).first
     self.assertEqual(str(a), "sllistnode(None)")
     b = sllist([1, None]).first
     self.assertEqual(str(b), "sllistnode(1)")
     c = sllist(["abc", None]).first
     self.assertEqual(str(c), "sllistnode(abc)")
Ejemplo n.º 3
0
 def test_node_repr(self):
     a = sllist([None]).first
     self.assertEqual(repr(a), '<sllistnode(None)>')
     b = sllist([1, None]).first
     self.assertEqual(repr(b), '<sllistnode(1)>')
     c = sllist(['abc', None]).first
     self.assertEqual(repr(c), '<sllistnode(\'abc\')>')
Ejemplo n.º 4
0
 def test_node_str(self):
     a = sllist([None, None]).first
     self.assertEqual(str(a), 'sllistnode(None)')
     b = sllist([1, None]).first
     self.assertEqual(str(b), 'sllistnode(1)')
     c = sllist(['abc', None]).first
     self.assertEqual(str(c), 'sllistnode(abc)')
Ejemplo n.º 5
0
 def test_guards_after_concat(self):
     a = sllist([1, 2])
     b = sllist([3, 4])
     c = a + b
     self.assertIsNot(c.first, None)
     self.assertEqual(c.first.value, 1)
     self.assertIsNot(c.last, None)
     self.assertEqual(c.last.value, 4)
Ejemplo n.º 6
0
 def test_guards_after_concat_inplace(self):
     a = sllist([1, 2])
     b = sllist([3, 4])
     orig_a_first = a.first
     a += b
     self.assertIs(a.first, orig_a_first)
     self.assertEqual(a.first.value, 1)
     self.assertIsNot(a.last, None)
     self.assertEqual(a.last.value, 4)
Ejemplo n.º 7
0
 def test_insert_value_before_first(self):
     ll = sllist(xrange(4))
     ref = sllist([10, 0, 1, 2, 3])
     next = ll.nodeat(0)
     arg_node = sllistnode(10)
     new_node = ll.insert(arg_node, ll.nodeat(0))
     self.assertNotEqual(new_node, arg_node)
     self.assertEqual(new_node.value, 10)
     self.assertEqual(new_node.next, next)
     self.assertEqual(new_node, ll.first)
     self.assertEqual(ll, ref)
Ejemplo n.º 8
0
 def test_appendleft(self):
     ll = sllist(xrange(4))
     ref = sllist([10, 0, 1, 2, 3])
     next = ll.nodeat(0)
     arg_node = sllistnode(10)
     new_node = ll.appendleft(arg_node)
     self.assertNotEqual(new_node, arg_node)
     self.assertEqual(new_node.value, 10)
     self.assertEqual(new_node.next, next)
     self.assertEqual(ll.first, new_node)
     self.assertEqual(ll, ref)
Ejemplo n.º 9
0
 def test_cmp(self):
     a = sllist(xrange(0, 1100))
     b = sllist(xrange(0, 1101))
     c = [xrange(0, 1100)]
     self.assertEqual(cmp(a, a), 0)
     self.assertEqual(cmp(a, b), -1)
     self.assertEqual(cmp(b, a), 1)
     self.assertEqual(cmp(a, c), 1)
     self.assertEqual(cmp(c, a), -1)
     self.assertEqual(cmp([], []), 0)
     self.assertEqual(cmp([], a), -1)
     self.assertEqual(cmp(a, []), 1)
Ejemplo n.º 10
0
 def test_insert_value_before(self):
     ll = sllist(xrange(4))
     ref = sllist([0, 1, 10, 2, 3])
     prev = ll.nodeat(1)
     next = ll.nodeat(2)
     arg_node = sllistnode(10)
     new_node = ll.insert(arg_node, ll.nodeat(2))
     self.assertNotEqual(new_node, arg_node)
     self.assertEqual(new_node.value, 10)
     self.assertEqual(new_node.next, next)
     self.assertEqual(prev.next, new_node)
     self.assertEqual(ll, ref)
Ejemplo n.º 11
0
 def test_appendright(self):
     ll = sllist(xrange(4))
     ref = sllist([0, 1, 2, 3, 10])
     prev = ll.nodeat(-1)
     arg_node = sllistnode(10)
     new_node = ll.appendright(arg_node)
     self.assertNotEqual(new_node, arg_node)
     self.assertEqual(new_node.value, 10)
     self.assertEqual(new_node.next, None)
     self.assertEqual(prev.next, new_node)
     self.assertEqual(ll.last, new_node)
     self.assertEqual(ll, ref)
Ejemplo n.º 12
0
 def test_concat(self):
     a_ref = range(0, 1024, 4)
     a = sllist(a_ref)
     b_ref = range(8092, 8092 + 1024, 4)
     b = sllist(b_ref)
     ab_ref = sllist(a_ref + b_ref)
     c = a + b
     self.assertEqual(c, ab_ref)
     self.assertEqual(len(c), len(ab_ref))
     c = a + b_ref
     self.assertEqual(c, ab_ref)
     self.assertEqual(len(c), len(ab_ref))
Ejemplo n.º 13
0
 def test_concat_empty(self):
     empty = sllist()
     filled_ref = range(0, 1024, 4)
     filled = sllist(filled_ref)
     res = empty + empty
     self.assertEqual(res, sllist([] + []))
     self.assertEqual(len(res), 0)
     res = empty + filled
     self.assertEqual(res, sllist([] + filled_ref))
     self.assertEqual(len(res), len(filled_ref))
     res = filled + empty
     self.assertEqual(res, sllist(filled_ref + []))
     self.assertEqual(len(res), len(filled_ref))
Ejemplo n.º 14
0
 def test_init_empty(self):
     ll = sllist()
     self.assertEqual(len(ll), 0)
     self.assertEqual(ll.size, 0)
     self.assertEqual(list(ll), [])
     self.assertIs(ll.first, None)
     self.assertIs(ll.last, None)
Ejemplo n.º 15
0
 def test_guards_after_remove(self):
     ll = sllist([1, 2])
     ll.remove(ll.last)
     self.assertIs(ll.first, ll.last)
     ll.remove(ll.first)
     self.assertIs(ll.first, None)
     self.assertIs(ll.last, None)
Ejemplo n.º 16
0
def _add(letters):
    """
    Runs the loveAlgorithm to reduce a length-5 singly-linked list to length <= 2.

    Returns sllist([1]) if an infinite loop occurs on the input.
    """

    def step(sll):
        for node in sll.iternodes():
            try:
                node.value += node.next()
                if node.value >= 10:
                    sll.insertbefore(node, node.value/10)
                    node.value %= 10
            except TypeError:
                'reached end of sllist'
                sll.popright()

    visited = set()
    while len(letters) > 2:
        curr = str(letters)  # stringify the Sllist since you cannot hash a mutable object 
        if curr in visited:
            return sllist([1])
        visited.add(curr)

        step(letters)

    return letters
Ejemplo n.º 17
0
 def test_guards_after_insert(self):
     ll = sllist()
     node1 = ll.insert(sllistnode(1))
     self.assertIs(ll.first, node1)
     self.assertIs(ll.last, node1)
     node2 = ll.insert(sllistnode(2))
     self.assertIs(ll.first, node1)
     self.assertIs(ll.last, node2)
Ejemplo n.º 18
0
 def test_guards_after_appendleft(self):
     ll = sllist()
     node1 = ll.appendleft(1)
     self.assertIs(ll.first, node1)
     self.assertIs(ll.last, node1)
     node2 = ll.appendleft(2)
     self.assertIs(ll.first, node2)
     self.assertIs(ll.last, node1)
Ejemplo n.º 19
0
 def test_guards_after_repeat_inplace(self):
     ll = sllist([1, 2])
     orig_first = ll.first
     orig_last = ll.last
     ll *= 4
     self.assertIs(ll.first, orig_first)
     self.assertIsNot(ll.last, None)
     self.assertIsNot(ll.last, orig_last)
Ejemplo n.º 20
0
 def test_guards_after_popright(self):
     ll = sllist([1, 2])
     ll.pop()
     self.assertIs(ll.first, ll.last)
     self.assertEqual(ll.first.value, 1)
     ll.pop()
     self.assertIs(ll.first, None)
     self.assertIs(ll.last, None)
Ejemplo n.º 21
0
 def test_popright(self):
     ref = range(0, 1024, 4)
     ll = sllist(ref)
     result = ll.popright()
     self.assertEqual(result, ref[-1])
     self.assertEqual(len(ll), len(ref) - 1)
     self.assertEqual(ll.size, len(ref) - 1)
     self.assertEqual(ll.last.value, ref[-2])
     self.assertEqual(list(ll), ref[:-1])
Ejemplo n.º 22
0
 def test_guards_after_del(self):
     ll = sllist([1, 2])
     orig_last = ll.last
     del ll[0]
     self.assertIs(ll.first, orig_last)
     self.assertIs(ll.last, orig_last)
     del ll[0]
     self.assertIs(ll.first, None)
     self.assertIs(ll.last, None)
Ejemplo n.º 23
0
 def test_iter(self):
     ref = range(0, 1024, 4)
     ll = sllist(ref)
     idx = 0
     for val in ll:
         self.assertFalse(isinstance(val, sllistnode))
         self.assertEqual(val, ref[idx])
         idx += 1
     self.assertEqual(idx, len(ref))
Ejemplo n.º 24
0
 def test_popleft(self):
     ref = range(0, 1024, 4)
     ll = sllist(ref)
     result = ll.popleft()
     self.assertEqual(result, ref[0])
     self.assertEqual(len(ll), len(ref) - 1)
     self.assertEqual(ll.size, len(ref) - 1)
     self.assertEqual(ll.first.value, ref[1])
     self.assertEqual(list(ll), ref[1:])
Ejemplo n.º 25
0
 def test_iternext(self):
     ref = range(0, 1024, 4)
     ll = sllist(ref)
     idx = 100
     for node in ll.nodeat(100).iternext():
         self.assertTrue(isinstance(node, sllistnode))
         self.assertEqual(node.value, ref[idx])
         idx += 1
     self.assertEqual(idx, len(ref))
Ejemplo n.º 26
0
 def test_reversed(self):
     ref = range(0, 1024, 4)
     ll = sllist(ref)
     idx = len(ref) - 1
     for val in reversed(ll):
         self.assertFalse(isinstance(val, sllistnode))
         self.assertEqual(val, ref[idx])
         idx -= 1
     self.assertEqual(idx, -1)
Ejemplo n.º 27
0
 def test_guards_after_concat_inplace_of_self(self):
     ll = sllist([1, 2])
     orig_first = ll.first
     orig_last = ll.last
     ll += ll
     self.assertIs(ll.first, orig_first)
     self.assertEqual(ll.first.value, 1)
     self.assertIsNot(ll.last, None)
     self.assertIsNot(ll.last, orig_last)
     self.assertEqual(ll.last.value, 2)
Ejemplo n.º 28
0
 def test_init_with_sequence(self):
     ref = range(0, 1024, 4)
     ll = sllist(ref)
     self.assertEqual(len(ll), len(ref))
     self.assertEqual(ll.size, len(ref))
     self.assertEqual(list(ll), ref)
     self.assertIsNot(ll.first, None)
     self.assertEqual(ll.first.value, 0)
     self.assertIsNot(ll.last, None)
     self.assertEqual(ll.last.value, 1020)
Ejemplo n.º 29
0
 def test_getitem(self):
     ref = range(0, 1024, 4)
     ll = sllist(ref)
     for idx in xrange(len(ll)):
         self.assertFalse(isinstance(ll[idx], sllistnode))
         self.assertEqual(ll[idx], ref[idx])
     for idx in xrange(len(ll)):
         self.assertFalse(isinstance(ll[idx], sllistnode))
         self.assertEqual(ll[-idx - 1], ref[-idx - 1])
     self.assertRaises(TypeError, ll.__getitem__, None)
     self.assertRaises(TypeError, ll.__getitem__, 'abc')
     self.assertRaises(IndexError, ll.__getitem__, len(ref))
     self.assertRaises(IndexError, ll.__getitem__, -len(ref) - 1)
Ejemplo n.º 30
0
 def test_nodeat(self):
     ref = range(0, 1024, 4)
     ll = sllist(ref)
     for idx in xrange(len(ll)):
         self.assertTrue(isinstance(ll.nodeat(idx), sllistnode))
         self.assertEqual(ll.nodeat(idx).value, ref[idx])
     for idx in xrange(len(ll)):
         self.assertTrue(isinstance(ll.nodeat(idx), sllistnode))
         self.assertEqual(ll.nodeat(-idx - 1).value, ref[-idx - 1])
     self.assertRaises(TypeError, ll.nodeat, None)
     self.assertRaises(TypeError, ll.nodeat, 'abc')
     self.assertRaises(IndexError, ll.nodeat, len(ref))
     self.assertRaises(IndexError, ll.nodeat, -len(ref) - 1)
Ejemplo n.º 31
0
 def test_node_after_popleft(self):
     ll = sllist([1, 2])
     node = ll.first
     ll.popleft()
     self.assertIs(node.next, None)
     self.assertIs(node.list, None)
    # test using custom singly linked list class
    llist_1 = List(2)
    nodellist_11 = List(5)
    nodellist_12 = List(7)
    llist_1.next = nodellist_11
    nodellist_11.next = nodellist_12

    llist_2 = List(3)
    nodellist_21 = List(11)
    llist_2.next = nodellist_21

    print('traversing LL #1')
    print(llist_1.traverse())

    print('traversing LL #2')
    print(llist_2.traverse())

    print('\nTraversing result_list:')
    print(merge_two_sorted_linked_list(llist_1, llist_2).traverse())

    # test using python module for linked list
    A = [2, 5, 7]
    B = [3, 11]

    # initialize two sllist
    first_list = sllist(A)
    sec_list = sllist(B)

    print(merge_sorted_linked_list(first_list, sec_list))
Ejemplo n.º 33
0
 def test_repeat(self):
     ref = range(0, 1024, 4)
     ll = sllist(ref)
     self.assertEqual(ll * 4, sllist(ref * 4))
Ejemplo n.º 34
0
 def test_remove_invalid_node(self):
     ll = sllist([1, 2, 3, 4])
     self.assertRaises(ValueError, ll.remove, sllistnode())
     self.assertEqual(len(ll), 4)
Ejemplo n.º 35
0
 def test_repeat_inplace_empty(self):
     ll = sllist()
     ll *= 4
     self.assertEqual(ll, sllist([] * 4))
Ejemplo n.º 36
0
 def test_remove_from_empty_list(self):
     ll = sllist()
     self.assertRaises(ValueError, ll.remove, sllistnode())
Ejemplo n.º 37
0
 def test_list_readonly_attributes(self):
     ll = sllist(range(4))
     self.assertRaises(AttributeError, setattr, ll, 'first', None)
     self.assertRaises(AttributeError, setattr, ll, 'last', None)
     self.assertRaises(AttributeError, setattr, ll, 'size', None)
Ejemplo n.º 38
0
 def test_str(self):
     a = sllist([])
     self.assertEqual(str(a), 'sllist()')
     b = sllist([None, 1, 'abc'])
     self.assertEqual(str(b), 'sllist([None, 1, abc])')
Ejemplo n.º 39
0
 def test_list_hash(self):
     self.assertEqual(hash(sllist()), hash(sllist()))
     self.assertEqual(hash(sllist(range(0, 1024, 4))),
                      hash(sllist(range(0, 1024, 4))))
     self.assertEqual(hash(sllist([0, 2])), hash(sllist([0.0, 2.0])))
Ejemplo n.º 40
0
 def test_pop_from_empty_list(self):
     ll = sllist()
     self.assertRaises(ValueError, ll.pop)
     self.assertRaises(ValueError, ll.popleft)
     self.assertRaises(ValueError, ll.popright)
Ejemplo n.º 41
0
 def test_node_after_remove(self):
     ll = sllist([1, 2, 3])
     node = ll.nodeat(1)
     ll.remove(node)
     self.assertIs(node.next, None)
     self.assertIs(node.list, None)
Ejemplo n.º 42
0
 def test_repr(self):
     a = sllist([])
     self.assertEqual(repr(a), 'sllist()')
     b = sllist([None, 1, 'abc'])
     self.assertEqual(repr(b), 'sllist([None, 1, \'abc\'])')
Ejemplo n.º 43
0
 def test_getitem_empty(self):
     ll = sllist()
     self.assertRaises(TypeError, ll.__getitem__, None)
     self.assertRaises(TypeError, ll.__getitem__, 'abc')
     self.assertRaises(IndexError, ll.__getitem__, 0)
     self.assertRaises(IndexError, ll.__getitem__, -1)
Ejemplo n.º 44
0
 def __init__(self, data=None):
     self.data = sllist()
 def __init__(self):
     self.__lnkdlist = sllist()
Ejemplo n.º 46
0
 def test_repeat_empty(self):
     ll = sllist()
     self.assertEqual(ll * 4, sllist([] * 4))
Ejemplo n.º 47
0
 def test_node_after_popright(self):
     ll = sllist([1, 2])
     node = ll.last
     ll.pop()
     self.assertIs(node.list, None)
Ejemplo n.º 48
0
 def test_repeat_inplace(self):
     ref = range(0, 1024, 4)
     ll = sllist(ref)
     ll *= 4
     self.assertEqual(ll, sllist(ref * 4))
Ejemplo n.º 49
0
 def __init__(self):
     self.data = sllist()