donnemartin / donnemartin/interactive-coding-challenges

sort_stack pseudocode and test cases text is incorrect

Open
#264 0 comments 0 reactions 0 assignees View on GitHub
needs-review
Dominant language
Python
Stars
31.8k
Forks
4.7k
PR merge metrics
No merged PRs in 30d

Description

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

```py
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:

```py
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've submitted pull request #263 for this issue.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.