def test_one_odd_node(self):
        node = Node(3)
        one_odd_list = LinkedList()
        one_odd_list.add_node(node)
        one_odd_list.remove_odds()

        "it should produce and empty list has only one node, which is odd"
        self.assertTrue(one_odd_list.is_empty())
    def test_all_even(self):
        all_even = LinkedList()
        node_vals = [2, 4, 6]
        for node_val in node_vals:
            all_even.add_node(Node(node_val))

        all_even.remove_odds()
        "It should not remove any nodes if all nodes are even"
        self.assertEqual(all_even.get_length(), 3)
    def test_tail_even(self):
        even_tail = LinkedList()
        node_vals = [2, 3, 4, 10]
        for node_val in node_vals:
            even_tail.add_node(Node(node_val))

        even_tail.remove_odds()
        "it should remove odds if the tail is even and the head is even"
        self.assertEqual(even_tail.tail.data, 10)
        self.assertEqual(even_tail.get_length(), 3)
    def test_tail_odd(self):
        odd_tail = LinkedList()
        node_vals = [2, 3, 4, 5]
        for node_val in node_vals:
            odd_tail.add_node(Node(node_val))

        odd_tail.remove_odds()
        "it should remove the tail if the tail is odd and the head is even"
        self.assertEqual(odd_tail.tail.data, 4)
        self.assertEqual(odd_tail.get_length(), 2)
    def test_all_odd(self):
        all_odd = LinkedList()
        node_vals = [1, 3, 5, 7]
        for node_val in node_vals:
            all_odd.add_node(Node(node_val))

        all_odd.remove_odds()
        "it should remove all nodes if all nodes are odd"
        self.assertEqual(all_odd.get_length(), 0)
        self.assertEqual(all_odd.head, None)
        self.assertEqual(all_odd.tail, None)
    def test_head_odd(self):
        odd_head = LinkedList()
        node_vals = [3, 4, 5, 6]
        for node_val in node_vals:
            odd_head.add_node(Node(node_val))

        odd_head.remove_odds()
        "it should remove odds when the head node is odd and the tail is even"
        self.assertEqual(odd_head.head.data, 4)
        self.assertEqual(odd_head.get_length(), 2)
        self.assertEqual(odd_head.tail.data, 6)
Exemple #7
0
def main():
    l1 = LinkedList()
    l2 = LinkedList()
    l3 = LinkedList()
    l4 = LinkedList()
    for i in ['a', 'b', 'c', 'b', 'a']:
        l1.add_node(i)
    for i in ['a', 'b', 'c', 'c', 'b', 'a']:
        l2.add_node(i)
    for i in ['a', 'b', 'c']:
        l3.add_node(i)
    for i in ['a', 'b', 'c', 'd']:
        l4.add_node(i)
    print "l1:"
    print_nodes(l1.root)
    print is_palindrome(l1.root)
    print "l2:"
    print_nodes(l2.root)
    print is_palindrome(l2.root)
    print "l3:"
    print_nodes(l3.root)
    print is_palindrome(l3.root)
    print "l4:"
    print_nodes(l4.root)
    print is_palindrome(l4.root)
Exemple #8
0
def main():
    l1 = LinkedList()
    l2 = LinkedList()
    for i in reversed(range(1, 4)):
        l1.add_node(i)
    for i in reversed(range(6, 9)):
        l2.add_node(i)
    print "l1:"
    print_nodes(l1.root)
    print "l2:"
    print_nodes(l2.root)
    print "sum:"
    sum_ll_root = add_numbers(l1.root, l2.root)
    print_nodes(sum_ll_root)
Exemple #9
0
def main():
    l1 = LinkedList()
    for i in [100, 80, 60, 40, 20, 0]:
        l1.add_node(i)
    print "l1:"
    print_nodes(l1.root)
    l1_reversed = reverse_iterative(l1.root)
    print "l1_reversed:"
    print_nodes(l1_reversed)
    print "Reverse LinkedList from node no 2 to node 4"
    l1 = LinkedList()
    for i in [100, 80, 60, 40, 20, 0]:
        l1.add_node(i)
    l1_reversed = reverse_between(l1.root, 2, 4)
    print_nodes(l1_reversed)
Exemple #10
0
def add_numbers(l1, l2):
    """
    Add two numbers represented by LinkedList

    :param l1: root of linked list
    :param l2: root of linked list
    :return: sum_ll_root: root of linked list
    """
    l1 = reverse_iterative(l1)
    l2 = reverse_iterative(l2)
    sum_ll = LinkedList()
    carry = 0
    while l1 is not None and l2 is not None:
        sum = l1.get_data() + l2.get_data() + carry
        s = sum % 10
        carry = sum / 10
        print carry, s, l1.get_data(), l2.get_data(), carry
        sum_ll.add_node(s)
        l1 = l1.get_next()
        l2 = l2.get_next()
    if carry:
        sum_ll.add_node(carry)
    sum_ll_root = reverse_iterative(sum_ll.root)
    return sum_ll_root
Exemple #11
0
 def test_one_even_node(self):
     one_even_list = LinkedList()
     one_even_list.add_node(Node(2))
     one_even_list.remove_odds()
     "it should not change the list if the list has ony one node, which is even"
     self.assertEqual(one_even_list.get_length(), 1)