Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import sys
  3. import math
  4.  
  5. def calc(o1, oper, o2):
  6.    if oper == '+': return o1 + o2
  7.    if oper == '-': return o1 - o2
  8.    if oper == '*': return o1 * o2
  9.    if oper == '/': return o1 // o2
  10.    if oper == '^': return int(math.pow(o1, o2))
  11.  
  12.  
  13. def compare_priority(oper1, oper2):
  14.    priority = {'+':0, '-':0, '/':1, '*':1, '^':2}
  15.    return priority[oper1] - priority[oper2]
  16.  
  17.  
  18. def is_oper(e):
  19.    return e in list("+-/*^")
  20.  
  21.  
  22. def is_paren(e):
  23.    return e in ['(', ')']
  24.  
  25.  
  26. def is_unary(e):
  27.    return e in ['-', '+']
  28.  
  29. def calc_at_idx(parsed_exp, idx):
  30.       new_val = calc(parsed_exp[idx - 3], parsed_exp[idx - 2], parsed_exp[idx - 1])
  31.       del parsed_exp[idx - 3]
  32.       del parsed_exp[idx - 3]
  33.       #print("got", new_val)
  34.       parsed_exp[idx - 3] = new_val
  35.  
  36. expression = sys.argv[1].replace(' ', '')
  37. parsed_exp = []
  38.  
  39. acc = ''
  40. for c in expression:
  41.    if c in '+-*/^()':
  42.        if acc:
  43.           parsed_exp.append(int(acc))
  44.           acc = ''  
  45.        parsed_exp.append(c)
  46.    else:
  47.        acc += c
  48. if acc: parsed_exp.append(int(acc))
  49.  
  50. idx = 0
  51. openp_idx = -1
  52. while len(parsed_exp) > 1:
  53.    #print(parsed_exp)
  54.    #print(idx)
  55.    if parsed_exp[idx] == '(':
  56.       openp_idx = idx
  57.    elif parsed_exp[idx] == ')' and idx - openp_idx >= 4:
  58.       # (14 - 5) -> (9)
  59.       calc_at_idx(parsed_exp, idx)
  60.       idx = -1
  61.    elif parsed_exp[idx] == ')' and idx - openp_idx == 2:
  62.       # (5) -> 5
  63.       del parsed_exp[idx]
  64.       del parsed_exp[idx-2]
  65.       idx = -1
  66.    elif (idx == 0 or parsed_exp[idx-1] == '(') and is_unary(parsed_exp[idx]):
  67.       # unary + or -
  68.       # +5 -> 5
  69.       # -(5+2) -> -1 * (5+2)
  70.       if parsed_exp[idx] == '+':
  71.          del parsed_exp[idx]
  72.          idx -= 1
  73.       else:
  74.          parsed_exp[idx] = '*'
  75.          parsed_exp.insert(idx, -1)
  76.          idx = -1
  77.    elif is_oper(parsed_exp[idx]) and is_oper(parsed_exp[idx-2]):
  78.       # 4*5+1 (calc on a lower priority operand)
  79.       if compare_priority(parsed_exp[idx], parsed_exp[idx-2]) <= 0:
  80.          calc_at_idx(parsed_exp, idx)
  81.          idx = -1
  82.       else:
  83.          pass
  84.    elif idx == len(parsed_exp) - 1 and is_oper(parsed_exp[idx-1]):
  85.       # 5-4 (calc on end of expr)
  86.       calc_at_idx(parsed_exp, idx+1)
  87.       idx = -1
  88.    idx += 1
  89.  
  90. result = parsed_exp[0]
  91. print(expression, '=', result)
  92. if len(sys.argv) == 3:
  93.    if result == int(sys.argv[2]):
  94.       print("PASS")
  95.    else:
  96.       print("FAIL")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement