Skip to content

Instantly share code, notes, and snippets.

View hickford's full-sized avatar

M Hickford hickford

  • Cambridge, United Kingdom
View GitHub Profile
#!python3
"""Solves Howard's goblins puzzle using backtracking. https://www.facebook.com/howard.dale.3/posts/10152106759878462?stream_ref=5 """
from collections import deque
class Game:
def __init__(self):
self.goblins = [0]*7
self.history = deque()
def time(self):

If a problem is hard, try solving a simpler version. Here, how to build n from atoms '1', '+' and '*' with a minimal number of 1's.

Make one observation. Suppose an optimal expression for n contains an expression for some smaller number m. Then that expression for m is optimal. Why? If it weren't, we could replace it with a better one, and so improve the expression for n. Contradiction.

Aha! This means we can attack the problem with dynamic programming. Assuming we have optimal expressions for all numbers smaller than n, we can deduce an optimal expression for n. How? The expression for n must end either with addition (n-1) + 1 or with multiplication d * (n/d) where d divides n. Inserting optimal expressions for n-1 and n/d , we compute the cost of the expressions for n, and choose the cheapest.

In code

best = MinDict() # optimal expression by number

best[1] = (1, "1", False) # cost, expression, needs brackets

@hickford
hickford / 500.py
Created July 7, 2015 12:50
Project Euler problem 500
# https://projecteuler.net/problem=500 Project Euler problem 500
"""The number of divisors of 120 is 16.
In fact 120 is the smallest number having 16 divisors.
Find the smallest number with 2**500500 divisors.
Give your answer modulo 500500507."""
from codejamhelpers import primes
import heapq
@hickford
hickford / gist:792036
Created January 23, 2011 12:20
test if brackets in string are well-nested
def f(word):
open = 0
for x in word:
if x == "(":
open += 1
elif x == ")":
open += -1
if open < 0:
return False
return open==0
@hickford
hickford / example.cs
Created March 8, 2012 23:05
Python's timedelta for .NET and C#
TimeDelta(hours:1,minutes:30)
@hickford
hickford / keyboard-churn.coffee
Created April 25, 2012 22:35
keyboard churn
#!/usr/bin/env coffee
process.stdin.resume()
require('tty').setRawMode(true)
history = {}
plans = {}
spew = () -> console.log(new Date)
setInterval spew, 1000
@hickford
hickford / foreach-final-item.cs
Created May 24, 2012 13:06
C# foreach loop with different action for final item
public static class EnumerableExtensions
{
/// <summary>
/// Perform an action on each element of an IEnumerable<T>, optionally specifying a different action for the final item.
/// </summary>
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action, Action<T> finalAction=null)
{
if (source == null)
{
throw new ArgumentNullException("source");
@hickford
hickford / reactive-where.cs
Created May 28, 2012 16:15
Reactive Extension's Where method reimplemented
public static class ObservableExtensions
{
public static IObservable<TSource> Where<TSource> (this IObservable<TSource> source, Func<TSource,bool> predicate)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (predicate == null)
{
@hickford
hickford / Co.key
Created October 5, 2015 18:43
FreeDOS ships with the Colemak keyboard layout (to use, type "keyb co"). This file extracted from FREEDOS\PACKAGES\BASE\KEYBX\source\keydos\Co.key
[KEYS:kcommon]
18S 33/f 33/F 33/#6 33/#0 33/#0
19S 25/p 25/P 25/#16 25/#0 25/#0
20S 34/g 34/G 34/#7 34/#0 34/#0
21S 36/j 36/J 36/#10 36/#0 36/#0
22S 38/l 38/L 38/#12 38/#0 38/#0
23S 22/u 22/U 22/#21 22/#0 22/#0
24S 21/y 21/Y 21/#25 21/#0 21/#0
25S 39/#59 39/: 39/#0 39/#0 39/#0
31S 19/r 19/R 19/#18 19/#0 19/#0
@hickford
hickford / osmos.py
Created May 7, 2013 09:51
Attempted solution to Google Code Jam's Osmos problem. Turned out to be incorrect. https://code.google.com/codejam/contest/2434486/dashboard
#!python
# https://code.google.com/codejam/contest/2434486/dashboard
# Armin is playing Osmos, a physics-based puzzle game developed by Hemisphere Games. In this game, he plays a "mote", moving around and absorbing smaller motes.
# lemma if we delete a mote size x, we should delete all motes greater than size x
import fileinput
f = fileinput.input()
T = int(f.readline())