Skip to content

Instantly share code, notes, and snippets.

@hickford
hickford / alice.txt
Last active Dec 17, 2015
Unified diff example `diff -u alice.txt bob.txt > unified-diff.txt`
View alice.txt
Alice was beginning to get very tired of sitting by her sister on the
bank, and of having nothing to do: once or twice she had peeped into the
book her sister was reading, but it had no pictures or conversations in
it, 'and what is the use of a book,' thought Alice 'without pictures or
conversation?'
View essay.txt
alicewasbeginningtogetverytiredofsittingbyhersisteronthe
bankandofhavingnothingtodoonceortwiceshehadpeepedintothe
bookhersisterwasreadingbutithadnopicturesorconversationsin
itandwhatistheuseofabookthoughtalicewithoutpicturesor
conversation
soshewasconsideringinherownmindaswellasshecouldforthe
hotdaymadeherfeelverysleepyandstupidwhetherthepleasure
ofmakingadaisychainwouldbeworththetroubleofgettingupand
pickingthedaisieswhensuddenlyawhiterabbitwithpinkeyesran
View coins.py
#!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
Attempted solution to Google Code Jam's Osmos problem. Turned out to be incorrect. https://code.google.com/codejam/contest/2434486/dashboard
View osmos.py
#!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 Mar 11, 2013
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…
View OrderedDictionary.cs
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
Reactive Extension's Where method reimplemented
View reactive-where.cs
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
C# foreach loop with different action for final item
View foreach-final-item.cs
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");
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
@hickford
hickford / group-adjacent-by.cs
Created Apr 1, 2012
GroupAdjacentBy method for .NET in C#
View group-adjacent-by.cs
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 Mar 29, 2012
BufferUntilCalm method for .NET's Reactive Extensions
View buffer-until-calm.cs
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)
You can’t perform that action at this time.