minimanimoh / Udacity_DS-A

0 stars 0 forks source link

DS&A - 2. Data Structures - Lesson 2. Reverse a Stack #18

Open minimanimoh opened 3 years ago

minimanimoh commented 3 years ago
def reverse_stack(stack):
    """
    Reverse a given input stack

    Args:
       stack(stack): Input stack to be reversed
    Returns:
       stack: Reversed Stack
    """

    # TODO: Write the reverse stack function
    temp = Stack()
    while not stack.is_empty():
        temp.push(stack.pop()) # pop the elements of the stack
    return temp
minimanimoh commented 3 years ago
def reverse_stack(stack):
    holder_stack = Stack()
    while not stack.is_empty():
        popped_element = stack.pop()
        holder_stack.push(popped_element)
    _reverse_stack_recursion(stack, holder_stack)

def _reverse_stack_recursion(stack, holder_stack):
    if holder_stack.is_empty():
        return
    popped_element = holder_stack.pop()
    _reverse_stack_recursion(stack, holder_stack)
    stack.push(popped_element)
minimanimoh commented 3 years ago

" _reverse_stack_recursion(stack, holder_stack)" ?? what is its role in both code defs?

minimanimoh commented 3 years ago

why do i need 'def _reverse_stack_recursion(stack, holder_stack):'?