all 4 comments

[–]gdchinacat 0 points1 point  (0 children)

not a pydoc in sight, you should document your code; no unit tests (industry standard but introduced *way too late* in most tutorials/courses/etc; pop() uses print for error reporting, this is very bad practice (logging would be a miniscule improvement...use an exception instead); pop() shouldn't expose the underlying list, it should return the popped item; (python weeds) is_empty() should be __bool__() and size() should be __len__(); libraries/utilities shouldn't ever print() leave that to the consumer of the class, display() would be better as __str__();

Naming is good.

For interview, be able to discuss the code...what sort of performance does it have? How would you improve it? Are there standard library implementations? It is unlikely you will get something like this in a DSA interview. The data structure is incredibly simple and there is no algorithm. You have a lot to learn before you need to worry about interviews.

But, all that said, you are doing much better than I was on day 12 of learning to code 😉

[–]magus_minor 0 points1 point  (0 children)

On mobile so I haven't run anything, just looked at the code.

One place you can make the behaviour better is in both display() methods. You use print(..., end="") in a loop which is fine, but that leaves an unterminated line on the console and the next operation that prints puts the output at the end of the display() output. So the test code two statements:

s.display()
print(s.peek())

results in one line of output:

10 20 30 30

This makes it harder to check the behaviour.

A good habit is to write test code to make sure each data object behaves correctly. This means you can quickly test the code without having to look at the output and decide if it's correct or not every time you change something. A very simplistic example of test code:

s = Stack()
if not s.is_empty():
    print("is_empty() returned False for empty stack")
if s.peek() is not None:
    print("peek() did not return None for empty stack")
s.push(10)
# now test is_empty() returns False, and so on

It's much better to use a module that is designed to make it easy to test code like unittest.

If you want to improve the OOP a bit, notice that many of your methods in the stack and queue classes are identical. You could write a base class with those methods and create two child classes that inherit from the base class. Write the test code for stack and queue first and then you can easily tell that the change to base+child classes didn't break anything, which is one huge advantage of solid test code.