design stacks using array solution - #2439
Conversation
super30admin
commented
Sep 3, 2026
The student has implemented a basic stack using an array (Python list) with the standard operations: Strengths:
Issues and Areas for Improvement:
Suggested Refactor: classmyStack:
def__init__(self):
self.stack= []
defisEmpty(self):
returnlen(self.stack) ==0defpush(self, item):
self.stack.append(item)
defsize(self):
returnlen(self.stack)
defpop(self):
ifself.isEmpty():
raiseIndexError("pop from empty stack")
returnself.stack.pop()
defpeek(self):
ifself.isEmpty():
raiseIndexError("peek from empty stack")
returnself.stack[-1]
defshow(self):
print(self.stack)This refactor is cleaner, more Pythonic, and avoids the pitfalls of manual index tracking. |
precoruse 1 problem 1