Skip to content

Conversation

@amirsoroush
Copy link

The current implementation of pop() in "Chapter05/Stack_using_array.py" file is not correct:

def pop():
  global top
  if top == -1:
    print("Stack Underflow")
  else:
    top = top-1
    data[top] = 0
    return data[top+1]

It first decreases the top and then sets its value to 0 !

What we want it this:

def pop():
    global top
    if top == -1:
        print("Stack Underflow")
    else:
        value = data[top]
        data[top] = 0
        top = top - 1
        return value

We need to first store the value somewhere then set the current position to 0 and then decrease the top and finally return the stored value.

I also applied "Black" formatting to the whole file as it's currently hard to read with 2 spaces indentation.



def peek():
global top
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we do not assign to the top, there is no need for global keyword here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Why there are so many errors in this book?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to contact the author for reviewing the PRs but he didn't respond to my email.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants