Refer to the instructions below and the images attached. Solve using python preferably in repl.it and follow
Question:
Refer to the instructions below and the images attached. Solve using python preferably in repl.it and follow the skeleton code below.
Â
#This program should use a stack to implement a reverse polish calculator. Â e.g. Â 5x(2+3) Â == Â 5 2 3 + x
# pre-requisite knowledge: how strings are represented in python, CLA, unicode/ascii, ord(), numbers start with a digit or with a '.'
# This calculator must correctly implement, '+', '-', 'x', and '/' operations (we will use little x for multiplication, NOT *).
# THIS VERSION WILL USE Command Line Arguments (CLA)
#Read the instructions tab for all assignment information
def pop(stk,sp) :
# YOUR CODE FOR POP GOES HERE
print("remove this print line later")
def push(val,stk,sp) :
sp[0] = sp[0] + 1
if(sp[0] == len(stk)) :
 stk.append(val)
else :
 stk[sp[0]] = val
def calculate(str) : # needs to be cleaned up to handle illegal strings
# creating stack. Â stk[] is an empty stack so the stack pointer (sp) starts as -1. Â Why is sp a list of size 1?
stk = []
sp = [-1]
# YOUR CODE GOES HERE
return stk[0]
Â
Â
Â
Â
def convert_str_types(str) :
print("remove this print line later")
# This procedure must convert the apporpriate elements of list str from strings to floats. Â You can use the following command to cast the string as a float iff str[k] would mathematically be a real number.
# str[k] = float(str[k])
# YOUR CODE GOES HERE
################# MAIN CALLING ROUTINE ################
from sys import * # for CLA
# MUST use x instead of * for multiplation as * has meaning on the command line
str = []
k = 1 #argv[0] is always the name of the file you are running
while(k str.append(argv[k])
k = k + 1
print(str)
# convert the parts of the string that are numbers to floats
convert_str_types(str)
ans = calculate(str)
print(ans)
Â
Cambridge International AS & A Level Computer Science
ISBN: 9781510457591
1st Edition
Authors: David Watson, Helen Williams