def test_inequality_with_different_nodes(self):
        first = SinglyLinkedList()
        first.append(100)
        second = SinglyLinkedList()
        second.append(100)
        second.append(200)
        second.append(300)

        self.assertNotEqual(first, second)
    def test_equality_with_values(self):
        first = SinglyLinkedList()
        first.append(100)
        first.append(200)
        first.append(300)
        second = SinglyLinkedList()
        second.append(100)
        second.append(200)
        second.append(300)

        self.assertEqual(first, second)
예제 #3
0
def merge(a, b):
    '''
    Merges two linked list sorting data in the nodes
    Returns a list_merged linked list
    '''
    list_merged = SinglyLinkedList()
    list_merged.prepend(Node(
        0))  # Add a fake head that is discarted later to avoid empty lists
    current = list_merged.head
    a_head = a.head
    b_head = b.head

    # Iterate over each node until we reach the tail one and build the list by updating the current next node
    while a_head != None or b_head != None:
        if a_head is None:
            current.next = b_head
            b_head = b_head.next
        elif b_head is None:
            current.next = a_head
            a_head = a_head.next
        else:
            if a_head.data < b_head.data:
                current.next = a_head
                a_head = a_head.next
            else:
                current.next = b_head
                b_head = b_head.next
        current = current.next

    # Discard fake head and set first list_merged node as head
    list_merged.head = list_merged.head.next
    return list_merged
    def test_remove(self):
        linked = SinglyLinkedList(100)
        linked.append(200)
        linked.append(300)
        linked.remove(1)

        self.assertEqual(linked.as_list(), [100, 300])
예제 #5
0
파일: 2_5.py 프로젝트: zomglings/intprep
def sumLists(a, b):
    current_a = a.head
    current_b = b.head
    result = SinglyLinkedList()

    # Digit addition step
    while (current_a is not None) and (current_b is not None):
        result.append(SinglyLinkedNode(current_a.value + current_b.value))
        current_a = current_a.child
        current_b = current_b.child
    while current_a is not None:
        result.append(SinglyLinkedNode(current_a.value))
        current_a = current_a.child
    while current_b is not None:
        result.append(SinglyLinkedNode(current_b.value))
        current_b = current_b.child

    # Carries step
    current_result = result.head
    while current_result is not None:
        if current_result.value >= 10:
            carry = int(current_result.value / 10)
            current_result.value = current_result.value - 10 * carry
            if current_result.child is not None:
                current_result.child.value += carry
            else:
                current_result.child = SinglyLinkedNode(carry)

        current_result = current_result.child

    return result
    def test_as_list(self):
        linked = SinglyLinkedList()
        linked.append(100)
        linked.append(200)
        linked.append(300)

        self.assertEqual(linked.as_list(), [100, 200, 300])
    def test_insert_in_the_middle(self):
        linked = SinglyLinkedList(100)
        linked.append(200)
        linked.append(300)
        linked.insert(1000, 1)

        self.assertEqual(linked.as_list(), [100, 1000, 200, 300])
예제 #8
0
def linked_list_sum(first, second):
    head = None
    current = None
    carry = 0
    while first != None or second != None or carry != 0:
        if first != None:
            first_num = first.value
            first = first.next
        else:
            first_num = 0

        if second != None:
            second_num = second.value
            second = second.next
        else:
            second_num = 0

        total = first_num + second_num + carry
        digit = total % 10
        carry = total / 10
        new_node = SinglyLinkedList(digit)

        if current != None:
            current.next = new_node

        if head == None:
            head = new_node

        current = new_node

    return head
    def test_append_multiple_with_initial_value(self):
        linked = SinglyLinkedList(1000)
        linked.append(100)
        linked.append(200)
        linked.append(300)

        self.assertEqual(linked.as_list(), [1000, 100, 200, 300])
예제 #10
0
파일: 2_8.py 프로젝트: zomglings/intprep
    def create_list(self, length_neck, length_loop):
        neck = SinglyLinkedList()
        for i in range(length_neck):
            neck.prepend(SinglyLinkedNode(length_neck - i))

        loop = SinglyLinkedList()
        for i in range(length_loop):
            loop.prepend(SinglyLinkedNode(length_neck + length_loop - i))

        if loop.head is not None:
            end = loop.head
            while end.child is not None:
                end = end.child
            end.child = loop.head

            neck.append(loop.head)
        return neck
    def test_remove_at_the_beginning_with_multiple_values(self):
        linked = SinglyLinkedList(100)
        linked.append(200)
        linked.append(300)
        linked.append(400)
        linked.remove(0)

        self.assertEqual(linked.as_list(), [200, 300, 400])
