Exemple #1
0
def main():
    input_stack = s.Stack(len(input_data), True)  # suppress_printing = True
    [input_stack.push(x) for x in input_data]

    expected_output_stack = s.Stack(len(expected_output_data),
                                    True)  # suppress_printing = True
    [expected_output_stack.push(x) for x in expected_output_data]

    print("Input: \n" + str(input_stack.prettify()))
    print("Expected: \n" + str(expected_output_stack.prettify()))
    print("Output: \n" + str(sort_stack_1(input_stack)))
def evaluate_postfix_rpn(expression_string):
    expression = list(expression_string)
    temp_stack = s.Stack(len(expression), True)  # suppress_printing = True
    valid_input = ["+", "-", "/", "*"]

    for c in expression:
        if c not in valid_input and not c.isdigit():
            return "Expression is invalid!"

        if c is "+":
            x = int(temp_stack.pop())
            y = int(temp_stack.pop())
            temp_stack.push(y + x)
        elif c is "-":
            x = int(temp_stack.pop())
            y = int(temp_stack.pop())
            temp_stack.push(y - x)
        elif c is "*":
            x = int(temp_stack.pop())
            y = int(temp_stack.pop())
            temp_stack.push(y * x)
        elif c is "/":
            x = int(temp_stack.pop())
            y = int(temp_stack.pop())
            temp_stack.push(y // x)
        else:
            temp_stack.push(c)

    result = temp_stack.pop()

    return result
def find_celebrity(party_data, num_people):
    stack = s.Stack(num_people, True)  # suppress_printing = True
    celebrity = -1

    for i in range(num_people):
        stack.push(i)

    while not stack.is_empty():
        x = stack.pop()

        # check if stack empty before popping second guest id
        if stack.is_empty():
            # if stack empty, previously popped guest id is the last id
            # and therefore we assume everyone knows them.
            celebrity = x
            break

        y = stack.pop()

        if party_data[x][y] is 1:
            stack.push(y)
        else:
            stack.push(x)

    for i in range(num_people):
        if celebrity is not i:
            if (party_data[celebrity][i] is
                    1) or (not party_data[i][celebrity] is 1):
                return -1

    return celebrity
def main():
    print("Stack initialized with capacity = 5")
    example = stack.Stack(5)

    print("------")
    print("pushing to stack...")
    example.push(5)
    example.push(6)
    example.push(7)
    example.push(8)
    example.push(9)
    example.push(2)
    example.push(7)

    print()
    example.print_stack()

    print("------")
    print("peeking at the top...")
    print(example.peek())

    print("------")
    print("popping from stack..")
    example.pop()
    example.pop()

    print()
    example.print_stack()

    print("------")
    print("peeking at the top...")
    print(example.peek())

    print("------")
    print("popping from stack..")
    example.pop()
    example.pop()
    example.pop()
    example.pop()
    example.pop()

    print()
    example.print_stack()

    print("------")
    print("peeking at the top...")
    print(example.peek())

    print("------")
    print("pushing to stack...")
    example.push(21)
    example.push(22)

    print()
    example.print_stack()

    print("------")
    print("peeking at the top...")
    print(example.peek())
def reverse_k_elements(queue, k):
    temp_stack = s.Stack(k, True) # suppress_printing = True

    for i in range(k):
        temp_stack.push(queue.dequeue())

    for i in range(k):
        queue.enqueue(temp_stack.pop())

    for i in range(queue.capacity - k):
        queue.enqueue(queue.dequeue())

    return queue.prettify()
Exemple #6
0
def sort_stack_1(stack):
    result = s.Stack(stack.capacity, True)  # suppress_printing = True

    while not stack.is_empty():
        value = stack.pop()
        if not result.is_empty() and value >= int(result.peek()):
            result.push(value)
        else:
            while not result.is_empty() and value < int(result.peek()):
                stack.push(result.pop())

            result.push(value)

    return result.prettify()
Exemple #7
0
def bfs_traversal(graph, root):
    result = ""
    visited = [False] * graph.num_vertices

    stack = s.Stack(graph.num_vertices, True)  # suppress_printing = True
    stack.push(root)

    while not stack.is_empty():
        g_node = stack.pop()
        result += str(g_node)

        dll = graph.adjacency_list[g_node]
        if dll is not None:
            current = dll.head
            while current:
                if not visited[current.value]:
                    stack.push(current.value)
                    visited[current.value] = True
                current = current.next

    return result
def next_greater_element_2(input_data):
    stack = s.Stack(len(input_data), True)  # suppress_printing = True
    result = []

    for x in input_data:
        if stack.is_empty():
            stack.push(x)
        else:
            if x < stack.peek():
                stack.push(x)
            else:
                while not stack.is_empty():
                    stack.pop()
                    result.append(x)

                stack.push(x)

    while not stack.is_empty():
        stack.pop()
        result.append(-1)

    return result
def check_balanced(expression):
    input_chars = list(expression)
    stack = s.Stack(len(input_chars), True)  # suppress_printing = True

    for char in input_chars:
        if char is "{" or char is "[" or char is "(":
            stack.push(char)
        else:
            if stack.is_empty():
                # if the string is unbalanced, number of opening parentheses do not match with the
                # closing parentheses, this will lead to stack running empty before the whole string
                # has been processed, so return False
                return False
            if char is "}" and stack.pop() is not "{":
                return False
            if char is "]" and stack.pop() is not "[":
                return False
            if char is ")" and stack.pop() is not "(":
                return False

    if not stack.is_empty():
        return False

    return True
 def __init__(self, capacity=None, suppress_printing=False):
     self.suppress_printing = suppress_printing
     self.capacity = capacity
     self.stack = s.Stack(capacity, suppress_printing)
     self.min_stack = s.Stack(capacity, suppress_printing)