Advertisement
Guest User

Untitled

a guest
Aug 21st, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. import math
  2.  
  3.  
  4. def get_probs_and_ds(calc_string, probs, ds):
  5.     for i in range(len(calc_string)):
  6.         if calc_string[i] == "d":
  7.             n_end = i + 1
  8.             while calc_string[n_end].isdigit():
  9.                 n_end += 1
  10.                 if n_end == len(calc_string):
  11.                     break
  12.             prob = 1 / int(calc_string[i+1:n_end])
  13.             probs[i] = prob
  14.             ds.append(int(calc_string[i+1:n_end]))
  15.  
  16.  
  17. def get_all_strings(ds, rem_string, results, acc=[]):
  18.     if not ds:
  19.         acc.append(rem_string)
  20.         res = eval("".join(acc))
  21.         if res == False:
  22.             res = 0
  23.         elif res == True:
  24.             res = 1
  25.         if res not in results:
  26.             results[res] = 1
  27.         else:
  28.             results[res] += 1
  29.         acc.pop()
  30.         return
  31.     fst = ds[0]
  32.  
  33.     d_index = rem_string.index("d")
  34.  
  35.     first_part = rem_string[:d_index]
  36.     last_part = rem_string[d_index + len(str(fst))+1:]
  37.  
  38.     acc.append(first_part)
  39.  
  40.     for i in range(fst):
  41.         cur = str(i+1)
  42.         acc.append(cur)
  43.         get_all_strings(ds[1:], last_part, results, acc)
  44.         acc.pop()
  45.     acc.pop()
  46.  
  47.  
  48. def main():
  49.     calc_string = input()
  50.  
  51.     if calc_string == "":
  52.         return
  53.  
  54.     probs = [1] * len(calc_string)
  55.  
  56.     ds = []
  57.  
  58.     get_probs_and_ds(calc_string, probs, ds)
  59.  
  60.     if not ds:
  61.         res = eval(calc_string)
  62.         if res == False:
  63.             res = 0
  64.         elif res == True:
  65.             res = 1
  66.         print(res, "100.00")
  67.         return
  68.  
  69.     all_prob = math.prod(probs) * 100
  70.  
  71.     results = {}
  72.  
  73.     get_all_strings(ds, calc_string, results)
  74.  
  75.     for key in sorted(results.keys()):
  76.         print(key, "{:.2f}".format(round(all_prob * results[key], 2)))
  77.  
  78.  
  79. if __name__ == "__main__":
  80.     main()
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement