{{ message }}

Instantly share code, notes, and snippets.

# M Hickford hickford

Last active Aug 29, 2015
View howards-goblins.py
 #!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):
View build-numbers-from-atoms.md

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
``````
Last active Aug 29, 2015
How to make 21 from the numbers 1,5,6,7?
View numbers-game.py
 #!python import operator import itertools from fractions import Fraction operations = dict() operations['+'] = operator.add operations['-'] = operator.sub operations['/'] = operator.truediv operations['*'] = operator.mul
Created Jul 7, 2015
Project Euler problem 500
View 500.py
 # 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
Created Jan 23, 2011
test if brackets in string are well-nested
View gist:792036
 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
Created Mar 8, 2012
Python's timedelta for .NET and C#
View example.cs
 TimeDelta(hours:1,minutes:30)
Created Apr 25, 2012
keyboard churn
View keyboard-churn.coffee
 #!/usr/bin/env coffee process.stdin.resume() require('tty').setRawMode(true) history = {} plans = {} spew = () -> console.log(new Date) setInterval spew, 1000
Created May 24, 2012
C# foreach loop with different action for final item
View foreach-final-item.cs
 public static class EnumerableExtensions { /// /// Perform an action on each element of an IEnumerable, optionally specifying a different action for the final item. /// public static void ForEach(this IEnumerable source, Action action, Action finalAction=null) { if (source == null) { throw new ArgumentNullException("source");
Created May 28, 2012
Reactive Extension's Where method reimplemented
View reactive-where.cs
 public static class ObservableExtensions { public static IObservable Where (this IObservable source, Func predicate) { if (source == null) { throw new ArgumentNullException("source"); } if (predicate == null) {
Created Oct 5, 2015
FreeDOS ships with the Colemak keyboard layout (to use, type "keyb co"). This file extracted from FREEDOS\PACKAGES\BASE\KEYBX\source\keydos\Co.key
View 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