This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): |