Пример #1
0
def add_linked_lists_reversed(l1, l2):
    """
    Adds two numbers stored in a linked list in reverse order
    Args:
        l1(LinkedList)
        l2(LinkedList)
    Returns: Result as a linked list
    """
    if l1.size() < l2.size():
        l1 = add_zeroes(l1, l2.size() - l1.size())
    elif l2.size() < l1.size():
        l2 = add_zeroes(l2, l1.size() - l2.size())
    ll = LinkedList()
    carry = 0
    t1 = l1.head
    t2 = l2.head
    while t1 is not None or t2 is not None:
        sum = 0
        sum += carry
        sum += t1.value
        sum += t2.value
        carry = sum // 10
        ll.append(Element(sum % 10))
        t1 = t1.next
        t2 = t2.next
    return ll.head
class AnimalQueue(object):
    """
    A Queue to store animals(cats and dogs)
    Attributes:
        dogs(LinkedList): A linked list to store dogs
        cats(LinkedList): A linked list to store cats
        order(Integer): Time when an animal is added to the queue
    """
    def __init__(self):
        self.dogs = LinkedList()
        self.cats = LinkedList()
        self.order = 0

    def enqueue(self, a):
        """
        Enqueues an animal to the queue
        Args:
            a(Animal): Animal to add to the queue
        """
        a.set_order(self.order)
        self.order += 1
        if type(a).__name__ == 'Dog':
            self.dogs.append(Element(a))
        elif type(a).__name__ == 'Cat':
            self.cats.append(Element(a))

    def dequeue(self):
        """
        Removes an animal from the queue
        Returns:
            Animal: Returned animal
        """
        if self.dogs.size() == 0:
            return self.dequeue_cats()
        elif self.cats.size() == 0:
            return self.dequeue_dogs()

        if self.dogs.peek().value.is_older_than(self.cats.peek().value):
            return self.dequeue_dogs()
        else:
            return self.dequeue_cats()

    def dequeue_dogs(self):
        return self.dogs.remove_head()

    def dequeue_cats(self):
        return self.cats.remove_head()
def check_palindrome(ll):
    """
    Check if a Linked List is a Palindrome
    Args:
         ll(LinkedList): The linked list to check
    Returns:
        Boolean: Indicates if linked list is a palindrome or not
    """
    t1 = ll
    t2 = copy.deepcopy(ll)
    LinkedList.reverse(t2)
    nxt1 = t1.head
    nxt2 = t2.head
    for i in range(0, (1 + ll.size() // 2)):
        if nxt1.value == nxt2.value:
            nxt1 = nxt1.next
            nxt2 = nxt2.next
        else:
            return False
    return True
Пример #4
0
def add_linked_lists(l1, l2):
    """
    Adds two numbers stored in a linked list
    Args:
        l1(LinkedList)
        l2(LinkedList)
    Returns:
        LinkedList: Result as a linked list
    """
    if l1.size() < l2.size():
        l1 = add_zeroes(l1, l2.size() - l1.size(), True)
    elif l2.size() < l1.size():
        l2 = add_zeroes(l2, l1.size() - l2.size(), True)
    ll = LinkedList()
    carry = 0
    t1 = l1.head
    t2 = l2.head
    res = add_lists_helper(t1, t2)
    return res.ll.head
         ll(LinkedList): The linked list to check
    Returns:
        Boolean: Indicates if linked list is a palindrome or not
    """
    t1 = ll
    t2 = copy.deepcopy(ll)
    LinkedList.reverse(t2)
    nxt1 = t1.head
    nxt2 = t2.head
    for i in range(0, (1 + ll.size() // 2)):
        if nxt1.value == nxt2.value:
            nxt1 = nxt1.next
            nxt2 = nxt2.next
        else:
            return False
    return True


e1 = Element(0)
e2 = Element(1)
e3 = Element(2)
e4 = Element(1)
e5 = Element(0)

ll = LinkedList(e1)
ll.append(e2)
ll.append(e3)
ll.append(e4)
ll.append(e5)

print(check_palindrome(ll))

e1 = Element(1)
e2 = Element(2)
e3 = Element(3)
e4 = Element(4)
e5 = Element(5)
e6 = Element(6)
e7 = Element(7)
e8 = Element(8)
e9 = Element(9)
e10 = Element(10)
e11 = Element(11)
e12 = Element(12)

ll = LinkedList(e1)
ll.append(e2)
ll.append(e3)
ll.append(e4)
ll.append(e5)
ll.append(e6)
ll.append(e7)
ll.append(e8)
ll.append(e9)
ll.append(e10)
ll.append(e11)
ll.append(e12)

delete_node(ll.head.next.next)
delete_node(ll.head.next.next.next)
print(ll.size())
Пример #7
0
        fast = fast.next

    return fast.value


e1 = Element(1)
e2 = Element(2)
e3 = Element(3)
e4 = Element(4)
e5 = Element(5)
e6 = Element(6)
e7 = Element(7)
e8 = Element(8)
e9 = Element(9)
e10 = Element(10)
e11 = Element(11)
e11.next = e4

ll = LinkedList(e1)
ll.append(e2)
ll.append(e3)
ll.append(e4)
ll.append(e5)
ll.append(e6)
ll.append(e7)
ll.append(e8)
ll.append(e9)
ll.append(e10)
ll.append(e11)

print(find_beginning(ll.head))
Пример #8
0
    elif length == 1:
        return Result(head.next, True)
    elif length == 2:
        return Result(head.next.next, head.value == head.next.value)
    res = check_palindrome(head.next, length - 2)
    if not res.result or res.node is None:
        return res
    else:
        res.result = (head.value == res.node.value)
        res.node = res.node.next
    return res


e1 = Element(0)
e2 = Element(1)
e3 = Element(2)
e7 = Element(3)
e8 = Element(2)
e4 = Element(1)
e5 = Element(0)

ll = LinkedList(e1)
ll.append(e2)
ll.append(e3)
ll.append(e7)
ll.append(e8)
ll.append(e4)
ll.append(e5)

p = check_palindrome(ll.head, ll.size())
print(p.result)
 def __init__(self):
     self.dogs = LinkedList()
     self.cats = LinkedList()
     self.order = 0
Пример #10
0
 def __init__(self, node=None, carry=0):
     self.node = node
     self.carry = carry
     self.ll = LinkedList()
Пример #11
0
            ll.insert(Element(0), 1)
    else:
        for i in range(0, diff):
            ll.append(Element(0))
    return ll


# Addition of 167 and 53 = 220
e1 = Element(7)
e2 = Element(6)
e3 = Element(1)

r1 = Element(5)
r2 = Element(9)

l1 = LinkedList(e1)
l1.append(e2)
l1.append(e3)

l2 = LinkedList(r1)
l2.append(r2)

# rev_result = add_linked_lists_reversed(l1, l2)
#
# while rev_result is not None:
#     print(rev_result.value, '-> ', end='')
#     rev_result = rev_result.next
# print(None)

result = add_linked_lists(l1, l2)