Need Help on Reverse Polish Notation program code

Thread Solved

Join Date: Sep 2009
Posts: 2
Reputation: ljvasil is an unknown quantity at this point 
Solved Threads: 0
ljvasil ljvasil is offline Offline
Newbie Poster

Need Help on Reverse Polish Notation program code

 
0
  #1
19 Days Ago
I am having problems with my reverse polish calculator code. The code is as follows:

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven
 
0
  #2
19 Days Ago
It's always good to use a few test prints:
  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:
  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; 19 Days Ago at 2:38 pm. Reason: code
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 2
Reputation: ljvasil is an unknown quantity at this point 
Solved Threads: 0
ljvasil ljvasil is offline Offline
Newbie Poster
 
0
  #3
19 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,983
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 926
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #4
18 Days Ago
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; 18 Days Ago at 5:51 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

Tags
python

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC