-
-
Save jettify/ae805d8e732be07b7b0e to your computer and use it in GitHub Desktop.
Revisions
-
ascv revised this gist
Feb 21, 2014 . 1 changed file with 7 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,12 +37,13 @@ def next(self): def term(self): result = self.factor() while self._current in ('*', '/'): if self._current == '*': self.next() result *= self.term() if self._current == '/': self.next() result /= self.term() return result if __name__ == '__main__': -
ascv revised this gist
Feb 21, 2014 . 1 changed file with 6 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,10 +12,11 @@ def __init__(self, tokens): def exp(self): result = self.term() while self._current in ('+', '-'): if self._current == '+': self.next() result += self.term() if self._current == '-': self.next() result -= self.term() return result @@ -36,10 +37,10 @@ def next(self): def term(self): result = self.factor() while self._current == '*': self.next() result *= self.term() while self._current == '/': self.next() result /= self.term() return result -
ascv revised this gist
Feb 21, 2014 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,12 +11,12 @@ def __init__(self, tokens): def exp(self): result = self.term() while self._current in ('+', '-'): self.next() if result is '+': result += self.term() if result is '-': result -= self.term() return result def factor(self): @@ -55,4 +55,4 @@ def term(self): tokens[-1] += lst[i] else: tokens.append(lst[i]) print Calculator(tokens).exp() -
ascv revised this gist
Feb 24, 2013 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ """ exp ::= term | exp + term | exp - term term ::= factor | factor * term | factor / term factor ::= number | ( exp ) """ @@ -35,13 +35,13 @@ def next(self): self._current = self._tokens[0] if len(self._tokens) > 0 else None def term(self): result = self.factor() while self._current is '*': self.next() result *= self.term() while self._current is '/': self.next() result /= self.term() return result if __name__ == '__main__': -
ascv revised this gist
Feb 24, 2013 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ """ exp ::= term | exp + term | exp - term term ::= factor * term | factor / term | factor factor ::= number | ( exp ) """ @@ -35,21 +35,21 @@ def next(self): self._current = self._tokens[0] if len(self._tokens) > 0 else None def term(self): while self._current is '*': self.next() result *= self.term() while self._current is '/': self.next() result = result / self.term() result = self.factor() return result if __name__ == '__main__': while True: lst = list(raw_input('> ').replace(' ', '')) tokens = [] for i in range(len(lst)): if lst[i].isdigit() and i > 0 and (tokens[-1].isdigit() or tokens[-1][-1] is '.'): tokens[-1] += lst[i] elif lst[i] is '.' and i > 0 and tokens[-1].isdigit(): tokens[-1] += lst[i] -
ascv revised this gist
Feb 24, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -49,7 +49,7 @@ def term(self): lst = list(raw_input('> ').replace(' ', '')) tokens = [] for i in range(len(lst)): if lst[i].isdigit() and i > 0 and (tokens[-1].isdigit() or tokens[-1][-1] is '.'): tokens[-1] += lst[i] elif lst[i] is '.' and i > 0 and tokens[-1].isdigit(): tokens[-1] += lst[i] -
ascv revised this gist
Feb 24, 2013 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -49,10 +49,10 @@ def term(self): lst = list(raw_input('> ').replace(' ', '')) tokens = [] for i in range(len(lst)): if lst[i].isdigit() and i > 0 and (tokens[-1].isdigit() or tokens[-1][-1] is '.'): tokens[-1] += lst[i] elif lst[i] is '.' and i > 0 and tokens[-1].isdigit(): tokens[-1] += lst[i] else: tokens.append(lst[i]) print Calculator(tokens).exp() -
ascv revised this gist
Feb 24, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ """ exp ::= term | exp + term | exp - term term ::= factor | factor * term | factor / term factor ::= number | ( exp ) """ -
ascv revised this gist
Feb 24, 2013 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,8 @@ """ exp ::= term | exp + term | exp - term term ::= factor * term | factor / term | factor factor ::= number | ( exp ) """ class Calculator(): def __init__(self, tokens): -
ascv revised this gist
Feb 24, 2013 . 1 changed file with 1 addition and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,3 @@ class Calculator(): def __init__(self, tokens): @@ -16,7 +11,7 @@ def exp(self): result += self.term() while self._current is '-': self.next() result -= self.term() return result def factor(self): -
ascv revised this gist
Feb 24, 2013 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,7 @@ def exp(self): def factor(self): result = None if self._current[0].isdigit() or self._current[-1].isdigit(): result = float(self._current) self.next() elif self._current is '(': @@ -49,8 +49,10 @@ def term(self): lst = list(raw_input('> ').replace(' ', '')) tokens = [] for i in range(len(lst)): if lst[i].isdigit() and len(tokens) > 0 and (tokens[-1].isdigit() or tokens[-1][-1] is '.'): tokens[-1] += lst[i] elif lst[i] is '.' and len(tokens) > 0 and tokens[-1].isdigit(): tokens[-1] += lst[i] else: tokens.append(lst[i]) print Calculator(tokens).exp() -
ascv revised this gist
Feb 24, 2013 . 1 changed file with 9 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,9 @@ """ exp ::= term | exp + term | exp - term term ::= factor * term | factor / term | factor factor ::= number | ( exp ) """ class Calculator(): def __init__(self, tokens): self._tokens = tokens @@ -32,15 +38,15 @@ def term(self): result = self.factor() while self._current is '*': self.next() result *= self.term() while self._current is '/': self.next() result /= self.term() return result if __name__ == '__main__': while True: lst = list(raw_input('> ').replace(' ', '')) tokens = [] for i in range(len(lst)): if lst[i].isdigit() and len(tokens) > 0 and tokens[-1].isdigit(): -
ascv created this gist
Feb 24, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ class Calculator(): def __init__(self, tokens): self._tokens = tokens self._current = tokens[0] def exp(self): result = self.term() while self._current is '+': self.next() result += self.term() while self._current is '-': self.next() result -= self.term return result def factor(self): result = None if self._current.isdigit(): result = float(self._current) self.next() elif self._current is '(': self.next() result = self.exp() self.next() return result def next(self): self._tokens = self._tokens[1:] self._current = self._tokens[0] if len(self._tokens) > 0 else None def term(self): result = self.factor() while self._current is '*': self.next() result *= self.factor() while self._current is '/': self.next() result /= self.factor() return result if __name__ == '__main__': while True: lst = list(raw_input('> ')) tokens = [] for i in range(len(lst)): if lst[i].isdigit() and len(tokens) > 0 and tokens[-1].isdigit(): tokens[-1] += lst[i] else: tokens.append(lst[i]) print Calculator(tokens).exp()