コード例 #1
0
 def test_remove_followup(self):
     for dp, rm in self.data:
         dpl, rml = LL(), LL()
         dpl.add_multiple(list(dp))
         rml.add_multiple(list(rm))
         remove_dups_followup(dpl)
     self.assertEqual(str(dpl), str(rml))
コード例 #2
0
def test_len():
    ll = LL()
    assert len(ll) == 0
    ll.append('brekky')
    assert len(ll) == 1
    ll.append('beer')
    assert len(ll) == 2
    ll.append('crush your enemies')
    ll.append('don\'t let your dreams be dreams')
    assert len(ll) == 4
    print(ll)
コード例 #3
0
ファイル: 5_Sum_Lists.py プロジェクト: abtkod/practice
def sum_forward(ll1, ll2):
    output = LL()
    if len(ll1) < len(ll2):
        for i in range(len(ll2) - len(ll1)):
            ll1.add_to_beginning(0)
    if len(ll2) < len(ll1):
        for i in range(len(ll1) - len(ll2)):
            ll2.add_to_beginning(0)

    c = sum_digit(ll1.head, ll2.head, output)
    if c > 0:
        output.add_to_beginning(c)
    return output
コード例 #4
0
ファイル: 5_Sum_Lists.py プロジェクト: abtkod/practice
def sum_backward(ll1, ll2):
    result = LL()
    current1 = ll1.head
    current2 = ll2.head
    c = 0
    while current1 and current2:
        added_value = (current1.value + current2.value + c) % 10
        result.add(added_value)
        c = (current1.value + current2.value + c) // 10
        current1, current2 = current1.next, current2.next
    if current1 is not None:
        while current1:
            result.add((current1.value + c) % 10)
            c = (current1.value + c) // 10
            current1 = current1.next
    elif current2 is not None:
        while current2:
            result.add((current1.value + c) % 10)
            c = (current1.value + c) // 10
            current2 = current2.next
    elif c != 0:
        result.add(c)

    return result
コード例 #5
0
ファイル: ll_sorting.py プロジェクト: willwile4/resources
def sort_me(l, which):
    if which == "bubble":
        return bubble(l)
    elif which == "insert":
        return insert(l)
    elif which == "select":
        return select(l)
    elif which == "merge":
        return merge(l)
    else:
        l.sort()
        return l



n = int(argv[1])
which = argv[2]
numbers = list(range(n+1))

random.shuffle(numbers)
ll = LL()
for number in numbers:
    ll.append(number)

print(ll)

sorted_nums = sort_me(ll, which)

print(ll)

コード例 #6
0
def test_create_LL():
    ll = LL()
コード例 #7
0
ファイル: Program.py プロジェクト: Jordaness/DataStructures
from BSTree import BinarySearchTree as BST
from LinkedList import LinkedList as LL

tree = BST()
tree.insert(50).insert(30).insert(20).insert(40).insert(10).insert(70).insert(
    60).insert(80).insert(90)
print(tree.preOrder.__doc__)
tree.preOrder()

linkedlist = LL()
linkedlist.append(12).append(20).append(3).append(7).append(19).append(
    35).append(1).append(44)
print(linkedlist)

linkedlist.reverse()
print(linkedlist)
コード例 #8
0
ファイル: 5_Sum_Lists.py プロジェクト: abtkod/practice
    c = sum_digit(current1.next, current2.next, output)
    c, s = (current1.value + current2.value +
            c) // 10, (current1.value + current2.value + c) % 10
    output.add_to_beginning(s)
    return c


def sum_forward(ll1, ll2):
    output = LL()
    if len(ll1) < len(ll2):
        for i in range(len(ll2) - len(ll1)):
            ll1.add_to_beginning(0)
    if len(ll2) < len(ll1):
        for i in range(len(ll1) - len(ll2)):
            ll2.add_to_beginning(0)

    c = sum_digit(ll1.head, ll2.head, output)
    if c > 0:
        output.add_to_beginning(c)
    return output


if __name__ == "__main__":
    ll1, ll2, result = LL(), LL(), LL()
    ll1.add_multiple([7, 1, 6])
    ll2.add_multiple([5, 9, 2])
    print(ll1)
    print(ll2)
    print("summing backward:", sum_backward(ll1, ll2))
    print("summing forward:", sum_forward(ll1, ll2))
コード例 #9
0
# https://www.geeksforgeeks.org/sum-of-two-linked-lists/4

from LinkedList import LinkedList as LL

first = LL()
second = LL()
first.push(3) 
first.push(6)
first.push(8)  
first.push(5) 
print("First Linked List:", end=" ")
first.printList() 
second.push(9)
second.push(4) 
second.push(8) 
second.push(2) 
second.push(4) 
second.push(8) 
print("\nSecond Linked List:", end=" ")
second.printList() 

def calculateSize(linkList):
    size = 0
    while(linkList is not None):
        size += 1
        linkList = linkList.next
    return size
firstSize = calculateSize(first.head)
secondSize = calculateSize(second.head)
diff = abs(firstSize - secondSize)
print("Linked List Size: ", firstSize, secondSize, diff)
コード例 #10
0
def main():
    l1 = LL(1, 2, 3, 4)
    l2 = LL(5, 6, 3, 4)
    print(getNodeInnterSect(l1, l2))