Question

In: Computer Science

Q: Assistance in understanding and solving this example from Data Structure & Algorithms (Computer Science) with...

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


Solutions

Expert Solution


Related Solutions

Q: Asking for assistance in understanding and solving this example on Modern Algebra II with the...
Q: Asking for assistance in understanding and solving this example on Modern Algebra II with the steps of the solution to better understand, thanks. **Please give the step by steps with details to completely see how the solution came about. 1) Determine all elements in an integral domain that are their own inverses under multiplication. 2) Let F be a finite field with n elements. Prove that x^(n-1 )= 1 for all nonzero x in F. Hint: Use the fact...
Computer Science: Data Structures and Algorithms Practice: Construct a C++ program that takes two SQUARE matrices,...
Computer Science: Data Structures and Algorithms Practice: Construct a C++ program that takes two SQUARE matrices, and multiplies them and produces a new matrix: Example: [Matrix A] * [Matrix B] = [Matrix C] (A 3x3 is the most preferred example to use) (The first two matrices, A and B can be fixed numbers, hard-coded into the program. as opposed to user input) Display Matrix C, also (cout etc.)
Q: Assistance to understand clearly and solve this example from Probability and Statistical Inference with the...
Q: Assistance to understand clearly and solve this example from Probability and Statistical Inference with the steps of the solution to better understand, thanks. **Please give the step by step with details to completely see how the solution came about, plenty of thanks. 1) To test two methods of instruction, 50 students are selected at random from each of two groups. At the end of the instruction period, each student is assigned a grade (A, B, C, D, or F)...
Thoroughly explain the impacts of data structures and algorithms in the development and implementation of computer...
Thoroughly explain the impacts of data structures and algorithms in the development and implementation of computer programs using modern computer technologies.
4. Gradient descent. Gradient descent is one of the most popular algorithms in data science and...
4. Gradient descent. Gradient descent is one of the most popular algorithms in data science and by far the most common way to optimise neural networks. A function is minimised by iteratively moving a little bit in the direction of negative gradient. For the two-dimensional case, the step of iteration is given by the formula xn+1 , yn+1 = xn, yn − ε ∇f(xn, yn). In general, ε does not have to be a constant, but in this question, for...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …,...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …,...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …,...
Describe different types of data structures in Computer Science with examples.
Describe different types of data structures in Computer Science with examples.
Using classes and array data structure write methods with algorithms for a software that an airline...
Using classes and array data structure write methods with algorithms for a software that an airline can use to view available/booked seats, management booking, canceling booking and reorder seats. The solution should consist of a minimum of two classes with one class containing the main method and second class for managing a manipulating data. The choice of data structure for this assignment will be static one dimension arrays. in C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT