示例#1
0
def test_peek_general(items: List[int]) -> None:
    """Test that peek works for a large range of stack sizes."""
    stack = Stack()
    for item in items:
        stack.push(item)
    assert peek(stack) == items[-1]
    assert stack.pop() == items[-1]
示例#2
0
def test_peek_doctest() -> None:
    """This is the doctest given in peek."""
    stack = Stack()
    stack.push(1)
    stack.push(2)
    assert peek(stack) == 2
    assert stack.pop() == 2
示例#3
0
def test_reverse_top_two_doctest2() -> None:
    """This is not the doctest given in reverse_top_two."""
    stack = Stack()
    stack.push(1)
    stack.push(2)
    stack.push(4)
    reverse_top_two(stack)
    assert stack.pop() == 1
    assert stack.pop() == 2
    assert stack.pop() == 4
    assert stack.is_empty()
示例#4
0
    def __init__(self) -> None:
        """
        Initialize this StackQueue.

        >>> q = StackQueue()
        >>> q.is_empty()
        True
        """
        # Create 2 stacks as attributes
        # self._add_to: This will be the stack that we always add to when
        #               add() is called.
        # self._remove_from: This will be the stack that we use to remove from
        #               when remove() is called.
        self._add_to = Stack()
        self._remove_from = Stack()
示例#5
0
def test_add_in_order() -> None:
    """Test is the doctest given in add_in_order."""
    stack = Stack()
    lst = [1, 2]
    add_in_order(stack, lst)
    results = [stack.pop(), stack.pop()]
    assert lst == results
    assert stack.is_empty()
    stack = Stack()
    lst = [1]
    add_in_order(stack, lst)
    results = [stack.pop()]
    assert lst == results
    assert stack.is_empty()
示例#6
0
def add_in_order(stack: Stack, lst: list) -> None:
    """
    Add all items in <lst> to <stack>, so that when items are removed from
    <stack>, they are returned in <lst> order.

    Precondition: stack.is_empty() is True

    >>> stack = Stack()
    >>> lst = [1, 2]
    >>> add_in_order(stack, lst)
    >>> results = [stack.pop(), stack.pop()]
    >>> lst == results
    True
    >>> stack.is_empty()
    True
    """
    for item in reversed(lst):
        stack.push(item)
示例#7
0
def reverse_top_two(stack: Stack) -> None:
    """Reverse the top two elements on <stack>.

    Precondition: <stack> has at least two items.

    >>> stack = Stack()
    >>> stack.push(1)
    >>> stack.push(2)
    >>> reverse_top_two(stack)
    >>> stack.pop()
    1
    >>> stack.pop()
    2
    >>> stack.is_empty()
    True
    """
    a = stack.pop()
    b = stack.pop()
    stack.push(a)
    stack.push(b)
示例#8
0
def peek(stack: Stack) -> Optional[Any]:
    """Return the top item on the given stack.

    If the stack is empty, return None.

    Unlike Stack.pop, this function should leave the stack unchanged when the
    function ends. You can (and should) still call pop and push, just make
    sure that if you take any items off the stack, you put them back on!

    >>> stack = Stack()
    >>> stack.push(1)
    >>> stack.push(2)
    >>> peek(stack)
    2
    >>> stack.pop()
    2
    """
    a = stack.pop()
    stack.push(a)
    return a
示例#9
0
def check_params(lines):
    parenthesis_stack = Stack()
    for idx, line in enumerate(lines):
        for cidx, character in enumerate(line):
            if character == '(':
                parenthesis_stack.push(character)
            elif character == ')' and not parenthesis_stack.is_empty():
                parenthesis_stack.pop()
            elif character == ')' and parenthesis_stack.is_empty():
                print(
                    f'You have are closing a parenthesis without opening one on line {idx} on position {cidx}'
                )
                return False
    if parenthesis_stack.is_empty():
        return True
    else:
        return False
