Question

In: Computer Science

Run Python code  List as Stack  and verify the following calculations; submit screen shots in a single file....

Run Python code  List as Stack  and verify the following calculations; submit screen shots in a single file.

Postfix Expression                Result

4 5 7 2 + - * = -16

3 4 + 2  * 7 / = 2

5 7 + 6 2 -  * = 48

4 2 3 5 1 - + * + = 18   

List as Stack

"""

File: pyStackPostfix.py

Author: JD

"""

# 5 7 + 6 2 -  * = 48

print("Postfix Calculator\n")

stack = []              # Empty stack

y = int(0)

z = int(0)

w = int(0)

while True:

   x = input("Enter a postfix expression one by one:")

   if x >= '0' and x<= '9':

      x = int(x)

      stack.append(x)      # Push on top of stack

   elif x == '+':          # Got an operator

      if len(stack) >= 2:

         y = stack.pop()   # Pop from top of stack

         z = stack.pop()

         w = y + z

         stack.append(w)   # Push result back

      else:

         print("Stack error") # Not enough operhand A + B

         break

   elif x == '-':

      if len(stack) >= 2:

         y = stack.pop()

         z = stack.pop()

         w = y - z

         stack.append(w)   

      else:

         print("Stack error")

         break

   elif x == '*':

      if len(stack) >= 2:

         y = stack.pop()

         z = stack.pop()

         w = y * z

         stack.append(w)   

      else:

         print("Stack error")

         break

   elif x == '/':

      if len(stack) >= 2:

         y = stack.pop()

         z = stack.pop()

         w = y / z

         stack.append(w)   

      else:

         print("Stack error")

         break

   elif x == '=':             # Equal operator

      if len(stack) == 1:

         z = stack.pop()      # Pop the result

         print (z)

         break

      else:

         print("Stack error")

         break

Postfix Calculator

Enter a postfix expression one by one:5

Enter a postfix expression one by one:7

Enter a postfix expression one by one:+

Enter a postfix expression one by one:6

Enter a postfix expression one by one:2

Enter a postfix expression one by one:-

Enter a postfix expression one by one:*

Enter a postfix expression one by one:=

48

Press any key to continue . . .

its python

Solutions

Expert Solution

code:

print("Postfix Calculator\n")

stack = []              # Empty stack

y = int(0)

z = int(0)

w = int(0)

while True:

   x = input("Enter a postfix expression one by one:")

   if x >= '0' and x<= '9':

      x = int(x)

      stack.append(x)      # Push on top of stack

   elif x == '+':          # Got an operator

      if len(stack) >= 2:

         y = stack.pop()   # Pop from top of stack

         z = stack.pop()

         w = z + y            #here u have to change it to operand2 operator operand1

         stack.append(w)   # Push result back

      else:

         print("Stack error") # Not enough operhand A + B

         break

   elif x == '-':

      if len(stack) >= 2:

         y = stack.pop()

         z = stack.pop()

         w = z - y      #here u have to change it to operand2 operator operand1

         stack.append(w)

      else:

         print("Stack error")

         break

   elif x == '*':

      if len(stack) >= 2:

         y = stack.pop()

         z = stack.pop()

         w = z * y              #here u have to change it to operand2 operator operand1

         stack.append(w)

      else:

         print("Stack error")

         break

   elif x == '/':

      if len(stack) >= 2:

         y = stack.pop()

         z = stack.pop()

         w = z // y    #here u have to change it to operand2 operator operand1,extra "/" operator for floor division.

         stack.append(w)

      else:

         print("Stack error")

         break

   elif x == '=':             # Equal operator

      if len(stack) == 1:

         z = stack.pop()      # Pop the result

         print (z)

         break

      else:

         print("Stack error")

         break


output:


Related Solutions

Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
Run the commands used for following tasks and paste the screen shots here: a) Search/find a...
Run the commands used for following tasks and paste the screen shots here: a) Search/find a specified word in a file and display all the lines containing that word. b) List all the files and directories in the current directory (including the hidden files). c) Change to the root directory / and show a complete long listing of it. d) Append the output of file1 to file2. [1 Mark] e) Count how many lines, words and characters in file1. f)...
Run the following ANOVA analysis in excel and interpret your findings. Upload your screen shots of...
Run the following ANOVA analysis in excel and interpret your findings. Upload your screen shots of the steps you took in running the analysis in excel. Treatment 1 Treatment 2 Treatment 3 0 1 6 1 4 5 0 1 8 3 2 5
Run the following ANOVA analysis in excel and interpret your findings. Upload your screen shots of...
Run the following ANOVA analysis in excel and interpret your findings. Upload your screen shots of the steps you took in running the analysis in excel. Treatment 1 Treatment 2 Treatment 3 0 1 6 1 4 5 0 1 8 3 2 5
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Name your program file warmup.py Submit your working Python code to your CodePost.io account. In this...
Name your program file warmup.py Submit your working Python code to your CodePost.io account. In this challenge, establish if a given integer num is a Curzon number. If 1 plus 2 elevated to num is exactly divisible by 1 plus 2 multiplied by num, then num is a Curzon number. Given a non-negative integer num, implement a function that returns True if num is a Curzon number, or False otherwise. Examples is_curzon(5) ➞ True 2 ** 5 + 1 =...
Marielab1 input your age and display what your age would be next year. submit screen shots...
Marielab1 input your age and display what your age would be next year. submit screen shots from Marie. im 24 and going to be 25 next year.
Use python programming to write this code and provide a screen short for the code. 2....
Use python programming to write this code and provide a screen short for the code. 2. Write a function that takes one argument (a string) and returns a string consisting of the single character from that string with the largest value. Your function should contain a for loop. You can assume that the input to your function will always be a valid string consisting of at least one character. You can assume that the string will consist only of lower-case...
I have a Python code that reads the text file, creates word list then calculates word...
I have a Python code that reads the text file, creates word list then calculates word frequency of each word. Please see below: #Open file f = open('example.txt', 'r') #list created with all words data=f.read().lower() list1=data.split() #empty dictionary d={} # Adding all elements of the list to a dictionary and assigning it's value as zero for i in set(list1):     d[i]=0 # checking and counting the values for i in list1:     for j in d.keys():        if i==j:           d[i]=d[i]+1 #Return all non-overlapping...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT