Skip to content

Instantly share code, notes, and snippets.

View hickford's full-sized avatar

M Hickford hickford

  • Cambridge, United Kingdom
View GitHub Profile
#!python
# -*- coding: utf-8 -*-
# http://blog.jgc.org/2013/04/the-minimum-coin-problem-with-prize.html
# I have in my pocket the following British coins: one £2, two £1, two 50p, three 20p, one 10p, two 5p, two 2p and two 1p. I wish to buy a copy of today's paper at a cost of £1.70.
# What is the maximum number of coins I can use to pay for the paper? With the restriction that I pay in a reasonable manner (e.g. I could give exactly £1.70 or an amount greater than that but without giving extraneous coins: e.g. giving the seller all the coins would not be acceptable, but giving him one £1 an two 50ps and expecting 30p in change is OK). The reasonable manner is simply: if I give the seller a set of coins he should not be able to return any to me without the sum of the remaining coins dropping below £1.70.
from collections import Counter, OrderedDict
def solve_exact(coins, P, f=len):
@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())
@hickford
hickford / OrderedDictionary.cs
Created March 11, 2013 20:19
Ordered dictionary class for C# and .NET (an omission from the standard library). A dictionary that remembers the order that keys were first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end. See http://stackoverflow.com/questions…
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
/// <summary>
/// A dictionary that remembers the order that keys were first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.
/// </summary>
/// <typeparam name="TKey">The type of keys</typeparam>
@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 / 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 / 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 / group-adjacent-by.cs
Created April 1, 2012 16:58
GroupAdjacentBy method for .NET in C#
public static class LINQExtensions
{
public static IEnumerable<IGrouping<TKey, TElement>> GroupAdjacentBy<TElement, TKey>(this IEnumerable<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey> comparer=null)
{
comparer = comparer ?? EqualityComparer<TKey>.Default;
List<TElement> elements = null;
TKey key = default(TKey);
TKey lastKey = default(TKey);
foreach (var x in source)
{
@hickford
hickford / buffer-until-calm.cs
Created March 29, 2012 21:32
BufferUntilCalm method for .NET's Reactive Extensions
public static class ObservableExtensions
{
/// <summary>
/// Group observable sequence into buffers separated by periods of calm
/// </summary>
/// <param name="source">Observable to buffer</param>
/// <param name="calmDuration">Duration of calm after which to close buffer</param>
/// <param name="maxCount">Max size to buffer before returning</param>
/// <param name="maxDuration">Max duration to buffer before returning</param>
public static IObservable<IList<T>> BufferUntilCalm<T>(this IObservable<T> source, TimeSpan calmDuration, Int32? maxCount=null, TimeSpan? maxDuration = null)
@hickford
hickford / example.cs
Created March 8, 2012 23:05
Python's timedelta for .NET and C#
TimeDelta(hours:1,minutes:30)
@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