예제 #1
0
class MyQueue:
    def __init__(self):
        self.front = Stack()
        self.end = Stack()

    def size(self):
        return self.front.size() + self.end.size()

    def enqueue(self, val):
        self.end.push(val)

    def dequeue(self):
        if self.front.is_empty() and not self.move():
            raise Exception("queue is empty")
        return self.front.pop()

    def peek(self):
        if self.front.is_empty() and not self.move():
            raise Exception("queue is empty")
        return self.front.peek()

    # time: O(n), however, the average is O(1)
    def move(self):
        moved = False
        while not self.end.is_empty():
            moved = True
            self.front.push(self.end.pop())
        return moved

    def print_q(self):
        print self.front
        print self.end
예제 #2
0
파일: chap_03.py 프로젝트: embatbr/ctci
    def version_2(test):
        (given, expected) = test

        original_stack = Stack()
        sorted_stack = Stack()

        for g in given:
            original_stack.push(g)

        # starts here

        num_eltos = original_stack.size

        for i in range(num_eltos):
            pivot = original_stack.pop()

            num_swaps = 0
            while (not sorted_stack.is_empty()) and (pivot < sorted_stack.peek()):
                original_stack.push(sorted_stack.pop())
                num_swaps += 1
            sorted_stack.push(pivot)

            for _ in range(num_swaps):
                sorted_stack.push(original_stack.pop())

        # sorts in reverse order (smallest on top)
        while not sorted_stack.is_empty():
            original_stack.push(sorted_stack.pop())

        given_str = ''.join(original_stack.to_array()) # doesn't count in the O(.) calculation
        return given_str == expected
예제 #3
0
def sort_stack(stack):

    s1 = stack
    s2 = Stack(s1.size)

    while(s1.is_empty() != True):
        temp = s1.pop()
        while(s2.is_empty() != True and s2.peek() > temp):
            s1.push(s2.pop())
        s2.push(temp)

    return s2
예제 #4
0
def sort_stack(stack):
    buf = Stack()
    while not stack.is_empty():
        cur = stack.pop()
        cnt = 0
        while not buf.is_empty() and cur < buf.peek():
            stack.push(buf.pop())
            cnt += 1
        buf.push(cur)
        for i in xrange(cnt):
            buf.push(stack.pop())
    while not buf.is_empty():
        stack.push(buf.pop())
예제 #5
0
def get_convex_hull_by_graham_scan(angle_sorted_list):
    temp_stack = Stack()
    temp_stack.push(angle_sorted_list[0])
    temp_stack.push(angle_sorted_list[1])
    for p in angle_sorted_list[2:] + angle_sorted_list[:1]:
        while True:
            top = temp_stack.peek()
            second_top = temp_stack.peek_peek()
            b = (p[0] - top[0], p[1] - top[1])
            a = (top[0] - second_top[0], top[1] - second_top[1])
            if a[0] * b[1] - a[1] * b[0] < 0:
                temp_stack.pop()
            else:
                break
        temp_stack.push(p)
    temp_stack.pop()
    return temp_stack.items
예제 #6
0
파일: chap_03.py 프로젝트: embatbr/ctci
    def version_1(test):
        (given, expected) = test

        original_stack = Stack()
        sorted_stack = Stack()

        for g in given:
            original_stack.push(g)

        # starts here

        while not original_stack.is_empty():
            pivot = original_stack.pop()

            while (not sorted_stack.is_empty()) and (pivot < sorted_stack.peek()):
                original_stack.push(sorted_stack.pop())
            sorted_stack.push(pivot)

        # sorts in reverse order (smallest on top)
        while not sorted_stack.is_empty():
            original_stack.push(sorted_stack.pop())

        given_str = ''.join(original_stack.to_array()) # doesn't count in the O(.) calculation
        return given_str == expected