Question

In: Computer Science

PYTHON-- create a method that prints items from a stack in a top to bottom order

PYTHON-- create a method that prints items from a stack in a top to bottom order

Solutions

Expert Solution

If you have any queries please comment in the comments section I will surely help you out and if you found this solution to be helpful kindly upvote.

Solution :

Code :

# write a class to define a stack
class Stack:
# constructor to initialize an empty list for stack elements and top as -1
def __init__(self):
self.elements = []
self.top = -1
# function to push the elements into the stack
def push(self, val):
# append the item passed in the function to the stack list
self.elements.append(val)
# increment top
self.top = self.top + 1
# function to pop the elements from the stack
def pop(self):
return self.elements.pop()
# function that print items of the stack from top to bottom
def top_to_bottom(self):
# iterate from top to bottom
for i in range(self.top,-1,-1):
# print the element
print(self.elements[i])
# create an object of stack class
s = Stack()
# push some elements into the stack
s.push(10)
s.push(20)
s.push(30)
s.push(40)
s.push(50)
# call the top to bottom function to print the elements from top to bottom
s.top_to_bottom()

Output :


Related Solutions

Create a python program that prints the series from 5 to 500.
Create a python program that prints the series from 5 to 500.
Write a Python program to (a) create a new empty stack. Then, (b) add items onto...
Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents:
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
Write a Python function that receives a stack object s, where the items in the stack...
Write a Python function that receives a stack object s, where the items in the stack are only numbers in the set {0,1} and returns the number of 1's in the stack s. The function must satisfy the following restrictions: the state of the stack must be preserved; ie., after calling this function the state of the stack s must be the same it was before the function was called. The function cannot use any additional variables of any of...
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack...
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack s using a Queue. Test your method using some example stacks. In java
Show the obrital-filling diagram for S (sulfur). Stack the subshells in order of energy, with the lowest-energy subshell at the bottom and the highest-energy subshell at the top
1) Show the obrital-filling diagram for S (sulfur). Stack the subshells in order of energy, with the lowest-energy subshell at the bottom and the highest-energy subshell at the top.2) Show the orbital-filling diagram for Br (bromnie). Stack the subshells in order of energy, with the lowest-energy subshell at the bottom and the highest-energy subshell at the top.
Create a program that parses a CSV file of product data and prints the items with...
Create a program that parses a CSV file of product data and prints the items with a price that is less than or equal to that input by the user. • Your program should take two arguments: an input file to process and a price limit. • Print only the names of each item to stdout that have a price less than or equal to the given limit. • If the given file does not exist or cannot be opened,...
Create and submit a Python program (in a module) that contains a main function that prints...
Create and submit a Python program (in a module) that contains a main function that prints 17 lines of the following text containing your name: Welcome to third week of classes at College, <your name here>! can someone please show me how to do this step by step? I'm just confused and keep getting errors in idle.
Write method reverseStack(Stack s) that receives a stack s and reverse the order of its elements....
Write method reverseStack(Stack s) that receives a stack s and reverse the order of its elements. the values inside the stack must be changed, that the top will be the last and so on. please use java code to slove. Thank you.
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then,...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents: Please use the following comments in your python script: # ************************************************* # COP3375 # Student's full name ( <<< your name goes here) # Week 8: Assignment 1 # ************************************************* #...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT