24th Feb 2022, 5:40 PM
Ipang
+ 2
So imagine the stack being like a literal stack of objects in irl. The self.items is an ordered list of the items on the stack. So when you are calling the push function, the code is basically inserting the item at the front of the list (python implictly reindexes items that are already in the list). When the pop is called, it returns the item at the front of the list and removes it from the list, essentially making the previously pushed item (previous to the popped item) at the front of the list. So basically the stack can model e.g. a stack of documents, when you push onto the stack you put a document on top of it and when you pop from the stack you take the document on top off of the stack. That is why the stack is called a LIFO (last-in-first-out) data structure, since the last item pushed onto it is also the first, and only one that can be popped off the stack.
25th Feb 2022, 7:58 AM
Donato Paul Thayalan
Donato Paul Thayalan - avatar
+ 2
The other two functions aren't really necessary for the stack to work by itself but it helps you get data and information about it. The is_empty function just compares the stack's list with an empty one and return this comparison as an true-or-false value (if true, the stack is empty). The print_stack function just prints the stack from top to bottom.
25th Feb 2022, 8:10 AM
Donato Paul Thayalan
Donato Paul Thayalan - avatar
+ 2
What Ipang sent you is just class, which isn't a program by itself. You will have to initialize the stack and operate on it yourself. e.g. <class Stack definition> s = Stack() s.push(<any object>) ... ^ is how you should be using it (angular brackets are placeholders)
26th Feb 2022, 2:09 PM
Donato Paul Thayalan
Donato Paul Thayalan - avatar
+ 1
Is it possible for you to send the program text you have written/are trying to run?
26th Feb 2022, 1:41 PM
Donato Paul Thayalan
Donato Paul Thayalan - avatar
+ 1
No problem. Have fun learning! ;)
26th Feb 2022, 2:14 PM
Donato Paul Thayalan
Donato Paul Thayalan - avatar
+ 1
Nice, what you did is a functional approach to model a stack, whereas the other is object-oriented.
1st Mar 2022, 12:59 PM
Donato Paul Thayalan
Donato Paul Thayalan - avatar
+ 1
Sorry for the delay, haven't been on SoloLearn for about a week, but yes, there's nothing wrong with the code.
11th Mar 2022, 5:28 AM
Donato Paul Thayalan
Donato Paul Thayalan - avatar