Created
October 2, 2014 00:15
-
-
Save benjic/42ee84cc41e4c033f522 to your computer and use it in GitHub Desktop.
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 characters
| def addition(a,b): | |
| try: | |
| return a + b | |
| except TypeError: | |
| print("Parameters must be numeric") | |
| def subtraction(a,b): | |
| try: | |
| return b - a | |
| except TypeError: | |
| print("parameters must be numeric") | |
| def multiplication(a,b): | |
| try: | |
| return a * b | |
| except TypeError: | |
| print("Parameters must be numeric") | |
| def division(a,b): | |
| try: | |
| return b / a | |
| except TypeError: | |
| print("Parameters must be numeric") | |
| except ZeroDivisionError: | |
| print("Dividing by zero is not defined.") | |
| if __name__ == "__main__": | |
| integers = [] | |
| floats = [] | |
| integers.append(addition(4,12)) | |
| integers.append(subtraction(4,12)) | |
| integers.append(multiplication(4,12)) | |
| integers.append(division(4,12)) | |
| floats.append(addition(3.5,1.2)) | |
| floats.append(subtraction(3.5,1.2)) | |
| floats.append(multiplication(3.5,1.2)) | |
| floats.append(division(3.5,1.2)) | |
| print("The list of results of integer operations on 4, 12") | |
| print(integers) | |
| print("The list of results of floating point operation on 3.5 and 1.2") | |
| print(floats) | |
| division(1,0) |
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 characters
| import arithmetic | |
| import expression | |
| from stack import Stack | |
| while True: | |
| user_input = raw_input('> ').strip() | |
| if user_input == 'quit': | |
| break | |
| postfix = expression.convert_postfix(user_input) | |
| print("Postfix %s" % (' '.join(postfix))) | |
| values = Stack() | |
| for token in postfix: | |
| try: | |
| token = int(token) | |
| values.push(token) | |
| except ValueError: | |
| try: | |
| token = float(token) | |
| values.push(token) | |
| except ValueError: | |
| if token == "+": | |
| value = arithmetic.addition(values.pop(), values.pop()) | |
| elif token == "-": | |
| value = arithmetic.subtraction(values.pop(), values.pop()) | |
| elif token == "/" or token == "\\": | |
| value = arithmetic.division(values.pop(), values.pop()) | |
| elif token == "*": | |
| value = arithmetic.multiplication(values.pop(), values.pop()) | |
| else: | |
| print("Unreconized operator %s" % (token)) | |
| continue | |
| values.push(value) | |
| print(values.pop()) |
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 characters
| import re | |
| from stack import Stack | |
| from collections import deque | |
| def convert_postfix(infix_string): | |
| tokens = tokenize(infix_string) | |
| output = Stack() | |
| operators = Stack() | |
| operator_pattern = re.compile(r'[\+\-\*\/\(\)]') | |
| operator_weights = { | |
| '+': 1, | |
| '-': 1, | |
| '*': 2, | |
| '/': 2 } | |
| for token in tokens: | |
| if operator_pattern.match(token): | |
| while not operators.isEmpty(): | |
| try: | |
| if operator_weights[token] < operator_weights[operators.peek()]: | |
| output.push(operators.pop()) | |
| else: | |
| break | |
| except KeyError: | |
| break | |
| if token == ')': | |
| while not operators.isEmpty() and not operators.peek() == '(': | |
| output.push(operators.pop()) | |
| if not operators.isEmpty(): | |
| operators.pop() | |
| else: | |
| operators.push(token) | |
| else: | |
| output.push(token) | |
| while not operators.isEmpty(): | |
| output.push(operators.pop()) | |
| # Build postfix expression | |
| postfix = deque() | |
| while not output.isEmpty(): | |
| postfix.appendleft(output.pop()) | |
| return postfix | |
| def tokenize(expression_string): | |
| digit_pattern = re.compile(r'[0-9.]') | |
| ws_pattern = re.compile(r'\s') | |
| tokens = [] | |
| in_number = False | |
| j,k = 0,0 | |
| for i in range(len(expression_string)): | |
| char = expression_string[i] | |
| if in_number: | |
| if not digit_pattern.match(char): | |
| tokens.append(expression_string[j:i]) | |
| in_number = False | |
| if not ws_pattern.match(char): | |
| tokens.append(char) | |
| elif i == len(expression_string)-1: | |
| tokens.append(expression_string[j:i]) | |
| else: | |
| if digit_pattern.match(char): | |
| in_number = True | |
| j = i | |
| elif not ws_pattern.match(char): | |
| tokens.append(char) | |
| if i == len(expression_string)-1: | |
| tokens.append(expression_string[i]) | |
| return tokens | |
| if __name__ == "__main__": | |
| print("( 2342.324 + 234 ) + ( 2 - 932.23 ) / 23 * 5") | |
| evaluate_string("( 2342.324 + 234 ) + ( 2 - 932.23 ) / 23 * 5") | |
| print("( 1 * 2 ) + ( 3 / ( 1 - 2 ) ) * 5") | |
| evaluate_string("( 1 * 2 ) + ( 3 / ( 1 - 2 ) ) * 5") |
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 characters
| # Python implementation of a Stack | |
| # End of the list will hold the top element of the stack - think about why? | |
| class Stack: | |
| def __init__(self): | |
| self.items = [] | |
| def isEmpty(self): | |
| return self.items == [] | |
| def push(self,item): | |
| self.items.append(item) | |
| def pop(self): | |
| return self.items.pop() | |
| def peek(self): | |
| return self.items[len(self.items)-1] | |
| def size (self): | |
| return len(self.items) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment