fork download
  1. import collections, itertools, re
  2.  
  3. expr = 'd4 + (d6 > 2)'
  4. variables = set(re.findall(r'd(\d+)', expr))
  5.  
  6. cnt = collections.defaultdict(int)
  7. total = 0
  8. for values in itertools.product(*[range(1, int(bound) + 1) for bound in variables]):
  9. total += 1
  10. tmp = expr
  11. for name, value in zip(variables, values):
  12. tmp = re.sub(r'd' + name, str(value), tmp)
  13. cnt[eval(tmp)] += 1
  14.  
  15. for value in sorted(cnt):
  16. print (f'{value} {100 * cnt[value]/total:.2f}')
Success #stdin #stdout 0.02s 9564KB
stdin
Standard input is empty
stdout
1 8.33
2 25.00
3 25.00
4 25.00
5 16.67