Exemplo n.º 1
0
def sum(list_a, list_b):
    """Calculates the sum of LinkedList data.

    calculates the sum of linked list data in a reverse
    manner. The data is then added in a reverse manner 
    into a new LinkedList.

    Args: 
        list_a: a linked list containing an operand
        list_b: a linked list containing an operand

    Returns:
        a LinkedList containing the sum of the operand
        args in a reverse ordering.
    """

    sum_list = SinglyLinkedList()
    temp = list()
    total = 0
    remainder = 0

    for nodes in it.zip_longest(list_a, list_b):
        total = remainder
        if nodes[0] and nodes[1]:
            total += nodes[0].value + nodes[1].value
        elif nodes[0]:
            total += nodes[0].value
        elif nodes[1]:
            total += nodes[1].value
        temp.append(total % 10)
        remainder = total // 10
    [sum_list.insert(item) for item in reversed(temp)]
    return sum_list
Exemplo n.º 2
0
def follow_up(list_a, list_b):
    """Calculates the sum of LinkedList data.

    calculates the sum of linked list data in a reverse
    manner. The data is then added in a foward manner 
    into a new LinkedList.

    Args: 
        list_a: a linked list containing an operand
        list_b: a linked list containing an operand

    Returns:
        a LinkedList containing the sum of the operand
        args in a foward ordering.
    """
    sum_list = SinglyLinkedList()
    total = 0

    add_zeroes(list_a, list_b)
    for nodes in it.zip_longest(list_a, list_b):
        total = (total * 10) + nodes[0].value + nodes[1].value
    [sum_list.insert(int(value)) for value in reversed(str(total))]
    return sum_list
Exemplo n.º 3
0
class Testpalindrome(unittest.TestCase):
    def setUp(self):
        self.a = SinglyLinkedList()

    def test_stack_true_one(self):
        self.a.insert('c')
        self.a.insert('i')
        self.a.insert('v')
        self.a.insert('i')
        self.a.insert('c')
        self.assertTrue(palindrome.check_with_stack(self.a))

    def test_stack_true_two(self):
        self.a.insert('a')
        self.a.insert('n')
        self.a.insert('n')
        self.a.insert('a')
        self.assertTrue(palindrome.check_with_stack(self.a))

    def test_stack_false_one(self):
        self.a.insert('h')
        self.a.insert('e')
        self.a.insert('l')
        self.a.insert('l')
        self.a.insert('o')
        self.assertFalse(palindrome.check_with_stack(self.a))

    def test_stack_false_two(self):
        self.a.insert('c')
        self.a.insert('r')
        self.a.insert('i')
        self.a.insert('n')
        self.a.insert('g')
        self.a.insert('e')
        self.assertFalse(palindrome.check_with_stack(self.a))

    def test_recurs_true_one(self):
        self.a.insert('c')
        self.a.insert('i')
        self.a.insert('v')
        self.a.insert('i')
        self.a.insert('c')
        self.assertTrue(palindrome.check_with_recur(self.a))

    def test_recurs_true_two(self):
        self.a.insert('a')
        self.a.insert('n')
        self.a.insert('n')
        self.a.insert('a')
        self.assertTrue(palindrome.check_with_recur(self.a))

    def test_recurs_false_one(self):
        self.a.insert('h')
        self.a.insert('e')
        self.a.insert('l')
        self.a.insert('l')
        self.a.insert('o')
        self.assertFalse(palindrome.check_with_recur(self.a))

    #odd case
    def test_recurs_false_two(self):
        self.a.insert('d')
        self.a.insert('e')
        self.a.insert('i')
        self.a.insert('r')
        self.a.insert('t')
        self.assertFalse(palindrome.check_with_recur(self.a))

    #even case
    def test_recurs_false_three(self):
        self.a.insert('e')
        self.a.insert('g')
        self.a.insert('n')
        self.a.insert('i')
        self.a.insert('r')
        self.a.insert('c')
        self.assertFalse(palindrome.check_with_recur(self.a))
Exemplo n.º 4
0
class TestFollowUpSumLists(unittest.TestCase):
    def setUp(self):
        self.a = SinglyLinkedList()
        self.b = SinglyLinkedList()

    def test_same_length(self):
        self.a.insert(1)
        self.a.insert(2)
        self.a.insert(3)
        self.b.insert(3)
        self.b.insert(2)
        self.b.insert(1)
        self.a = sumlists.follow_up(self.a, self.b)

        self.c = [4, 4, 4]
        self.d = list()
        for node in self.a:
            self.d.append(node.value)
        self.assertListEqual(self.c, self.d)

    def test_remainders(self):
        self.a.insert(7)
        self.a.insert(1)
        self.a.insert(6)

        self.b.insert(5)
        self.b.insert(9)
        self.b.insert(2)

        self.a = sumlists.follow_up(self.a, self.b)

        self.c = [9, 1, 2]
        self.d = list()
        for node in self.a:
            self.d.append(node.value)
        self.assertListEqual(self.c, self.d)

    def test_diff_length(self):
        self.a.insert(3)
        self.a.insert(2)
        self.a.insert(1)

        self.b.insert(1)
        self.b.insert(2)
        self.b.insert(3)
        self.b.insert(1)
        self.a = sumlists.follow_up(self.a, self.b)

        self.c = [1, 4, 4, 4]
        self.d = list()
        for node in self.a:
            self.d.append(node.value)

        self.assertListEqual(self.c, self.d)
