In: Computer Science
Q: Assistance in understanding and solving this example from Data Structure & Algorithms (Computer Science) with the steps of the solution to better understand, thanks.
##Using R studio language and code to use provided below, thanks.
In this assignment, we will implement a function to evaluate an
arithmetic expression. You will use two stacks to keep track of
things. The function is named ‘evaluate’, it takes a string as
input parameter, and returns an integer. The input string
represents an arithmetic expression, with each character in the
string being a token, while the returned value should be the value
of the expression.
Examples:
• evaluate ('1+2*3') ➔ 7
• evaluate ('1*2+3') ➔ 5
##This is the code provided to use
"""Basic example of an adapter class to provide a stack interface to
Python's list."""
class ArrayStack:
"""LIFO Stack implementation using a Python list as underlying
storage."""
def __init__ (self):
"""Create an empty stack."""
self._data = [] # nonpublic list instance
def __len__ (self):
"""Return the number of elements in the stack."""
return len (self._data)
def is_empty (self):
"""Return True if the stack is empty."""
return len (self._data) == 0
def push (self, e):
"""Add element e to the top of the stack."""
self._data.append (e) # new item stored at end of
list
def top (self):
"""Return (but do not remove) the element at the top of the stack.
Raise Empty exception if the stack is empty.
"""
if self.is_empty ():
raise Empty ('Stack is empty')
return self._data [-1] # the last item in the list
def pop (self):
"""Remove and return the element from the top of the stack (i.e.,
LIFO).
Raise Empty exception if the stack is empty.
"""
if self.is_empty ():
raise Empty ('Stack is empty')
return self._data.pop () # remove last item from list
class Empty (Exception):
"""Error attempting to access an element from an empty container."""
pass
if __name__ == '__main__':
S = ArrayStack () # contents: [ ]
S.push (5) # contents: [5]
S.push (3) # contents: [5, 3]
print (len (S)) # contents: [5, 3]; outputs 2
print (S.pop ()) # contents: [5]; outputs 3
print (S.is_empty ()) # contents: [5]; outputs False
print (S.pop ()) # contents: [ ]; outputs 5
print (S.is_empty ()) # contents: [ ]; outputs True
S.push (7) # contents: [7]
S.push (9) # contents: [7, 9]
print (S.top ()) # contents: [7, 9]; outputs 9
S.push (4) # contents: [7, 9, 4]
print (len (S)) # contents: [7, 9, 4]; outputs 3
print (S.pop ()) # contents: [7, 9]; outputs 4
S.push (6) # contents: [7, 9, 6]
S.push (8) # contents: [7, 9, 6, 8]
print (S.pop ()) # contents: [7, 9, 6]; outputs 8