Bad_Programist

Untitled

Jan 27th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. def plus(B, j):
  2.     return B[j + 1] + B[j + 2]
  3. def minus(B, j):
  4.     return B[j + 2] - B[j + 1]
  5.  
  6. A = input().split()
  7. B = A[::-1]
  8. S = 0
  9. i = 0
  10. for j in range(len(B)):
  11.     if B[j] != '+' and B[j] != '-' and B[j] != '*':
  12.         B[j] = int(B[j])
  13. while i < len(B):
  14.     if B[i] == '+':
  15.         S += plus(B, i)
  16.         i += 3
  17.     elif B[i] == '-':
  18.         S += minus(B, i)
  19.         i += 3
  20.     elif B[i] == '*':
  21.         if B[i + 1] == '+' and B[i + 4] == '+':
  22.             S += plus(B, i + 1) * plus(B, i + 4)
  23.             i += 7
  24.         elif B[i + 1] == '-' and B[i + 4] == '+':
  25.             S += minus(B, i + 1) * plus(B, i + 4)
  26.             i += 7
  27.         elif B[i + 1] == '+' and B[i + 4] == '-':
  28.             S += plus(B, i + 1) * minus(B, i + 4)
  29.             i += 7
  30.         elif B[i + 1] == '-' and B[i + 4] == '-':
  31.             S += minus(B, i + 1) * minus(B, i + 4)
  32.             i += 7
  33.         elif B[i + 2] == '+':
  34.             S += plus(B, i + 2) * B[i + 1]
  35.             i += 5
  36.         elif B[i + 2] == '-':
  37.             S += minus(B, i + 2) * B[i + 1]
  38.             i += 5
  39.     else:
  40.         S += B[i]
  41.         i += 1
  42. print(S)
Advertisement
Add Comment
Please, Sign In to add comment