Beispiel #1
0
def partition_by_x(head, x):
    node_more_head = None
    node_less_tail = None

    while head != None:
        # Update the more linkedlist, append to tail. more_head->...more
        if head.data > x:
            if not node_more_head:
                node_more_head = LinkedListNode(head.data)
                more_head = node_more_head
            else:
                node_more_tail = LinkedListNode(head.data)
                while node_more_head.next != None:
                    node_more_head = node_more_head.next
                node_more_head.next = node_more_tail
                print "more: %s" % more_head.to_str()
        else:
            # Update the less linkedlist, update the head. less...->less_tail
            if not node_less_tail:
                node_less_tail = LinkedListNode(head.data)
                less_tail = node_less_tail

            else:
                node_less_head = LinkedListNode(head.data)
                node_less_head.next = node_less_tail
                node_less_tail = node_less_head
                print "less: %s" % node_less_tail.to_str()

        head = head.next
    # concatenat. less...->less_tail->more_head->...more
    less_tail.next = more_head

    return node_less_tail
Beispiel #2
0
def partition_by_x(head, x):
    node_more_head = None
    node_less_tail = None

    while head != None:
        #Update the more linkedlist, append to tail. more_head->...more
        if head.data > x:
            if not node_more_head:
                node_more_head = LinkedListNode(head.data)
                more_head = node_more_head
            else:
                node_more_tail = LinkedListNode(head.data)
                while node_more_head.next != None:
                    node_more_head = node_more_head.next
                node_more_head.next = node_more_tail
                print 'more: %s' % more_head.to_str()
        else:
            #Update the less linkedlist, update the head. less...->less_tail
            if not node_less_tail:
                node_less_tail = LinkedListNode(head.data)
                less_tail = node_less_tail

            else:
                node_less_head = LinkedListNode(head.data)
                node_less_head.next = node_less_tail
                node_less_tail = node_less_head
                print 'less: %s' % node_less_tail.to_str()

        head = head.next
    #concatenat. less...->less_tail->more_head->...more
    less_tail.next = more_head

    return node_less_tail
Beispiel #3
0
def sum_lists_recursive(node_a, node_b, carry=0):
    if not node_a and not node_b and carry == 0:
        return None

    result = LinkedListNode(0)
    value = carry
    if node_a:
        value += node_a.value
    if node_b:
        value += node_b.value

    result.value = value % 10

    if node_a or node_b:
        more = LinkedListNode(sum_lists_recursive(
            node_a.next if node_a else None,
            node_b.next if node_b else None,
            1 if value >= 10 else 0
        ))
        result.next = more
    return result
Beispiel #4
0
 def push(self, data):
     """Push data to the stack top. Add new head.
     """
     new_top = LinkedListNode(data)
     new_top.next = self.top
     self.top = new_top
Beispiel #5
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}
Beispiel #6
0
 def push(self, data):
     """Push data to the stack top. Add new head.
     """
     new_top = LinkedListNode(data)
     new_top.next = self.top
     self.top = new_top