示例#10
0
def reverse_top_two(stack: Stack) -> None:
    """Reverse the top two elements on <stack>.

    Precondition: <stack> has at least two items.

    >>> stack = Stack()
    >>> stack.push(1)
    >>> stack.push(2)
    >>> reverse_top_two(stack)
    >>> stack.pop()
    1
    >>> stack.pop()
    2
    >>> stack.is_empty()
    True
    """
    popped = list()
    for x in range(0, 2):
        if not stack.is_empty():
            popped.append(stack.pop())
    for x in popped:
        stack.push(x)
示例#11
0
def remove_all_but_one(queue: Queue) -> None:
    """Remove all items from the given queue except the last one.

    Precondition: <queue> contains at least one item.
                  or: not queue.is_empty()

    >>> queue = Queue()
    >>> queue.enqueue(1)
    >>> queue.enqueue(2)
    >>> queue.enqueue(3)
    >>> remove_all_but_one(queue)
    >>> queue.is_empty()
    False
    >>> queue.dequeue()
    3
    >>> queue.is_empty()
    True
    """
    temp = Stack()
    while not queue.is_empty():
        temp.push(queue.dequeue())
    queue.enqueue(temp.pop())
示例#12
0
文件: prep4.py 项目: Jimmyzz2/csc148
def peek(stack: Stack) -> Optional[Any]:
    """Return the top item on the given stack.

    If the stack is empty, return None.

    Unlike Stack.pop, this function should leave the stack unchanged when the
    function ends. You can (and should) still call pop and push, just make
    sure that if you take any items off the stack, you put them back on!
    Need to consider the case when stack is empty

    >>> stack = Stack()
    >>> stack.push(1)
    >>> stack.push(2)
    >>> peek(stack)
    2
    >>> stack.pop()
    2
    """
    if stack.is_empty():
        return None
    peek_result = stack.pop()
    stack.push(peek_result)
    return peek_result
示例#13
0
def reverse_top_two(stack: Stack) -> None:
    """Reverse the top two elements on <stack>.

    Precondition: <stack> has at least two items.

    >>> stack = Stack()
    >>> stack.push(1)
    >>> stack.push(2)
    >>> reverse_top_two(stack)
    >>> stack.pop()
    1
    >>> stack.pop()
    2
    >>> stack.is_empty()
    True
    """

    if stack.is_empty():
        pass
    else:
        firstvar = stack.pop()
        secondvar = stack.pop()
        stack.push(firstvar)
        stack.push(secondvar)
示例#14
0
 def __init__(self, length: int = DEFAULT_SIZE) -> None:
     """
     complexity: O(n) where n is the length
     """
     Stack.__init__(self)
     self.array = [None] * length
示例#15
0
文件: prep4.py 项目: Jimmyzz2/csc148
def reverse_top_two(stack: Stack) -> None:
    """Reverse the top two elements on <stack>.

    Precondition: <stack> has at least two items.

    >>> stack = Stack()
    >>> stack.push(1)
    >>> stack.push(2)
    >>> reverse_top_two(stack)
    >>> stack.pop()
    1
    >>> stack.pop()
    2
    >>> stack.is_empty()
    True
    """
    stack_help = Stack()
    last_item = stack.pop()
    while not stack.is_empty():
        stack_help.push(stack.pop())
    first_item = stack_help.pop()
    stack.push(last_item)
    while not stack_help.is_empty():
        stack.push(stack_help.pop())
    stack.push(first_item)
