Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python3
# Code to solve the "Riddler Classic" from 4/13/2018
# https://fivethirtyeight.com/features/can-you-find-the-perfect-poker-hand-can-you-shut-this-infernal-box/
#
# This code aims to come up with an approximation for the answer by using a
# Monte Carlo simulation if millions of runs of dice rolls and seeing if a perfect
# player could with them (in this case, playing perfectly means that there is any
# way to win, to do this in reality would involve being psychic)
#!/usr/bin/env python
import random
TRIES = 10000000
MORNING_RAIN_PROB = .5
EVENING_RAIN_PROB = .4
def is_raining(prob):
return random.random() < prob
#!/usr/bin/env python
# Solves the Riddler Express at https://fivethirtyeight.com/features/in-space-no-one-can-hear-your-3d-printer-die/
BASE_NUMBER = '530,131,801,762,787,739,802,889,792,754,109,70_,139,358,547,' + \
'710,066,257,652,050,346,294,484,433,323,974,747,960,297,803,' + \
'292,989,236,183,040,000,000,000'
# Try each of the digits in turn, and try to factor out numbers between 99 and 2
# If you get down to remainder greater than 1, then you can't get that number by
# multiplying numbers between 1 and 99. You need to go in reverse to avoid
#!/usr/bin/env python
from collections import defaultdict
GAMES_FILE = 'womens.txt'
WINNER = 'Baylor'
# Maps team name to a set of teams they lost to
beat_by = defaultdict(set)
#!/usr/bin/env python
# A solution for FiveThirtyEight's Riddler for 2019.05.10
# https://fivethirtyeight.com/features/can-the-riddler-bros-beat-joe-dimaggios-hitting-streak/
#
# Adapted from solution at https://www.askamathematician.com/2010/07/q-whats-the-chance-of-getting-a-run-of-k-successes-in-n-bernoulli-trials-why-use-approximations-when-the-exact-answer-is-known/
#
# Usage: hit_streak.py batting_average number_of_seasons
import sys
#!/usr/bin/env python
#
# Solution for https://fivethirtyeight.com/features/can-you-help-dakota-jones-raid-the-lost-arc/
#
# This is a "cheap and cheerful" solution. You could probably do something faster
# using dynamic programing, but this works in a reasonable amount of time.
from collections import defaultdict
# Abbreviation list from the USPS https://pe.usps.com/text/pub28/28apb.htm
@lvaughn
lvaughn / best_husband.py
Created November 2, 2019 23:59
Solution to fivethirtyeight.com riddler for the sultan selecting a husband
#!/usr/bin/env python
#
# This solves the "Riddler Classic" problem from
# https://fivethirtyeight.com/features/how-long-is-the-snails-slimy-trail/
#
# It's brute force, but runs in 33 seconds on a 5 year old MacBook Pro,
# so I'm calling it good enough
import itertools
@lvaughn
lvaughn / spelling_bee.py
Created January 4, 2020 05:12
Solution for fivethirtyeight.com's Riddler for 2020-01-03
#!/usr/bin/env python3
#
# Solution to this week's "Riddler classic"
#
# https://fivethirtyeight.com/features/can-you-solve-the-vexing-vexillology/
#
# The general approach is to create a mapping from all of the words in the list
# and index them by their set of unique letters. Then for every possible lattice
# find all the subsets and find the sum of all the scores for each subset. Then
# just find the highest score.
#!/usr/bin/env python3
#
# My solution to https://fivethirtyeight.com/features/can-you-find-a-number-worth-its-weight-in-letters/
from fractions import Fraction
import sys
# Since 1/2020 == 2/4040 and 1/2019==2/4038, 2/4039 is between them, so
# 4039 is an upper bound
#!/usr/bin/env python3
#
# My solution to the 538 Riddler for Feb 7, 2020
#
# https://fivethirtyeight.com/features/how-many-more-palindrome-dates-will-you-see/
#
# The basic approach is to recursively work out all of the possible ways you can parse the
# string and have the |'s balance out
#
# E.g., |-1|-2|-3| can evaluate to two different values