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, 21.06.2019 16:00
In a cellular network each cell is controlled by a tower what are these towers called?
Answers: 3
question
Computers and Technology, 21.06.2019 17:10
Write an application that allows a user to enter the names and birthdates of up to 10 friends. continue to prompt the user for names and birthdates until the user enters the sentinel value “zzz” for a name or has entered 10 names, whichever comes first. when the user is finished entering names, produce a count of how many names were entered, and then display the names. in a loop, continuously ask the user to type one of the names and display the corresponding birthdate or an error message if the name has not been previously entered. the loop continues until the user enters “zzz” for a name. save the application as birthdayreminder.java.
Answers: 1
question
Computers and Technology, 22.06.2019 10:40
5. illustrate how fine-line inventory classification can be used with product and market segments. what are the benefits and considerations when classifying inventory by product, market, and product/market?
Answers: 2
question
Computers and Technology, 22.06.2019 23:30
Creating "smart interfaces" in all sectors of industry, government, and the public arena is one of the fastest growing hct areas. these interfaces model, interpret, and analyze such human characteristics as speech, gesture, and vision. the field of biometrics, in which humans authenticate themselves to machines, is an area of considerable interest to hct practitioners. fingerprint scans are one of the most frequently used biometric options, and this article, biometric student identification: practical solutions for accountability & security in schools, makes a case for the implementation of fingerprint scans in schools. critique the article, and answer the following questions: according to the author, what are the main benefits of adopting fingerprint scans in schools for student identification? according to the author, what are the main drawbacks of adopting fingerprint scans in schools for student identification? do you agree with the author's assessment of the pl
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
Mathematics, 01.02.2020 18:43
question
Chemistry, 01.02.2020 18:43
Questions on the website: 13722362