예제 #1
0
class Stack:
    def __init__(self):
        self.linked_list = LinkedList()

    def is_empty(self):
        return self.linked_list.tail is None

    def peek(self):
        if (self.is_empty()):
            return None
        return self.linked_list.tail.value

    def push(self, value):
        self.linked_list.append(value)

    def pop(self):
        removedTail = self.linked_list.delete_tail()
        return removedTail.value if removedTail else None

    def __call__(self):
        l = self.linked_list()
        return l[::-1]

    def __str__(self):
        return str(self.linked_list)
예제 #2
0
def run_linked_list_demo():
    print("\nRandomly generated linked list:")
    l_list = LinkedList()
    for i in range(10):
        num = randint(1, 100)
        print(f"Add {num}")
        l_list.add(num)
예제 #3
0
파일: Q4.py 프로젝트: fzhao99/interviews
def partition(ll, val):
    before_list = LinkedList()
    after_list = LinkedList()

    current = ll.head
    while current:
        if current.value < val:
            before_list.add(current.value)
        else:
            after_list.add(current.value)

    if before_list.tail is None:
        return after_list
    else:
        before_list.add(Node(None))
        before_list.tail.next = after_list.head
        return before_list
예제 #4
0
파일: Q3.py 프로젝트: fzhao99/interviews
def list_depths_depth_help(root, level, result):
    if root is None:
        return
    current = LinkedList()
    if result[level] is None:
        result[level] = current

    result[level].add(root)
    list_depths_depth(root.left, level + 1, result)
    list_depths_depth(root.right, level + 1, result)
예제 #5
0
def sum_list_fol(ll_a, ll_b):
    if len(ll_a) < len(ll_b):
        for i in range(len(ll_b) - len(ll_a)):
            ll_a.add_to_beginning(0)
    else:
        for i in range(len(ll_a) - len(ll_b)):
            ll_b.add_to_beginning(0)

    ll_a_run, ll_b_run = ll_a.head, ll_b.head
    result = 0
    while ll_a_run and ll_b_run:
        result = result * 10 + ll_a_run.value + ll_b_run.value

        ll_a_run = ll_a_run.next
        ll_b_run = ll_b_run.next

    ll = LinkedList()
    for i in str(result):
        ll.add(int(i))

    return ll
예제 #6
0
파일: Q6.py 프로젝트: fzhao99/interviews
class AnimalShelter:
    def __init__(self):
        self.cats = LinkedList()
        self.dogs = LinkedList()
        self.order = 0

    def dequeueAny(self):
        if len(self.cats) == 0:
            return self.dequeueDog()
        if len(self.dogs) == 0:
            return self.dequeueCat()

        cur_cat = self.cats.head.value
        cur_dog = self.dogs.head.value

        if cur_cat.is_earlier(cur_dog):
            self.cats.head = self.cats.head.next
            return cur_cat
        if cur_dog.is_earlier(cur_cat):
            self.dogs.head = self.dogs.head.next
            return cur_dog

    def dequeueDog(self):
        cur_dog = self.dogs.head.value
        self.dogs.head = self.dogs.head.next
        return cur_dog

    def dequeueCat(self):
        cur_cat = self.cats.head.value
        self.cats.head = self.cats.head.next
        return cur_cat

    def enqueue(self, animal):
        animal.set_order(self.order)
        self.order += 1

        if type(animal) is Dog:
            self.dogs.add(animal)
        if type(animal) is Cat:
            self.cats.add(animal)
예제 #7
0
class Queue:
    def __init__(self):
        self.linked_list = LinkedList()

    def enqueue(self, value):
        self.linked_list.append(value)

    def dequeue(self):
        removed_head = self.linked_list.delete_head()
        return removed_head

    def is_empty(self):
        return self.linked_list.tail is None

    def peek(self):
        if self.linked_list.head is None:
            return None
        return self.linked_list.head.value

    def __str__(self):
        s = self.linked_list()
        return "<--".join(s)
예제 #8
0
def sum_list_prov(ll_a, ll_b):
    ll_a_run, ll_b_run = ll_a.head, ll_b.head
    carry = 0
    ans = LinkedList()
    while ll_a_run or ll_b_run:
        result = carry
        if ll_a_run:
            result += ll_a_run.value
        if ll_b_run:
            result += ll_b_run.value
        ans.add(result % 10)
        carry = result // 10

        ll_a_run = ll_a_run.next
        ll_b_run = ll_b_run.next

    if carry:
        ans.add(1)
    return ans
