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 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 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.
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,...
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 # ************************************************* #...
Python please! Create one program to include all 10 elements: Create a function that prints a...
Python please! Create one program to include all 10 elements: Create a function that prints a sentence passed to the function as parameter. On the next line, print the same sentence without the 1st and the last character, use string slicing. Use the following sentence: This is the sentence. Given a string: This string was copied in an article. Replace the relevant words to read as follows: This string was discovered in the article xyz. Given a string: This string...
Why do items in a Python dictionary NOT stay in the order in which they are...
Why do items in a Python dictionary NOT stay in the order in which they are added? Put another way, how are the values indexed?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT