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 23:20
How can you tell if someone sent you a text message to your email instead of a email
Answers: 1
question
Computers and Technology, 23.06.2019 02:00
Read this excerpt from helen keller’s autobiography, the story of my life. have you ever been at sea in a dense fog, when it seemed as if a tangible white darkness shut you in, and the great ship, tense and anxious, groped her way toward the shore with plummet and sounding-line, and you waited with beating heart for something to happen? i was like that ship before my education began, only i was without compass or sounding-line, and had no way of knowing how near the harbour was. "light! give me light! " was the wordless cry of my soul, and the light of love shone on me in that very hour. . the morning after my teacher came she led me into her room and gave me a doll. the little blind children at the perkins institution had sent it and laura bridgman had dressed it; but i did not know this until afterward. when i had played with it a little while, miss sullivan slowly spelled into my hand the word "d-o-l-l." i was at once interested in this finger play and tried to imitate it. when i finally succeeded in making the letters correctly i was flushed with childish pleasure and pride. running downstairs to my mother i held up my hand and made the letters for doll. i did not know that i was spelling a word or even that words existed; i was simply making my fingers go in monkey-like imitation. in the days that followed i learned to spell in this uncomprehending way a great many words, among them pin, hat, cup and a few verbs like sit, stand and walk. based on this excerpt, which words best describe helen keller?
Answers: 2
question
Computers and Technology, 23.06.2019 04:31
Q14 what is most important for you to choose before you build a network? a. private network b. nos c. network media d. network protocol e. directory service
Answers: 1
question
Computers and Technology, 24.06.2019 05:30
Why is hard disk space important to an audio engineer? why are usb ports and firewire ports useful for an audio engineer? explain in 2-3 sentences. (3.0 points) here's a list of different audio software: ableton live apple inc.'s garageband apple inc.'s logic studio digidesign's pro tools propellerhead sofware's reason sony creative software's acid pro steinberg cubase steinberg nuendo choose one of the software programs listed above, and then go to that software program's web site. read about what the software program is used for, and then write 4-5 sentences about what you learned. (10.0 points) which type of software license is the most limiting? why? explain in 2-3 sentences. (3.0 points) when sending a midi channel voice message, how can you control the volume of the sound? explain in 2-3 sentences. (4.0 points)
Answers: 1
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
Health, 28.04.2021 18:20
question
Chemistry, 28.04.2021 18:20
question
Mathematics, 28.04.2021 18:20
question
Mathematics, 28.04.2021 18:20
question
Mathematics, 28.04.2021 18:20
question
Chemistry, 28.04.2021 18:20