Skip to content

Instantly share code, notes, and snippets.

@lvaughn
Created November 12, 2021 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvaughn/dc0f5a30ee374e2cfe91cd3ecffc3442 to your computer and use it in GitHub Desktop.
Save lvaughn/dc0f5a30ee374e2cfe91cd3ecffc3442 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#
# Answer to the 538 Riddler Express for Nov 12, 2021
# Author: Lowell Vaughn
def generate_roles(dice):
""" Generates a list of lists enumerating all of outcomes of rolling a set of dice of
various numbers of sides. For instance, generate_roles([4, 6, 8]) will return a list
of all outcomes from rolling 4-sided, 6-sized, and 8-sided die (in that order)
"""
if not dice:
yield []
else:
for rest in generate_roles(dice[1:]):
for i in range(dice[0]):
yield [i + 1] + rest
def is_asc(roles):
""" Returns true of a list's values are strictly increasing in size """
return all(a[0] < a[1] for a in zip(roles[:-1], roles[1:]))
def get_probs(dice_sizes):
""" Determines the probability that the outcome of rolling the dice is strictly
increasing for the given set of dice sizes
"""
n_seq = 0
asc_seq = 0
for sequence in generate_roles(dice_sizes):
n_seq += 1
if is_asc(sequence):
asc_seq += 1
return asc_seq, n_seq, asc_seq / n_seq
print("4, 6, 8", get_probs((4, 6, 8)))
print("4, 6, 8, 10, 12, 20", get_probs((4, 6, 8, 10, 12, 20)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment