Пример #1
0
def add_two_linkedlist(node1, node2, followup=False):
    #4->2->8->1->9: 91824    4->2->8->1->9: 91824
    #4->2->8->1->9: 91824    4->2->8->1:     1824
    #sum:          183648                   93648

    carry = 0
    result_node = None

    if not followup:
        while node1 != None or node2 != None:
            if node2 == None:
                digit = node1.data + carry
            if node1 == None:
                digit = node2.data + carry
            if node1 != None and node2 != None:
                digit = node1.data + node2.data + carry

            carry = 0  # reset carry

            if digit >= 10:
                digit = digit % 10
                carry = 1

            if result_node == None:
                result_node = LinkedListNode(digit)
            else:
                result_node.append_to_tail(digit)

            if node1 != None:
                node1 = node1.next
            if node2 != None:
                node2 = node2.next

        if carry != 0:
            result_node.append_to_tail(carry)

        return result_node
Пример #2
0
def add_two_linkedlist(node1, node2, followup=False):
    # 4->2->8->1->9: 91824    4->2->8->1->9: 91824
    # 4->2->8->1->9: 91824    4->2->8->1:     1824
    # sum:          183648                   93648

    carry = 0
    result_node = None

    if not followup:
        while node1 != None or node2 != None:
            if node2 == None:
                digit = node1.data + carry
            if node1 == None:
                digit = node2.data + carry
            if node1 != None and node2 != None:
                digit = node1.data + node2.data + carry

            carry = 0  # reset carry

            if digit >= 10:
                digit = digit % 10
                carry = 1

            if result_node == None:
                result_node = LinkedListNode(digit)
            else:
                result_node.append_to_tail(digit)

            if node1 != None:
                node1 = node1.next
            if node2 != None:
                node2 = node2.next

        if carry != 0:
            result_node.append_to_tail(carry)

        return result_node
Пример #3
0
    def setUp(self):
        head = LinkedListNode('a')
        head = head.append_to_tail('b')
        head = head.append_to_tail('b')
        head = head.append_to_tail('c')

        other_head = LinkedListNode(4)
        other_head = other_head.append_to_tail(2)
        other_head = other_head.append_to_tail(8)
        other_head = other_head.append_to_tail(1)
        other_head = other_head.append_to_tail(9)

        another_head = LinkedListNode(4)
        another_head = another_head.append_to_tail(2)
        another_head = another_head.append_to_tail(8)
        another_head = another_head.append_to_tail(1)

        loop0 = LinkedListNode('a')
        loop1 = LinkedListNode(data='b', next=loop0)
        loop2 = LinkedListNode(data='c', next=loop1)
        loop3 = LinkedListNode(data='d', next=loop2)
        loop4 = LinkedListNode(data='e', next=loop3)
        loop5 = LinkedListNode(data='f', next=loop4)
        loop0.next = loop3

        palindrome_odd = LinkedListNode('a')
        palindrome_odd = palindrome_odd.append_to_tail('b')
        palindrome_odd = palindrome_odd.append_to_tail('c')
        palindrome_odd = palindrome_odd.append_to_tail('b')
        palindrome_odd = palindrome_odd.append_to_tail('a')

        palindrome_even = LinkedListNode('a')
        palindrome_even = palindrome_even.append_to_tail('b')
        palindrome_even = palindrome_even.append_to_tail('b')
        palindrome_even = palindrome_even.append_to_tail('a')

        palindrome = LinkedListNode('a')
        palindrome = palindrome.append_to_tail('b')
        palindrome = palindrome.append_to_tail('c')
        palindrome = palindrome.append_to_tail('d')
        palindrome = palindrome.append_to_tail('e')

        self.board = [['x','x','o'],
                      ['o','o','x'],
                      ['x','o','x']]

        self.board_diag = [['x','x','o'],
                           ['o','x','x'],
                           ['x','o','x']]

        self.sequences = [1, 2, 4, 7, 10, 11, 7, 12, 6, 7, 16, 18, 19]
        self.palindrome = palindrome #it's a->b->c->d->e
        self.palindrome_odd = palindrome_odd #it's a->b->c->b->a
        self.palindrome_even = palindrome_even #it's a->b->b->a
        self.loop_head = loop5 #it's f->e->d->c->b->a->d
        self.head = head #it's a->b->b->c
        self.other_head = other_head #it's 4->2->8->1->9
        self.another_head = another_head #it's 4->2->8->1
        self.node = self.head.next.next #it's b. b->c
        self.node_none = self.head.next.next.next #it's c. c->None
        self.words = {'Do': 1, 'Here': 1, 'I': 1, 'a': 2, 'book': 2,
                      'check': 1, 'find': 1, 'for': 1, 'frequencies': 1,
                      'important': 2, 'in': 2, 'is': 1, 'need': 1,
                      'testing': 1, 'the': 2, 'this': 1, 'to': 1,
                      'topics': 1, 'very': 6, 'words': 1}