Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- def get_probs_and_ds(calc_string, probs, ds):
- for i in range(len(calc_string)):
- if calc_string[i] == "d":
- n_end = i + 1
- while calc_string[n_end].isdigit():
- n_end += 1
- if n_end == len(calc_string):
- break
- prob = 1 / int(calc_string[i+1:n_end])
- probs[i] = prob
- ds.append(int(calc_string[i+1:n_end]))
- def get_all_strings(ds, rem_string, results, acc=[]):
- if not ds:
- acc.append(rem_string)
- res = eval("".join(acc))
- if res == False:
- res = 0
- elif res == True:
- res = 1
- if res not in results:
- results[res] = 1
- else:
- results[res] += 1
- acc.pop()
- return
- fst = ds[0]
- d_index = rem_string.index("d")
- first_part = rem_string[:d_index]
- last_part = rem_string[d_index + len(str(fst))+1:]
- acc.append(first_part)
- for i in range(fst):
- cur = str(i+1)
- acc.append(cur)
- get_all_strings(ds[1:], last_part, results, acc)
- acc.pop()
- acc.pop()
- def main():
- calc_string = input()
- if calc_string == "":
- return
- probs = [1] * len(calc_string)
- ds = []
- get_probs_and_ds(calc_string, probs, ds)
- if not ds:
- res = eval(calc_string)
- if res == False:
- res = 0
- elif res == True:
- res = 1
- print(res, "100.00")
- return
- all_prob = math.prod(probs) * 100
- results = {}
- get_all_strings(ds, calc_string, results)
- for key in sorted(results.keys()):
- print(key, "{:.2f}".format(round(all_prob * results[key], 2)))
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement