@@ -16,20 +16,20 @@ class Expression:
1616 is invalid.
1717 """
1818
19- # The __operator_stack variable uses standard Python lists to implement
19+ # The _operator_stack variable uses standard Python lists to implement
2020 # a simple stack. As operators are parsed from the string,
2121 # they are appended to the stack. As the input string is processed, the
2222 # grows as needed. In the end, it should be empty.
2323
24- __operator_stack = [] # Holds the current stack of operators
24+ _operator_stack = [] # Holds the current stack of operators
2525
2626 # Store the string, and where we are in our parsing run.
27- __expr_string = ""
28- __output_string = ""
29- __current_position = 0
27+ _expr_string = ""
28+ _output_string = ""
29+ _current_position = 0
3030
3131 # Have we evaluated this expressions yet?
32- __evaluated = False
32+ _evaluated = False
3333
3434 def __init__ (self , expression_string ):
3535 """
@@ -38,19 +38,19 @@ def __init__(self, expression_string):
3838 """
3939
4040 # Add '$' as an end of line marker
41- self .__expr_string = expression_string .strip () + "$"
41+ self ._expr_string = expression_string .strip () + "$"
4242
4343 # Start parsing at the first character
44- self .__current_position = 0
44+ self ._current_position = 0
4545
4646 # No output string yet
47- self .__output_string = ""
47+ self ._output_string = ""
4848
4949 # Clear the stack
50- self .__operator_stack .clear ()
50+ self ._operator_stack .clear ()
5151
5252 # Reset the evaluated flag
53- self .__evaluated = False
53+ self ._evaluated = False
5454
5555 def result (self ):
5656 """
@@ -59,18 +59,18 @@ def result(self):
5959 If this is unsuccessful, we raise a ValueError exception.
6060 Else we return the output string.
6161 """
62- if not self .__evaluated :
62+ if not self ._evaluated :
6363 self .parse ()
64- if not self .__evaluated :
64+ if not self ._evaluated :
6565 raise ValueError
66- return self .__output_string
66+ return self ._output_string
6767
6868 def parse (self ):
6969 """ Parses the current infix expression, and return the RPN version."""
7070
7171 # If we've already evaluated, just return the result
72- if self .__evaluated :
73- return self .__output_string
72+ if self ._evaluated :
73+ return self ._output_string
7474
7575 # Let's start evaluating
7676 # Right now, every expression starts with an operand
@@ -80,18 +80,18 @@ def parse(self):
8080 expecting_operand = True
8181
8282 # Get the current character to inspect
83- current_char = self .__expr_string [self .__current_position ]
83+ current_char = self ._expr_string [self ._current_position ]
8484
8585 # Loop until we're past the end of the string
8686 while (
87- self .__current_position < len (self .__expr_string )
87+ self ._current_position < len (self ._expr_string )
8888 and current_char != "$"
8989 ):
9090
9191 # Skip any leading whitespace characters
9292 while current_char .isspace ():
93- self .__current_position += 1
94- current_char = self .__expr_string [self .__current_position ]
93+ self ._current_position += 1
94+ current_char = self ._expr_string [self ._current_position ]
9595
9696 # Store whatever is next in the current_token string
9797 current_token = ""
@@ -101,18 +101,18 @@ def parse(self):
101101 # First, we need to check for a leading '-' or '+' sign
102102 if current_char == "-" or current_char == "+" :
103103 current_token += current_char
104- self .__current_position += 1
105- current_char = self .__expr_string [self .__current_position ]
104+ self ._current_position += 1
105+ current_char = self ._expr_string [self ._current_position ]
106106
107107 # Now we loop for as long as we have numbers
108108 while current_char in "0123456789" :
109109 current_token += current_char
110- self .__current_position += 1
111- current_char = self .__expr_string [self .__current_position ]
110+ self ._current_position += 1
111+ current_char = self ._expr_string [self ._current_position ]
112112
113113 # We should have a number now - add it to the output string,
114114 # space delimited
115- self .__output_string += current_token + " "
115+ self ._output_string += current_token + " "
116116
117117 # And after every operand, we need to look for an operator
118118 expecting_operand = False
@@ -133,45 +133,45 @@ def parse(self):
133133 # - Pop it and output it.
134134 # - Push the current operator
135135
136- if len (self .__operator_stack ) == 0 :
137- self .__operator_stack .append (current_operator )
136+ if len (self ._operator_stack ) == 0 :
137+ self ._operator_stack .append (current_operator )
138138
139139 else :
140- top_operator = self .__operator_stack [
141- len (self .__operator_stack ) - 1
140+ top_operator = self ._operator_stack [
141+ len (self ._operator_stack ) - 1
142142 ]
143143 while (
144- len (self .__operator_stack ) > 0
144+ len (self ._operator_stack ) > 0
145145 and top_operator .precedence
146146 > current_operator .precedence
147147 ):
148- self .__output_string += top_operator .op_string + " "
149- self .__operator_stack .pop ()
150- if len (self .__operator_stack ) > 0 :
151- top_operator = self .__operator_stack [
152- len (self .__operator_stack ) - 1
148+ self ._output_string += top_operator .op_string + " "
149+ self ._operator_stack .pop ()
150+ if len (self ._operator_stack ) > 0 :
151+ top_operator = self ._operator_stack [
152+ len (self ._operator_stack ) - 1
153153 ]
154154
155- self .__operator_stack .append (current_operator )
155+ self ._operator_stack .append (current_operator )
156156
157157 # Get the next character
158- self .__current_position += 1
159- current_char = self .__expr_string [self .__current_position ]
158+ self ._current_position += 1
159+ current_char = self ._expr_string [self ._current_position ]
160160
161161 # Skip any trailing whitespace characters
162162 while current_char .isspace ():
163- self .__current_position += 1
164- current_char = self .__expr_string [self .__current_position ]
163+ self ._current_position += 1
164+ current_char = self ._expr_string [self ._current_position ]
165165
166166 # After every operator, look for an operand
167167 expecting_operand = True
168168
169169 # At this point, we're done with the string, so we just need to pop
170170 # the remaining operators off the stack
171171
172- while len (self .__operator_stack ) > 0 :
173- top_operator = self .__operator_stack .pop ()
174- self .__output_string += top_operator .op_string + " "
172+ while len (self ._operator_stack ) > 0 :
173+ top_operator = self ._operator_stack .pop ()
174+ self ._output_string += top_operator .op_string + " "
175175
176- self .__evaluated = True
177- return self .__output_string
176+ self ._evaluated = True
177+ return self ._output_string
0 commit comments