Guest User

Untitled

a guest
Oct 28th, 2020 (edited)
21
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def iter_exprs(num_operands=None):
  2. if num_operands is None:
  3. i = 1
  4. while True:
  5. yield from iter_exprs(i)
  6. i += 1
  7. else:
  8. if num_operands == 1:
  9. yield "1"
  10. else:
  11. for i in range(1, num_operands):
  12. for left in iter_exprs(i):
  13. for right in iter_exprs(num_operands-i):
  14. for operator in ["+", "*"]:
  15. yield f"({left}{operator}{right})"
  16. for expr in iter_exprs(num_operands):
  17. yield expr + "!"
  18.  
  19.  
  20. for x in iter_exprs():
  21. print(x)
RAW Paste Data