예제 #1
0
def sumLists(lst1, lst2):
    '''
    find sum of the input lists and output as a list
    '''
    curr1, curr2 = lst1.head, lst2.head
    result = LinkedList()
    carry = 0
    while curr1 is not None and curr2 is not None:
        res = curr1.data + curr2.data + carry
        carry = res / 10
        res = res % 10
        result.add(Node(res))
        curr1 = curr1.next
        curr2 = curr2.next
    rem = curr1 if curr2 is None else curr2

    while rem is not None:
        res = rem.data + carry
        carry = 0
        if res > 9:
            res = res - 10
            carry = 1
        result.add(Node(res))
        rem = rem.next
    return result
예제 #2
0
def test_isPalindrome():
    '''
    test for isPalindrome methods
    '''
    lst = LinkedList()
    lst.add(1).add(2).add(3)

    assert not isPalindrome(lst)
    assert not isPalin2(lst.head, lst.head)[0]

    lst.add(2).add(1)

    assert isPalindrome(lst)
    assert isPalin2(lst.head, lst.head)[0]

    print 'Test Passed.'
예제 #3
0
def test_hasLoop():
    '''
    test for hasLoop method
    '''
    print 'Testing hasLoop method ... ',
    nodea = Node('a')
    lst = LinkedList()
    ignore = [lst.add(Node(x)) for x in range(4)]
    lst.add(nodea)
    ignore = [lst.add(Node(x)) for x in range(10)]

    assert not hasLoop(lst)[0]

    lst.tail.next = nodea
    result, node = hasLoop(lst)
    assert result
    assert node is nodea
    print 'Passed'
예제 #4
0
 def add(self, item):
     node = Node(item)
     LinkedList.add(self, node)