コード例 #1
0
    def _run_test(data, correct):
        l = linked_list.List()

        for item in data:
            node = linked_list.Node(item)
            l.add_last(node)

        assert is_palindrom(l) == correct
コード例 #2
0
    def test1(self):
        l = linked_list.List([1, 2, 3])
        last = l.head.next.next
        k = l.head.next
        last.next = k

        actual = detect_list_cycles(l)
        self.assertTrue(k is actual)
コード例 #3
0
 def test_remove_node(self):
     my_list = linked_list.List(linked_list.Node(5))
     node = linked_list.Node(7)
     my_list.append(node)
     my_list.append(linked_list.Node(9))
     my_list.remove(node)
     self.assertEqual(my_list.get_head().get_data(), 5)
     self.assertEqual(my_list.get_head().get_next().get_data(), 9)
     self.assertEqual(my_list.get_tail().get_data(), 9)
コード例 #4
0
ファイル: sexpr.py プロジェクト: black13/sexpr
def from_python(expr):
    """ Use python syntax to build SExprs

        Unfortunately, you can't omit the commas :P.
    """
    if isinstance(expr, Expression):
        return expr
    if isinstance(expr, basestring):
        return String(0, 0, str(expr))
    if isinstance(expr, int) or isinstance(expr, long):
        return Int(0, 0, expr)
    # No tokens at this level
    list = linked_list.List()
    for elt in expr:
        list.append(from_python(elt))
    return List(0, 0, list)
コード例 #5
0
ファイル: parser.py プロジェクト: black13/sexpr
 def next(self):
     # The lexer already takes care of ( vs ) matching
     lx = self._lexer.next()
     assert type(lx) in (BeginList, EndList, String, Token)
     if not isinstance(lx, BeginList):
         if isinstance(lx, Token):
             return lx._maybe_int()
         return lx
     line = lx.line
     column = lx.column
     lst = linked_list.List()
     while True:
         # Not self._lexer.next() - we recurse
         lx = self.next()
         if isinstance(lx, EndList):
             return List(line, column, lst)
         lst.append(lx)
コード例 #6
0
    def random_cyclical_list(self):
        upper = 50
        lower = 10
        n = random.randint(lower, upper)
        l = linked_list.List([random.randint(0, 10000) for _ in range(n)])

        # Get last element in list
        node = l.head
        while node.next != None:
            node = node.next
        last = node

        # Get k'th element from list
        k = random.randint(1, n - 1)
        node = l.head
        for _ in range(k):
            node = node.next

        # Assign last element's next node to element k
        last.next = node

        return l, node
コード例 #7
0
ファイル: tasks.py プロジェクト: logotip123/py_tasktracker
 def __init__(self):
     self._array = linked_list.List()
コード例 #8
0
 def test_list_creation(self):
     my_list = linked_list.List(linked_list.Node(2))
     self.assertEqual(my_list.get_head().get_data(), 2)
     self.assertEqual(my_list.get_tail().get_data(), 2)
コード例 #9
0
 def test_remove_head(self):
     my_list = linked_list.List(linked_list.Node(5))
     node = linked_list.Node(7)
     my_list.append(node)
     my_list.remove(linked_list.Node(5))
     self.assertEqual(my_list.get_head().get_data(), 7)
コード例 #10
0
 def test_append(self):
     my_list = linked_list.List(linked_list.Node(5))
     node = linked_list.Node(7)
     my_list.append(node)
     self.assertEqual(my_list.get_tail().get_data(), 7)
     self.assertEqual(my_list.get_head().get_next().get_data(), 7)
コード例 #11
0
    # creating a gap of k items between two pointers
    for _ in range(k):
        if not gap_node:
            raise KeyError("There a less than k items in the list")
        gap_node = gap_node.next

    curr_node = l.head
    while gap_node:
        curr_node = curr_node.next
        gap_node = gap_node.next

    if not curr_node:
        raise KeyError("Not valid value of k")

    return curr_node


if __name__ == '__main__':
    l = linked_list.List()

    for i in range(20):
        node = linked_list.Node(i)
        l.add_last(node)

    kth_from_the_end_node = kth_last(l, 5)
    assert kth_from_the_end_node.value == 15

    kth_from_the_end_node = kth_last(l, 20)
    assert kth_from_the_end_node.value == 0