programming

Stack Operations in Python

Stack Operations in Python

STACK OPERATION USING LIST IN PYTHON CONTENT * PROGRAM STATEMENT * CONCEPT * SOLUTION * EXPLANATION * OUTPUT PROGRAM STATEMENT Write a Python program to implement the Stack Operations i.e, Push and Pop using List and demonstrate its working. CONCEPT Python is a simple and very powerful programming language. The reason for it to be simple is that there are a lot of built-in functions for our use which solves a lot of problems. Python uses an interpreter which means it executes each line before moving to the next one, unlike the languages which use a compiler that executes the entire program together. For demonstrating the stack operations, we are going to use a data structure called List in python. The list is equivalent to an array, but with some added benefits. Important Characteristics of Lists: * Lists are ordered. * Lists can contain any arbitrary objects. * List elements can be accessed by index. * Lists can be nested to arbitrary depth. * Lists are mutable. * Lists are dynamic. Example of a simple list: simple list [https://itnepal.org/media/simple_list-90e4.png] => The list in python is created using the square brackets as shown in the example above. => List can also be created by using a constructor of inbuilt class List as follows: constructor list [https://itnepal.org/media/constructor_list-50f4.png] => It is not necessary to store similar items in a list. Elements of the list can belong to different data types as shown below: different element's list [https://itnepal.org/media/different_elements-0ab7.png] The List class contains various methods which perform different operations on the list like append(), pop(), remove() etc. These operations can be viewed by using the dir() as shown below: [https://itnepal.org/media/list methods-90d9.png] => There are methods starting with double underscores which are called magic methods or dunder methods, but those are not necessary for stack operations. => Pay attention to the methods without the underscores. These are the built-in methods at our disposal. We will be using some of these methods in our program. SOLUTION # defining the push function def push(): item = input('Enter the element to push: ') # using the built-in method append() my_list.append(item) # append() places the element at the end of the list # defining the pop function def pop(): # using the built-in method pop() my_list.pop() # pop() removes an element from the end of the list # defining the display function def display(): print(my_list) # creating an empty list using constructor my_list = list() # running an infinity loop while True: print('''Stack Operations 1. Push 2. Pop 3. Display 4. Exit''') choice = int(input('Enter your choice: ')) if choice == 1: push() print('Push successful.') elif choice == 2: pop() print('Pop successful.') elif choice == 3: display() elif choice == 4: quit() else: print('Invalid Input. Try again with a valid input.')   EXPLANATION Let us understand the program in order of execution. Firstly, we create an empty list named my_list using the constructor. The program runs an infinite loop which allows the user to continuously perform the stack operations. The menu is given to the user in the output console and the program waits for the user to enter the choice. Depending on the value of choice entered by the user, the conditional statement (if-elif-else) comes into play. It compares the values and calls an appropriate function which is already defined i.e, push(), pop() and display(), with respect to the value entered by the user. Our functions use built-in methods of the list to carry out the stack operations. The built-in list methods used in our program are: append(element) - It inserts the element at the end of the list. pop() - It removes the element at the end of the list. => You can see the functionality of other methods of the list at https://docs.python.org/3/tutorial/datastructures.html [https://docs.python.org/3/tutorial/datastructures.html] OUTPUT stack output [https://itnepal.org/media/stack output-90fe.png] This is a Python translation of original writing in JAVA [https://www.rabinsxp.com/java/stack-using-arrays-push-pop-display-methods-java-programming-language/].

Editors' Pick

Cyclone Category Program

OBJECT ORIENTED PROGRAMMING QUESTION Write a python application that displays a cyclone's category based on the user's input of the wind speed. The Cyclone Scale classifies cyclones into five categories numbered 1 through 5. The minimum sustained wind speeds for categories 4 through 1 are 260, 225, 170 and 125 km per hour, respectively. Category 5 cyclone has sustained winds of at least 280 km per hour. Any storm with winds of less than 125 km per hour is not a cyclone. SOLUTION class Cyclone: def category1(self): print('This is a Category-1 cyclone which has sustained wind speed of 125 km per hour.') def category2(self): print('This is a Category-2 cyclone which has sustained wind speed of 170 km per hour.') def category3(self): print('This is a Category-3 cyclone which has sustained wind speed of 225 km per hour.') def category4(self): print('This is a Category-4 cyclone which has sustained wind speed of 260 km per hour.') def category5(self): print('This is a Category-5 cyclone which has sustained wind speed of at least 280 km per hour.') def calculate(self, speed): if 170 > speed >= 125: self.category1() elif 225 > speed >= 170: self.category2() elif 260 > speed >= 225: self.category3() elif 280 > speed >= 260: self.category4() elif speed >= 280: self.category5() else: print('It is not a Cyclone.') while True: wind_speed = int(input('Enter the wind speed in km per hour: ')) if wind_speed == 0: quit() Cyclone().calculate(wind_speed) print('Enter 0 to exit.') OUTPUT Cyclone Program Output [http://itnepal.org/media/Cyclone Program Output-1dd9.png] EXPLANATION The program categorizes the cyclones based on the wind speeds provided by the user. The user input wind_speed is passed as an argument to the calculate(self, speed) method of class Cyclone which compares the user input i.e, the speed with sustained wind speeds. Depending on the comparisons, respective category methods (like category1()) are called within the calculate method. The category methods contain suitable printing statements for each category of cyclones i.e, category 1 through 5. Since the program is in an infinite loop, the user can continuously give the wind speed values. And the program is made to exit when the wind speed value entered is 0.

Top