Example #1
0
def graham(points: List) -> List:
    """
    Graham's scan is a method of finding the convex hull of a finite
    set of points in the plane with time complexity O(n log n).
    It is named after Ronald Graham, who published the original
    algorithm in 1972.The algorithm finds all vertices of the
    convex hull ordered along its boundary.
    """

    # Find the bottom left point and remove it from the set
    bottom_left_point = min(points, key=lambda x: (x[1], x[0]))

    # Put the points on a min heap by polar angle
    polar_points = [(get_polar_angle(bottom_left_point, x), x) for x in points if x != bottom_left_point]
    point_heap = BinomialHeap(polar_points)

    # Push the first 3 elements on a stack
    stack = Stack()
    stack.push(bottom_left_point)
    stack.push(point_heap.pop()[1])
    stack.push(point_heap.pop()[1])

    for _ in range(len(point_heap)):
        _, p3 = point_heap.pop()
        while is_counterclockwise(stack.peek(1), stack.peek(), p3) < 0:
            stack.pop()
        stack.push(p3)

    return stack.to_array()
Example #2
0
def graham(points: List) -> List:
    """
    Graham's scan is a method of finding the convex hull of a finite
    set of points in the plane with time complexity O(n log n).
    It is named after Ronald Graham, who published the original
    algorithm in 1972.The algorithm finds all vertices of the
    convex hull ordered along its boundary.
    """

    # Find the bottom left point and remove it from the set
    bottom_left_point = min(points, key=lambda x: (x[1], x[0]))

    # Put the points on a min heap by polar angle
    polar_points = [(get_polar_angle(bottom_left_point, x), x) for x in points
                    if x != bottom_left_point]
    point_heap = BinomialHeap(polar_points)

    # Push the first 3 elements on a stack
    stack = Stack()
    stack.push(bottom_left_point)
    stack.push(point_heap.pop()[1])
    stack.push(point_heap.pop()[1])

    for _ in range(len(point_heap)):
        _, p3 = point_heap.pop()
        while is_counterclockwise(stack.peek(1), stack.peek(), p3) < 0:
            stack.pop()
        stack.push(p3)

    return stack.to_array()
Example #3
0
class CustomStack(Stack):

    def __init__(self):
        super().__init__()

        self.min_stack = Stack()

    def push(self, value):

        # Keep track of the min value
        if self.min_stack.is_empty() or self.min_stack.peek() >= value:
            self.min_stack.push(value)

        super().push(value)

    def pop(self):

        value = super().pop()

        if value == self.min_stack.peek():
            self.min_stack.pop()

        return value

    def get_minimum(self):
        if not self.min_stack.is_empty():
            return self.min_stack.peek()
Example #4
0
class CustomStack(Stack):
    def __init__(self):
        super().__init__()

        self.min_stack = Stack()

    def push(self, value):

        # Keep track of the min value
        if self.min_stack.is_empty() or self.min_stack.peek() >= value:
            self.min_stack.push(value)

        super().push(value)

    def pop(self):

        value = super().pop()

        if value == self.min_stack.peek():
            self.min_stack.pop()

        return value

    def get_minimum(self):
        if not self.min_stack.is_empty():
            return self.min_stack.peek()
Example #5
0
def infix_to_postfix(expression):
    operator_stack = Stack()

    precedence_rules = {'/': 3, '*': 3, '+': 2, '-': 2, '%': 1, '(': 1}

    output_list = []

    for char in expression:

        if char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789':
            output_list.append(char)

        elif char == '(':
            operator_stack.push('(')

        elif char == ')':
            current_char = operator_stack.pop()
            while current_char != '(':
                output_list.append(current_char)
                current_char = operator_stack.pop()

        elif char == ' ':
            pass

        elif char in precedence_rules.keys():
            while len(operator_stack) > 0 and precedence_rules[
                    operator_stack.peek()] >= precedence_rules[char]:
                output_list.append(operator_stack.pop())

            operator_stack.push(char)

    while len(operator_stack):
        output_list.append(operator_stack.pop())

    return ' '.join(output_list)
Example #6
0
    def test_stack(self):
        stack = Stack()

        stack.push(5)

        assert len(stack) == 1

        stack.push(2)

        assert len(stack) == 2

        assert stack.peek() == 2

        assert stack.pop() == 2

        assert stack.pop() == 5

        assert_raises(IndexError, stack.pop)
Example #7
0
    def test_stack(self):
        stack = Stack()

        stack.push(5)

        assert len(stack) == 1

        stack.push(2)

        assert len(stack) == 2

        assert stack.peek() == 2

        assert stack.pop() == 2

        assert stack.pop() == 5

        assert_raises(IndexError, stack.pop)
Example #8
0
def sort_stack(stack: Stack):

    extra_stack = Stack()

    while not stack.is_empty():

        element = stack.pop()

        elements_transfered = 0
        while not extra_stack.is_empty() and extra_stack.peek() > element:
            elements_transfered += 1
            stack.push(extra_stack.pop())

        extra_stack.push(element)

        for _ in range(elements_transfered):
            extra_stack.push(stack.pop())

    while not extra_stack.is_empty():
        stack.push(extra_stack.pop())
Example #9
0
def infix_to_postfix(expression):
    operator_stack = Stack()

    precedence_rules = {
        '/': 3,
        '*': 3,
        '+': 2,
        '-': 2,
        '%': 1,
        '(': 1
    }

    output_list = []

    for char in expression:

        if char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789':
            output_list.append(char)

        elif char == '(':
            operator_stack.push('(')

        elif char == ')':
            current_char = operator_stack.pop()
            while current_char != '(':
                output_list.append(current_char)
                current_char = operator_stack.pop()

        elif char == ' ':
            pass

        elif char in precedence_rules.keys():
            while len(operator_stack) > 0 and precedence_rules[operator_stack.peek()] >= precedence_rules[char]:
                output_list.append(operator_stack.pop())

            operator_stack.push(char)

    while len(operator_stack):
        output_list.append(operator_stack.pop())

    return ' '.join(output_list)