예제 #12
0
def split(list):
    '''
    Splits a linked list into two new ones right in the middle or None in the right if it has 1 node only
    '''
    left = SinglyLinkedList()
    right = SinglyLinkedList()

    if list is None or list.head is None:
        left = list
        right = None
    else:
        mid_point = list.size() // 2
        mid_node = list.node_at_index(mid_point - 1)
        left = list
        right.head = mid_node.next
        mid_node.next = None
    return left, right
    def test_direct_access_to_elements_more_than_one_element(self):
        linked = SinglyLinkedList()
        linked.append(100)
        linked.append(200)
        linked.append(300)
        linked.append(400)

        self.assertEqual(linked[2], 300)
예제 #14
0
    def setup_method(self):
        self.test_list = SinglyLinkedList()

        self.test_list.append(42)
        self.test_list.append(33)
        self.test_list.append('spam')
        self.test_list.prepend([])
        self.test_list.prepend({'a': 42})
    def test_item_setting_by_index_overrides_previous_value_at_the_end(self):
        linked = SinglyLinkedList()
        linked.append(100)
        linked.append(200)
        linked.append(300)
        linked[2] = 400

        self.assertEqual(linked.as_list(), [100, 200, 400])
    def test_remove_at_the_end(self):
        linked = SinglyLinkedList(100)
        linked.append(200)
        linked.append(300)
        linked.append(400)
        linked.append(500)
        linked.remove(4)

        self.assertEqual(linked.as_list(), [100, 200, 300, 400])
예제 #17
0
 def rebuild(self, bincount):
     """rebuild."""
     tmp = self.table
     self.table = [SinglyLinkedList() for _ in range(bincount)]
     self.size = 0
     for linked_list in tmp:
         for node in linked_list:
             self[node.key] = node.item
     del tmp
예제 #18
0
 def __init__(self, bin_count=10, max_load=0.7, hashfunc=hash):
     """__init__."""
     super(ChainedHashDict, self).__init__()
     if bin_count <= 0:
         raise TypeError("Bin count must be greater than zero.")
     self.table = [SinglyLinkedList() for _ in range(bin_count)]
     self.max_load = max_load
     self.hash = hashfunc
     self.size = 0
예제 #19
0
def test_linked_list_empty():
    """test_linked_list_empty."""
    ll = SinglyLinkedList()
    for _ in ll:
        raise AssertionError("Collection should be empty, iterator returned.")
    assert not any(value in ll for value in range(-100, 100))
    assert ll.remove(5) is None
    assert len(ll) is 0
    print("No items, ", ll)
    print("Passed all tests for empty linked list.")
예제 #20
0
파일: 2_4.py 프로젝트: zomglings/intprep
    def test_2(self):
        linked_list = SinglyLinkedList()
        values = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
        for value in values:
            linked_list.append(SinglyLinkedNode(value))

        partition(linked_list, 10)

        traversal = linked_list.traverse()
        self.assertEqual(traversal, [9, 8, 7, 6, 5, 4, 3, 2, 1, 10])
예제 #21
0
파일: 2_1.py 프로젝트: zomglings/intprep
    def test_problem(self):
        linked_list = SinglyLinkedList(SinglyLinkedNode(-1))
        for i in range(100):
            linked_list.append(SinglyLinkedNode(i))
            linked_list.append(SinglyLinkedNode(i))

        for i in range(100):
            linked_list.append(SinglyLinkedNode(0))

        deduplicate(linked_list)
        self.assertEqual(linked_list.traverse(), [-1] + range(100))
예제 #22
0
파일: 2_4.py 프로젝트: zomglings/intprep
    def test_1(self):
        linked_list = SinglyLinkedList()
        values = [3, 5, 8, 5, 10, 2, 1]
        for value in values:
            linked_list.append(SinglyLinkedNode(value))

        partition(linked_list, 5)

        traversal = linked_list.traverse()
        self.assertEqual(set(traversal[:3]), set([1, 2, 3]))
        self.assertEqual(set(traversal[3:]), set([5, 8, 10]))
        self.assertEqual(len(traversal), 7)
