Guest User

Untitled

a guest
Jul 17th, 2024
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. # Ugly solution to https://news.ycombinator.com/item?id=40989044
  2. $ cat testrun.py
  3. import sys
  4. prio1ops = ('*', '/')
  5. prio2ops = ('+', '-')
  6.  
  7. def compute(tokens):
  8.     while True:
  9.         resolveAt = None
  10.         for i, token in enumerate(tokens):
  11.             # this can be done more elegantly if prio1 and -2 are an array-like type: TODO
  12.             if token in prio1ops:
  13.                 resolveAt = i
  14.                 break
  15.         if resolveAt is None:
  16.             break
  17.         else:
  18.             if i == 0 or i == (len(tokens) - 1):
  19.                 print('operator at the start or end?', tokens)
  20.                 return False
  21.             # so tempting to use eval here, since this is ugly, but not sure what the better solution is
  22.             if tokens[i] == '/':
  23.                 tokens[i - 1] = tokens[i - 1] / tokens[i + 1]
  24.             if tokens[i] == '*':
  25.                 tokens[i - 1] = tokens[i - 1] * tokens[i + 1]
  26.             # so we modified index-1, then delete index and index+1
  27.             del tokens[i]
  28.             # because it shifts, we need to remove the index position twice
  29.             del tokens[i]
  30.  
  31.     # Copied from the whileTrue above, TODO really should merge this into DRY...
  32.     while True:
  33.         resolveAt = None
  34.         for i, token in enumerate(tokens):
  35.             if token in prio2ops:
  36.                 resolveAt = i
  37.                 break
  38.         if resolveAt is None:
  39.             break
  40.         else:
  41.             if i == 0 or i == (len(tokens) - 1):
  42.                 print('operator at the start or end?', tokens)
  43.                 return False
  44.             # so tempting to use eval here, since this is ugly, but not sure what the better solution is
  45.             if tokens[i] == '+':
  46.                 tokens[i - 1] = tokens[i - 1] + tokens[i + 1]
  47.             if tokens[i] == '-':
  48.                 tokens[i - 1] = tokens[i - 1] - tokens[i + 1]
  49.             # so we modified index-1, then delete index and index+1
  50.             del tokens[i]
  51.             # because it shifts, we need to remove the index position twice
  52.             del tokens[i]
  53.  
  54.     if len(tokens) != 1:
  55.         print('Resolved the operators, but we are left with more than one token?', tokens)
  56.         return False
  57.  
  58.     return tokens[0]
  59.  
  60.  
  61. for line in sys.stdin:
  62.     line = line.replace(' ', '').strip()
  63.     charssofar = ''
  64.     tokens = []
  65.     for char in line:
  66.         if char in prio1ops + prio2ops:
  67.             tokens.append(float(charssofar))
  68.             charssofar = ''
  69.             tokens.append(char)
  70.         else:
  71.             charssofar += char
  72.     if len(charssofar) < 1:
  73.         print('ends in an operator?')
  74.         continue
  75.     # TODO wrap float interpreting in try-catches
  76.     tokens.append(float(charssofar))
  77.  
  78.     print(line, '=', compute(tokens))
  79.  
  80.  
  81. $ cat testset
  82. 1 + 1
  83. 1 * 2
  84. 1 + 2 * 2
  85. 1-1
  86. 1/2
  87. 1+2/2
  88.  
  89.  
  90. $ python3 testrun.py <testset
  91. 1+1 = 2.0
  92. 1*2 = 2.0
  93. 1+2*2 = 5.0
  94. 1-1 = 0.0
  95. 1/2 = 0.5
  96. 1+2/2 = 2.0
  97.  
Advertisement
Add Comment
Please, Sign In to add comment