示例#16
0
class StackQueue(Container):
    """
    An implementation of the class Queue but using Stacks to store the
    content.
    """
    def __init__(self) -> None:
        """
        Initialize this StackQueue.

        >>> q = StackQueue()
        >>> q.is_empty()
        True
        """
        # Create 2 stacks as attributes
        # self._add_to: This will be the stack that we always add to when
        #               add() is called.
        # self._remove_from: This will be the stack that we use to remove from
        #               when remove() is called.
        self._add_to = Stack()
        self._remove_from = Stack()

    def add(self, value: object) -> None:
        """
        Add value to this StackQueue.

        >>> q = StackQueue()
        >>> q.add(5)
        >>> q.add(3)
        >>> q.remove()
        5
        >>> q.remove()
        3
        """
        # Simply add to self._add_to for add()
        self._add_to.add(value)

    def remove(self) -> object:
        """
        Remove the item at the front of this StackQueue.

        >>> q = StackQueue()
        >>> q.add(5)
        >>> q.add(3)
        >>> q.remove()
        5
        >>> q.remove()
        3
        """
        # Empty out self._add_to and put all of the items into self._remove_from
        # In this case, you'll be adding the items in reverse order.
        # If self._add_to looked like Top -> 4, 3, 2, 1
        # Then removing from it would give Top -> 3, 2, 1
        # Adding that to self._remove_from would make it Top -> 4
        # Removing the next item from self._add_to would make it Top -> 2, 1
        # And adding that to self._remove_from would make it Top -> 3, 4
        # So by the time self._add_to is empty, self._remove will have the
        # items in reverse order.

        # Empty our self._add_to
        while not self._add_to.is_empty():
            # Add the item removed into self._remove_from
            self._remove_from.add(self._add_to.remove())

        # We remove from self._remove_from to get the 'first item' in our Queue
        return_value = self._remove_from.remove()

        # And then we put self._add_to back in its original order sans that
        # first item (e.g. empty our self._remove_from into self._add_to)

        # Empty our self._remove_from
        while not self._add_to.is_empty():
            # Add the item removed into self._add_to
            self._add_to.add(self._remove_from.remove())

        return return_value

    def is_empty(self) -> bool:
        """
        Return whether this StackQueue is empty or not.

        >>> q = StackQueue()
        >>> q.is_empty()
        True
        >>> q.add(5)
        >>> q.is_empty()
        False
        """
        # Since we always add to self._add_to, we just return whether that's
        # empty or not
        return self._add_to.is_empty()
示例#17
0
    False
    >>> queue.dequeue()
    3
    >>> queue.is_empty()
    True
    """
    temp = Stack()
    while not queue.is_empty():
        temp.push(queue.dequeue())
    queue.enqueue(temp.pop())


if __name__ == '__main__':
    # import doctest
    # doctest.testmod()

    # Remember, to get this to work you need to Run this file, not just the
    # doctests in this file!
    # import python_ta
    # python_ta.check_all(config={
    #     'extra-imports': ['adts']
    # })
    stack = Stack()
    stack.push(1)
    stack.push(2)
    stack.push(3)
    stack.push(4)
    reverse_top_two(stack)
    print(stack.fuscat1)

示例#18
0
def peek(stack: Stack) -> Optional[Any]:
    """Return the top item on the given stack.

    If the stack is empty, return None.

    Unlike Stack.pop, this function should leave the stack unchanged when the
    function ends. You can (and should) still call pop and push, just make
    sure that if you take any items off the stack, you put them back on!

    >>> stack = Stack()
    >>> stack.push(1)
    >>> stack.push(2)
    >>> peek(stack)
    2
    >>> stack.pop()
    2
    """
    temp1 = Stack()
    temp2 = Stack()
    if stack.is_empty():
        return None
    while not stack.is_empty():
        temp1.push(stack.pop())
    while not temp1.is_empty():
        value = temp1.pop()
        stack.push(value)
        temp2.push(value)
    return temp2.pop()
示例#19
0
def reverse_top_two(stack: Stack) -> None:
    """Reverse the top two elements on <stack>.

    Precondition: <stack> has at least two items.

    >>> stack = Stack()
    >>> stack.push(1)
    >>> stack.push(2)
    >>> reverse_top_two(stack)
    >>> stack.pop()
    1
    >>> stack.pop()
    2
    >>> stack.is_empty()
    True
    """
    # This is wrong
    temp1 = Stack()
    temp2 = Stack()
    temp1.push(stack.pop())
    temp1.push(stack.pop())
    temp2.push(temp1.pop())
    temp2.push(temp1.pop())
    stack.push(temp2.pop())
    stack.push(temp2.pop())