Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort_stack pseudocode and test cases text is incorrect #264

Open
ML-Chen opened this issue Oct 19, 2019 · 0 comments
Open

sort_stack pseudocode and test cases text is incorrect #264

ML-Chen opened this issue Oct 19, 2019 · 0 comments

Comments

@ML-Chen
Copy link

ML-Chen commented Oct 19, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants