Skip to content

Instantly share code, notes, and snippets.

@jejones3141
jejones3141 / graze.py
Last active October 14, 2018 21:33
quick hack for 10/12/18 Riddler Classic problem
import random, math
# WLOG, we take the field radius R to be 1 and the stake for the goat's rope
# to be at (1,0).
# random point in the field; sqrt() gives uniformly distributed distance from
# the origin (see https://programming.guide/random-point-within-circle.html;
# thanks, David)
def randPoint():
randr = math.sqrt(random.random())
import math
from scipy.optimize import brentq
import matplotlib.pyplot as plt
# Solve the 10/12/18 Riddler Classic problem on fivethirtyeight.com.
# The following is the result of several steps...
#
# 1. WLOG, we take the field to be a unit circle, and arrange the coordinate
# system so the stake is at (1,0).
# 2. The goat-grazeable region is bounded on the left by the arc corresponding
@jejones3141
jejones3141 / digitprod.py
Last active September 2, 2019 03:09
count iterations/recursive calls to a function that repeatedly evaluates the digit product of a base 10 number representation
from functools import reduce
from operator import mul
# For DRYness's sake, we'll write digitprod here. We have to rull our own prod, since
# Python doesn't have one, but will take Trey Hunner's advice and use operator.mul, which
# gives * a name so we can avoid a lambda.
def digitprod(n):
return reduce(mul, (int(c) for c in str(n)), 1)
# Now for the real action. We repeatedly evaluate the "digit product" of the base 10
@jejones3141
jejones3141 / digitprod.hs
Created September 2, 2019 03:29
Haskell version of code in digitprod.py
{-
Here's a Haskell equivalent of digitprod.py.
Haskell is a non-strict (aka lazy) pure functional programming language,
and that makes it differ from the code in digitprod.py:
- Haskell uses recursion rather than looping, but has higher-order
functions that let you avoid repeating boilerplate.
- Functions are "first-class citizens" in Haskell, so you can create
them and return them and pass them.
- Haskell lets you write functions in "point free" form. If you look,
you'll see that dpReduce and digitProduct make no mention of their
@jejones3141
jejones3141 / riddlerexp.py
Last active January 3, 2020 03:08
solution to 09/20/19 Riddler Express--it's like the 10/12/18 Riddler Classic, just changing the function we're looking for a zero of.
import math
from scipy.optimize import brentq
import scipy.integrate as integrate
import matplotlib.pyplot as plt
# Solve the 09/20/19 Riddler Express problem on fivethirtyeight.com.
# This is much like the 10/12/18 Riddler Classic problem with the goatn
# and the circular pen; perhaps the new Riddler thinks it not all that
# hard, especially with the earlier problem as a guide.
#
@jejones3141
jejones3141 / bday.py
Created October 12, 2019 13:03
Monte Carlo code to aid with solution of the October 4, 2019 fivethirtyeight.com Riddler Classic problem
import random, collections
def bdaySample(size):
sample = collections.Counter()
for i in range(size):
sample[random.randrange(365)] += 1
return max(sample.values())
def probAtLeast(n, size, nSamples):
atLeastN = sum(1 for c in (
@jejones3141
jejones3141 / spellingbee.py
Last active January 19, 2020 02:09
Solution for Jan 3 2020 fivethirtyeight.com Riddler Classic
# Jan 3 2020 Riddler Classic -- find "Spelling Bee" puzzle w/max total word score
# Here we're trying to take Peter Norvig's solution a step further by noting that
# the subsets (independent of a particular honeycomb) can be precalculated, too.
# Does that turn out faster than running combinations()? We'll see.
# We're now representing the center by its index in the honeycomb string.
from collections import Counter
def signature(w):