Skip to content

Instantly share code, notes, and snippets.

@yourcelf
yourcelf / 2016.py
Last active January 7, 2016 22:14
Find all ways to create "2016" using the numbers 1,2,3,4,5 and basic operations (parentheses, multiplication, addition, subtraction, exponentiation).
from __future__ import division, print_function
from itertools import permutations, combinations_with_replacement, product
def guarded_exponentiation(a,b):
"""
Raise a to the power of b, but only if the result won't be silly large, as
silly largeness slows us way down.
"""
if (a > 1 or a < -1) and b > 1000:
raise OverflowError
#!/usr/bin/env python
# Simulation of the 'Can you cross the river' riddler problem:
# https://fivethirtyeight.com/features/night-falls-a-storm-rolls-in-can-you-cross-the-river/
#
# n
# / | \
# a - b - c
# | | |
# d - e - f
# \ | /