예제 #23
0
    def test_3(self):
        linked_list = SinglyLinkedList(SinglyLinkedNode(0))
        last = 100000
        current = last
        current_node = SinglyLinkedNode(current)
        while current > 0:
            current -= 1
            current_node = SinglyLinkedNode(current, current_node)
        linked_list.append(current_node)

        k = 7623

        self.assertEqual(toLast(k, linked_list), last - k)
예제 #24
0
파일: main.py 프로젝트: jayraj3/coding-task
def main():
    List = SinglyLinkedList(
    )  # creating list to store the main Fibonacci sequence

    count = 100
    cal_fib = fibonacci(count)  # fibonacci generator object

    # add data in linked list
    for i in range(100):
        List.insert(next(cal_fib))

    print(List)  # print linked list data
    List.reverse()  # reverse linked list
    print(List)  # print reverse linked list
예제 #25
0
파일: 2_6.py 프로젝트: zomglings/intprep
def palindromic(l):
    r = SinglyLinkedList()

    current = l.head
    while current is not None:
        r.prepend(SinglyLinkedNode(current.value))
        current = current.child

    l_iter = l.head
    r_iter = r.head
    while l_iter is not None:
        if l_iter.value != r_iter.value:
            return False
        l_iter = l_iter.child
        r_iter = r_iter.child
    return True
예제 #26
0
def test_linked_list_single_item():
    """test_linked_list_single_item."""
    ll = SinglyLinkedList()
    ll.prepend(5)
    assert ll.head.item is 5
    print("One item, ", ll)
    for node in ll:
        assert node.item is 5
    assert 5 in ll
    assert not any(value in ll for value in range(-100, 100) if value is not 5)
    assert len(ll) is 1
    assert ll.remove(5) is 5
    assert 5 not in ll
    assert len(ll) is 0
    for _ in ll:
        raise AssertionError("Collection should be empty, iterator returned.")
    print("Passed all tests for single item in linked list")
예제 #27
0
def test_linked_list_many_items():
    """test_linked_list_many_items."""
    ll = SinglyLinkedList()
    numbers = [value for value in range(-100, 100)]
    for number in numbers:
        ll.prepend(number)
    print("Many items, ", ll)
    assert len(ll) == len(numbers)
    assert [node.item for node in ll] == numbers[-1::-1]
    assert all(number in ll for number in numbers)
    assert not any(
        number in ll for number in chain(range(-200, -100), range(100, 200)))
    assert ll.remove(500) is None
    assert all(ll.remove(value) is not None for value in numbers)
    assert not any(number in ll for number in numbers)
    assert len(ll) is 0
    for _ in ll:
        raise AssertionError("Collection should be empty, iterator returned.")
    print("Passed all tests for single item in linked list")
예제 #28
0
    def test_2(self):
        linked_list = SinglyLinkedList(SinglyLinkedNode(-1))
        for i in range(100):
            linked_list.append(SinglyLinkedNode(i))

        self.assertIsNone(toLast(1000, linked_list))
예제 #29
0
    def test_1(self):
        linked_list = SinglyLinkedList(SinglyLinkedNode(0))
        for i in range(1, 100):
            linked_list.append(SinglyLinkedNode(i))

        self.assertEqual(toLast(1, linked_list), 98)
예제 #30
0
        if current != None:
            current.next = new_node

        if head == None:
            head = new_node

        current = new_node

    return head


first = SinglyLinkedList(
    1,
    nxt=SinglyLinkedList(2,
                         nxt=SinglyLinkedList(
                             3,
                             nxt=SinglyLinkedList(4,
                                                  nxt=SinglyLinkedList(5)))))
second = SinglyLinkedList(7, nxt=SinglyLinkedList(9, nxt=SinglyLinkedList(6)))
expected = SinglyLinkedList(
    8,
    nxt=SinglyLinkedList(1,
                         nxt=SinglyLinkedList(
                             0,
                             nxt=SinglyLinkedList(5,
                                                  nxt=SinglyLinkedList(5)))))
assert linked_list_sum(first, second) == expected

first = SinglyLinkedList(9, nxt=SinglyLinkedList(9))
second = SinglyLinkedList(5, nxt=SinglyLinkedList(2))