Hey guys. Help me write an array implemented Python program to implement Stack operations.  | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 3

Hey guys. Help me write an array implemented Python program to implement Stack operations. 

Implementing Stack Operations using Python

24th Feb 2022, 5:27 PM
Takudzwa A Svosve
Takudzwa A Svosve - avatar
17 Réponses
+ 4
Hey Ipang , yes you posted the code, but it makes no sense to just replicate it. Why dont we discuss it and see where the pop, push and the other functions are coming from and what they mean. I'd be pleased to get an explanation.
24th Feb 2022, 7:01 PM
Takudzwa A Svosve
Takudzwa A Svosve - avatar
+ 4
Oh ok. Then it seems i still have a lot to learn. Thanks Donato Paul Thayalan
26th Feb 2022, 2:12 PM
Takudzwa A Svosve
Takudzwa A Svosve - avatar
28th Feb 2022, 9:52 PM
Takudzwa A Svosve
Takudzwa A Svosve - avatar
+ 4
11th Mar 2022, 6:02 AM
Takudzwa A Svosve
Takudzwa A Svosve - avatar
+ 3
Hey Donato Paul Thayalan , I've been quiet trying to grasp what you've said but still nothing seems to make sense to me.
26th Feb 2022, 12:22 PM
Takudzwa A Svosve
Takudzwa A Svosve - avatar
+ 3
Ok Donato Paul Thayalan , i get the theoretics behind what you've said, but the code, I've been trying to run it but it doesn't seem to output anything. Is it complete like this or there's something that I'm missing?
26th Feb 2022, 12:33 PM
Takudzwa A Svosve
Takudzwa A Svosve - avatar
+ 3
I tried to run the one Ipang sent initially
26th Feb 2022, 1:49 PM
Takudzwa A Svosve
Takudzwa A Svosve - avatar
+ 3
Hey guys Ipang and Donato Paul Thayalan, i think i have finally managed to get the hang of the code i was talking about. Let me post the code i wrote here and see if you guys agree
28th Feb 2022, 9:49 PM
Takudzwa A Svosve
Takudzwa A Svosve - avatar
+ 3
Oh ok. But its legit right?
1st Mar 2022, 2:43 PM
Takudzwa A Svosve
Takudzwa A Svosve - avatar
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