944,050 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1696
  • Python RSS
Nov 7th, 2009
0

Need Help on Reverse Polish Notation program code

Expand Post »
I am having problems with my reverse polish calculator code. The code is as follows:

Python Syntax (Toggle Plain Text)
  1. import operator
  2.  
  3. OPERATORS = {
  4. '+': operator.add,
  5. '-': operator.sub,
  6. '*': operator.mul,
  7. '/': operator.div,
  8. }
  9.  
  10. class Stack(object):
  11. "Creates a stack to enter numbers or operators into"
  12.  
  13. def __init__(self):
  14. self.storage = [0,0,0,0]
  15. self.sptn = -1
  16.  
  17. def push(self, item):
  18. self.sptn = self.sptn + 1
  19. self.storage[self.sptn] = item
  20.  
  21. def pop(self):
  22. self.sptn = self.sptn -1
  23.  
  24. def top(self):
  25. return self.storage[self.sptn]
  26.  
  27. def __rep__(self):
  28. return self.storage[:self.sptn + 1]
  29.  
  30.  
  31. def get_input():
  32.  
  33. equation = raw_input("Enter the equation in reverse polish notation: ")
  34. return equation
  35.  
  36. def evaluate_rpn(equation):
  37.  
  38. s = Stack()
  39.  
  40. try:
  41. for item in equation.split():
  42. try:
  43. result = float(item)
  44. s.push(result)
  45. except ValueError:
  46. x = s.pop()
  47. y = s.pop()
  48. result = OPERATORS[item](x, y)
  49. s.push(result)
  50. result = s.top()
  51.  
  52. except IndexError:
  53. print "Please input at least two numbers before entering an operator."
  54.  
  55. def main():
  56. equation = get_input()
  57. answer = evaluate_rpn(raw_input("Enter the equation in reverse polish notation: "))
  58. print answer

The code trips up when it hits an operator for some reason. The line is causing me trouble is the "results = OPERATOR[item] (x, y)"

I would appreaciate any help!!! Please!!!

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ljvasil is offline Offline
4 posts
since Sep 2009
Nov 7th, 2009
0
Re: Need Help on Reverse Polish Notation program code
It's always good to use a few test prints:
Python Syntax (Toggle Plain Text)
  1. import operator
  2.  
  3. OPERATORS = {
  4. '+': operator.add,
  5. '-': operator.sub,
  6. '*': operator.mul,
  7. '/': operator.div,
  8. }
  9.  
  10. class Stack(object):
  11. "Creates a stack to enter numbers or operators into"
  12.  
  13. def __init__(self):
  14. self.storage = [0,0,0,0]
  15. self.sptn = -1
  16.  
  17. def push(self, item):
  18. self.sptn = self.sptn + 1
  19. self.storage[self.sptn] = item
  20.  
  21. def pop(self):
  22. self.sptn = self.sptn -1
  23.  
  24. def top(self):
  25. return self.storage[self.sptn]
  26.  
  27. def __rep__(self):
  28. return self.storage[:self.sptn + 1]
  29.  
  30.  
  31. def get_input():
  32.  
  33. equation = raw_input("Enter the equation in reverse polish notation: ")
  34. return equation
  35.  
  36. def evaluate_rpn(equation):
  37.  
  38. s = Stack()
  39.  
  40. try:
  41. for item in equation.split():
  42. try:
  43. result = float(item)
  44. s.push(result)
  45. except ValueError:
  46. x = s.pop()
  47. y = s.pop()
  48. print x, y # test --> None None problem is here!!!
  49. result = OPERATORS[item](x, y)
  50. s.push(result)
  51. result = s.top()
  52. print result # test
  53. return result
  54.  
  55. except IndexError:
  56. print "Please input at least two numbers before entering an operator."
  57.  
  58. def main():
  59. # use for testing
  60. equation = '2 3 +'
  61. #equation = get_input()
  62. answer = evaluate_rpn(equation)
  63. print answer
  64.  
  65. main()
The problem is in your class Stack. If you use a Python Queue as a stack it works fine:
Python Syntax (Toggle Plain Text)
  1. import operator
  2. import Queue
  3.  
  4. OPERATORS = {
  5. '+': operator.add,
  6. '-': operator.sub,
  7. '*': operator.mul,
  8. '/': operator.div,
  9. }
  10.  
  11. def get_input():
  12.  
  13. equation = raw_input("Enter the equation in reverse polish notation: ")
  14. return equation
  15.  
  16. def evaluate_rpn(equation):
  17.  
  18. # use a Python queue for a stack
  19. s = Queue.Queue(-1)
  20.  
  21. try:
  22. for item in equation.split():
  23. try:
  24. result = float(item)
  25. s.put(result)
  26. except ValueError:
  27. x = s.get()
  28. y = s.get()
  29. #print x, y # test
  30. result = OPERATORS[item](x, y)
  31. s.put(result)
  32. result = s.get()
  33. #print result # test
  34. return result
  35.  
  36. except IndexError:
  37. print "Please input at least two numbers before entering an operator."
  38.  
  39. def main():
  40. # use for testing
  41. equation = '2 3 +'
  42. # otherwise use ...
  43. #equation = get_input()
  44. answer = evaluate_rpn(equation)
  45. print answer # '2 3 +' --> 5.0
  46.  
  47. main()
Last edited by sneekula; Nov 7th, 2009 at 2:38 pm. Reason: code
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Nov 7th, 2009
0
Re: Need Help on Reverse Polish Notation program code
Ok, thank you for the corrections. The problem is that I have to use a class for this part of the course. I thought I started the positions by s.sptn (start point) = -1 in the __init__(self) function. Any more help with trying to figure out why the class Stack is incorrect would be appreciated!

Thanks,
Linda
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ljvasil is offline Offline
4 posts
since Sep 2009
Nov 8th, 2009
0
Re: Need Help on Reverse Polish Notation program code
Why don't you simply separate your class Stack out and send it through some simple tests to figure out the errors?

Looks like pop() should return something.
Last edited by vegaseat; Nov 8th, 2009 at 5:51 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Dictionaries become empty
Next Thread in Python Forum Timeline: Drawing Circles?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC