subject

The Stack As mentioned in class, Python 3 provides the LifoQueue class as part of the Queue library. To see how this is really just a wrapper around the basic list class below is our own Stack class that uses a list This code has been provided to you below in a module called stackclass. py You MAY NOT change the structure of the provided Stack class in ANY way. Your post-fix logic should go in your main script. If you find yourself wanting to add more methods to the class, it means you are writing redundant code and should stop. The Stack class has the following public interface: _init_ #creates an empty stack _str_#returns a string representation of the stack • push(data) #adds a new element (data) to the top of the stack • pop() #removes and returns the top element from the stack .top() #returns what on top of the stack, but doesn't remove it • isEmpty() #Returns True if the stack is empty and False otherwise. Postfix Math You are probably most familiar with math written in infix notation. For example, "4+3 - 7". In infix notation, operators are placed between their inputs. This is a very nice way to read math, but it is nontrivial for a computer to parse. An alternative representation is called postfix notation. In postfix we write the two inputs followed by the operator. The previous example would be "4 3 + 7-". This is much easier to parse because we have both inputs before the operator. We also don't have to worry about parenthesis Note: You may assume all symbols will have spaces between them. This makes it easy to split the expression. You will never be given "2 3+" instead of "23 + You may also assume that the only operators supported are +, -*./. Examples Postfix Infix Result 21+ 2+1 3 54- 5-4 1 72* 7*2 14 93/ 9/3 3 25+7* (2+5)*7 49 Postfix notation works well with a Stack. Here's how you can do it: 1. Push Numbers onto Stack as read from input. 2. When operator seen: 1. Pop top 2 items on stack. Since the second item popped off the top came before the top item in the postfix statement, it is the left operand. 2. Complete Operation. 3. Push result onto Stack. 3. Repeat until end of input. 4. Final Result will be on top of Stack. Within your main script, implement a function postfix(exp) that takes a string exp containing a postfix math expression as input and returns the result as a floating point number. You MUST use the provided Stack class to implement this function. Main Program In your main. py script (which should now also include your postfix(exp) function) ask the user to enter a postfix expression and print the result. Repeat this process until the user enters "exit". Here is an example, which should demonstrated the expected I/O (Note: All the tests for this lab are exact output tests). Welcome to Postfix Calculator Enter exit to quit Enter Expression 1 2 + Result: 3.0 Enter Expression 2 3 + Result: 5.0 Enter Expression 4 5 + Result: 9.0 Enter Expression 6 7 + Result: 13.0 Enter Expression 10 12 + Result: 22.0 Enter Expression 99 1 + Result: 100.0 Enter Expression -9 -8 + Result: -17.0 Enter Expression 1 2 3 4 5 6 + + + + + Result: 21.0 Enter Expression 3 4 * Result: 12.0 Enter Expression 6 5 * Result: 30.0 Enter Expression 9 7 * Result: 63.0 Enter Expression 10 9 * Result: 90.0 * Enter Expression 100 -9 Result: -900.0 Enter Expression 90 3 * Result: 270.0 Enter Expression 5 5 5 5 * * * Result: 625.0 Enter Expression 4 3 - Result: 1.0 Enter Expression 3 4 - Result: -1.0 Enter Expression 9 8 - Result: 1.0 Enter Expression 12 3 - Result: 9.0 Enter Expression 3 12 - Result: -9.0 Enter Expression 10 9 8 7 6 - - - Result: 8.0 Enter Expression 1 2 / Result: 0.5 Enter Expression 4 2 / Result: 2.0 Enter Expression 9 3 / Result: 3.0 Enter Expression 25 5 / Result: 5.0 Enter Expression 125 5 / Result: 25.0 Enter Expression 1 7 / Result: 0.14285714285714285 Enter Expression 100 2 2 2 /// Result: 50.0 Enter Expression 100 2 2 2 ** / Result: 12.5 Enter Expression 4 -1 9 5 2 3 + * - */ Result: 0.25 Enter Expression 100 2 / 2 / 2 / Result: 12.5 Enter Expression 1 2 * 7 + 9 * 11 + Result: 92.0 Enter Expression exit class Stack: #Create a New Empty Stack definit (self): self. __S = [] #Display the Stack def __str__(self): return str(self. __S) #Add a new element to top of stack def push(self, x): self. - S. append(x) #Remove the top element from stack def pop (self): return self.__S. pop #See what element is on top of stack #Leaves stack unchanged def top (self): return self.__S[-1] def isEmpty (self): return len(self. __S)==0

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 00:30
At an open or uncontrolled intersection, yield if a. the cross road has more lanes than yours b. the cross road has fewer land than yours c. you’re on a state highway and the cross road is a secondary road d. do you have three or more passengers in your vehicle
Answers: 2
question
Computers and Technology, 22.06.2019 06:20
In what kind of attack can attackers make use of millions of computers under their control in an attack against a single server or network availability confidentiality integrity identity automated attack software? those who wrongfully disclose individually identifiable health information can be fined up to what amount per calendar year? single most expensive malicious attack hipaa what are script kiddies? advanced persistent threat security manager security engineer what level of security access should a computer user have to do their job what process describes using technology as a basis for controlling the access and usage of sensitive data? cybercriminal
Answers: 1
question
Computers and Technology, 23.06.2019 00:00
Suppose you have 9 coins and one of them is heavier than others. other 8 coins weight equally. you are also given a balance. develop and algorithm to determine the heavy coin using only two measurements with the of the balance. clearly write your algorithm in the form of a pseudocode using the similar notation that we have used in the class to represent sorting algorithms
Answers: 1
question
Computers and Technology, 23.06.2019 23:00
How do you know if the website is secure if you make a purchase
Answers: 2
You know the right answer?
The Stack As mentioned in class, Python 3 provides the LifoQueue class as part of the Queue library....
Questions
question
Social Studies, 04.11.2020 20:40
question
Mathematics, 04.11.2020 20:40
Questions on the website: 13722363