예제 #9
0
파일: Q3.py 프로젝트: fzhao99/interviews
def list_depths_breadth(root):
    current = LinkedList()
    result = {}

    if root is not None:
        current.add(root)

    counter = 0

    while not current.is_empty():
        result[str(counter)] = current
        parents = current
        current = LinkedList()

        for node in parents:
            if node.left:
                current.add(node.left)
            if node.right:
                current.add(node.right)

        counter += 1
    return result
예제 #10
0
파일: Q1.py 프로젝트: fzhao99/interviews

def remove_dups_follow_up(ll):
    if ll.head is None:
        return

    current = ll.head

    while current:
        runner = current

        while runner.next:
            if runner.next.value == current.value:
                runner.next = runner.next.next
            else:
                runner = runner.next
        current = current.next

    return ll


ll = LinkedList()
ll.generate(30, 0, 9)
print(ll)
print(remove_dups(ll))

ll = LinkedList()
ll.generate(30, 0, 9)
print(ll)
print(remove_dups_follow_up(ll))
예제 #11
0
 def __init__(self):
     self.linked_list = LinkedList()
예제 #12
0
def sum_lists(d1, d2):
    answer = LinkedList()
    d1_runner = d1.head
    d2_runner = d2.head
    tens_from_prev = False
    while d1_runner and d2_runner:
        digit_sum = d1_runner.value + d2_runner.value
        if tens_from_prev:
            answer.add(Node(digit_sum % 10 + 1))
        else:
            answer.add(Node(digit_sum % 10))

        if digit_sum >= 10:
            tens_from_prev = True
        else:
            tens_from_prev = False

        d1_runner = d1_runner.next
        d2_runner = d2_runner.next

    if d1_runner is None and d2_runner is None:
        if tens_from_prev:
            answer.add(Node(1))
            return answer
        else:
            return answer
    elif d1_runner is None:
        while d2_runner:
            if tens_from_prev:
                answer.add(Node(d2_runner.value + 1))
            else:
                answer.add(Node(d2_runner.value))

            if d2_runner.value + 1 >= 10:
                tens_from_prev = True
            else:
                tens_from_prev = False

            d2_runner = d2_runner.next
        return answer
    elif d2_runner is None:
        while d1_runner:
            if tens_from_prev:
                answer.add(Node(d1_runner.value + 1))
            else:
                answer.add(Node(d1_runner.value))

            if d1_runner.value + 1 >= 10:
                tens_from_prev = True
            else:
                tens_from_prev = False
            d1_runner = d1_runner.next
        return answer
예제 #13
0
def sum_list_fol(ll_a, ll_b):
    if len(ll_a) < len(ll_b):
        for i in range(len(ll_b) - len(ll_a)):
            ll_a.add_to_beginning(0)
    else:
        for i in range(len(ll_a) - len(ll_b)):
            ll_b.add_to_beginning(0)

    ll_a_run, ll_b_run = ll_a.head, ll_b.head
    result = 0
    while ll_a_run and ll_b_run:
        result = result * 10 + ll_a_run.value + ll_b_run.value

        ll_a_run = ll_a_run.next
        ll_b_run = ll_b_run.next

    ll = LinkedList()
    for i in str(result):
        ll.add(int(i))

    return ll


ll1 = LinkedList()
ll1.generate(7, 0, 9)
print(ll1)
ll2 = LinkedList()
ll2.generate(5, 0, 9)
print(ll2)
print(sum_list_fol(ll2, ll1))
예제 #14
0
파일: Q2.py 프로젝트: fzhao99/interviews
import sys
sys.path.append("..")

from data_structures.LinkedList import LinkedList


def kth_to_last(ll, k):
    end_tracker = ll.head
    elem_tracker = ll.head

    for i in range(k):
        if end_tracker.next:
            end_tracker = end_tracker.next
        else:
            return None

    while end_tracker:
        end_tracker = end_tracker.next
        elem_tracker = elem_tracker.next

    return elem_tracker.value


ll = LinkedList()
ll.generate(10, 0, 99)
print(ll)
print(kth_to_last(ll, 3))
예제 #15
0
파일: Q6.py 프로젝트: fzhao99/interviews
 def __init__(self):
     self.cats = LinkedList()
     self.dogs = LinkedList()
     self.order = 0