Created
October 8, 2020 16:32
-
-
Save codecakes/3b1bbb187ddf99e3d180081394770aac to your computer and use it in GitHub Desktop.
Sort Stack in ascdending order as more stacks increase by value, with Lowest value at top
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Lowest value at top, highest on top.""" | |
from Stack import MyStack | |
def sort_stack(stack): | |
if not stack.is_empty(): | |
top_val = stack.pop() | |
sort_stack(stack) | |
_insert(stack, top_val) | |
return stack | |
def _insert(stack, top_val): | |
if not stack.is_empty() and top_val > stack.top(): | |
temp = stack.pop() | |
_insert(stack, top_val) | |
top_val = temp | |
stack.push(top_val) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment