I think there are some mistakes in sort_stack's pseudocode and test cases description.
The test cases section says:
Empty stack -> None
But it should be returning an empty stack, not None.
Under the algorithm section, it says
" * While buffer is not empty or buffer top is > than temp\n",
but the code says
while not buff.is_empty() and temp < buff.peek():
so that should be an and, not an or.
It also says
"* Our buffer will hold elements in reverse sorted order, smallest at the top\n",
but the buffer stores elements in sorted order, largest at the top. Otherwise, how could we return buffer as our answer when the output should have the largest element at the top?
I also suggest changing
"* Store the current top element in a temp variable\n",
to
" * Pop the current top element of stack into a temp variable\n",
This clarifies that the top element is not just copied to a temp variable, but rather popped off into the temp variable. Also this happens within the outer while loop, not before.
Here's the code version of the solution for reference:
class MyStackSimplified(Stack):
def sort(self):
buff = MyStack()
while not self.is_empty():
temp = self.pop()
while not buff.is_empty() and temp < buff.peek():
self.push(buff.pop())
buff.push(temp)
return buff
I think there are some mistakes in sort_stack's pseudocode and test cases description.
The test cases section says:
But it should be returning an empty stack, not None.
Under the algorithm section, it says
but the code says
so that should be an and, not an or.
It also says
but the buffer stores elements in sorted order, largest at the top. Otherwise, how could we return buffer as our answer when the output should have the largest element at the top?
I also suggest changing
to
This clarifies that the top element is not just copied to a temp variable, but rather popped off into the temp variable. Also this happens within the outer while loop, not before.
Here's the code version of the solution for reference:
I've submitted pull request #263 for this issue.