Exemplo n.º 5
0
class TestRmSortDups(unittest.TestCase):
    def setUp(self):
        self.a = SinglyLinkedList()

    def test_table_one_dup(self):
        self.a.insert(3)
        self.a.insert(2)
        self.a.insert(6)
        self.a.insert(2)
        self.a.insert(1)

        rmdups.remove_with_sort(self.a)
        self.a.delete(2)
        self.assertEqual(self.a.search(2), None)

    def test_table_multi_dup(self):
        self.a.insert(3)
        self.a.insert(2)
        self.a.insert(6)
        self.a.insert(2)
        self.a.insert(1)
        self.a.insert(1)

        rmdups.remove_with_table(self.a)
        self.a.delete(2)
        self.a.delete(1)
        self.assertEqual(self.a.search(2), None)
        self.assertEqual(self.a.search(1), None)

    def test_table_edge_dup(self):
        self.a.insert(1)
        self.a.insert(2)
        self.a.insert(6)
        self.a.insert(2)
        self.a.insert(1)
        self.a.insert(1)

        rmdups.remove_with_table(self.a)
        self.a.delete(2)
        self.a.delete(1)
        self.assertEqual(self.a.search(2), None)
        self.assertEqual(self.a.search(1), None)
Exemplo n.º 6
0
class TestIntersection(unittest.TestCase):
    def setUp(self):
        self.a = SinglyLinkedList()
        self.b = SinglyLinkedList()

    def test_same_len(self):
        c = Node(96)

        self.a.insert(1)
        self.a.insert_node(c)
        self.a.insert(2)

        self.b.insert(1)
        self.b.insert_node(c)
        self.b.insert(2)
        self.assertEqual(intersection.intersect(self.a, self.b), c)

    def test_diff_len_one(self):
        c = Node(96)
        self.a.insert_node(c)
        self.a.insert(3)

        self.b.insert_node(c)
        self.b.insert(5)
        self.b.insert(6)
        self.b.insert(7)
        self.b.insert(8)
        self.assertEqual(intersection.intersect(self.a, self.b), c)

    def test_diff_len_two(self):
        a = Node(23)
        b = Node(45)
        c = Node(96)

        self.a.insert_node(a)
        self.a.insert_node(b)
        self.a.insert_node(c)
        self.a.insert(3)

        self.b.insert_node(a)
        self.b.insert_node(b)
        self.b.insert_node(c)
        self.b.insert(5)
        self.b.insert(6)
        self.b.insert(7)
        self.b.insert(8)
        self.assertEqual(intersection.intersect(self.a, self.b), c)
Exemplo n.º 7
0
class TestSinglyLinkedList(unittest.TestCase):
    def setUp(self):
        self.a = SinglyLinkedList()

    def test_len_empty(self):

        self.assertEqual(len(self.a), 0)

    def test_len_one(self):
        self.a.insert(3)
        self.a.insert(2)

        self.assertEqual(len(self.a), 2)

    def test_len_two(self):
        self.a.insert(3)
        self.a.insert(2)
        self.a.insert(2)
        self.a.insert(2)
        self.a.insert(2)
        self.a.insert(2)

        self.assertEqual(len(self.a), 6)

    def test_insert_zero(self):
        self.a.insert(0)
        self.assertEqual(self.a.head.value, 0)

    def test_insert_one(self):
        self.a.insert(1)
        self.a.insert(2)
        self.a.insert(4)
        self.assertEqual(self.a.head.value, 4)

    def test_search_one(self):
        self.a.insert(44)
        found_node = self.a.search(44)
        self.assertIsInstance(found_node, Node)
        self.assertEqual(found_node.value, 44)

    def test_search_two(self):
        self.a.insert(44)
        self.a.insert(66)
        self.a.insert(67)
        self.a.insert(34)
        self.a.insert(78)
        found_node = self.a.search(78)
        self.assertIsInstance(found_node, Node)
        self.assertEqual(found_node.value, 78)

    def test_search_no_exist(self):
        self.a.insert(44)
        self.a.insert(66)
        self.a.insert(67)
        self.a.insert(34)
        self.a.insert(78)
        self.assertEqual(self.a.search(100), None)

    def test_delete_zero(self):
        self.a.insert(44)
        self.a.delete(44)
        self.assertEqual(self.a.search(66), None)

    def test_delete_one(self):
        self.a.insert(44)
        self.a.insert(66)
        self.a.insert(67)
        self.a.delete(66)
        self.assertEqual(self.a.search(66), None)

    def test_delete_two(self):
        self.a.insert(44)
        self.a.insert(66)
        self.a.insert(67)
        self.a.delete(67)
        self.assertEqual(self.a.search(67), None)