Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. def complex_evaluation(tokens):
  2. brackets = ["(", "[", "{", ")", "]", "}"]
  3. bracketsclose = [")", "]", "}"]
  4. bracketsopen = ["(", "[", "{"]
  5. brackpos = []
  6. for i in range(len(tokens)):
  7. if tokens[i] in brackets:
  8. brackpos.append([tokens[i], i])
  9. while len(brackpos) != 0:
  10. last = brackpos[0][1]
  11. for i in brackpos:
  12. if i[0] in bracketsclose:
  13. x = simple_evaluation(tokens[last + 1:i[1]])
  14. del tokens[last:i[1] + 1]
  15. tokens.insert(last, x)
  16. last = i[1]
  17. print(tokens)
  18.  
  19.  
  20. complex_evaluation([2, "^", "[", 4, "*", "(", 4, "+", 5, ")", "]", "/", 4])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement