Skip to content

Instantly share code, notes, and snippets.

@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)
@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 / f#-google-code-jam.md
Last active May 23, 2018
An appeal to support F# programming language in Google Code Jam
View f#-google-code-jam.md

To post at https://groups.google.com/forum/#!forum/google-code

An appeal to support F# programming language

"Though we no longer accept solutions in all programming languages, we have aimed to support the most popular programming languages used in past Code Jam contests, and we are continuously exploring opportunities to expand this list in the future." https://codejam.withgoogle.com/codejam/resources/faq#languages

Dear Google,

Please consider adding support for the programming language F# in future Google Code Jam contests. To quote https://fsharp.org/ "F# is a mature, open source, cross-platform, functional-first programming language. It empowers users and organizations to tackle complex computing problems with simple, maintainable and robust code." We also have a great community! https://hackernoon.com/reflecting-on-f-in-2017-5ac67fb138ff

View element-words.py
#!python
elements = "H He Li Be B C N O F Ne Na Mg Al Si P S Cl Ar K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe Cs Ba La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn Fr Ra Ac Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No Lr Rf Db Sg Bh Hs Mt Ds Rg Cn Uut Fl Mc Lv Ts Og".split()
def decompose(word):
"""Express given word as chemical compound. If there are multiple solutions, return one of minimal weight."""
progress = [False for x in range(len(word)+1)] # solution for word[:i]
progress[0] = []
for i in range(1, len(word)+1):
possibles = list()
View pizza.py
"""I'm thinking of a ten-digit integer whose digits are all distinct. It happens that the number formed by the first n of them is divisible by n for each n from 1 to 10. What is my number?
http://blog.pizzahut.com/flavor-news/national-pi-day-math-contest-problems-are-here-2/ """
digits = [None] * 10
def pretty(A):
return "".join(str(x) if x != None else "-" for x in A)
assert pretty([5, None, 3]) == "5-3"
View sortingnetworks.py
def comparator(x, i, j):
"""Swap x[i] and x[j] if they are out of order"""
if x[i] > x[j]:
x[i], x[j] = x[j], x[i]
def oddevenmergesort(x, indexes=None):
"""In-place odd-even mergesort, applied to slice of x defined by indexes. Assumes len(x) is a power of 2. """
if indexes == None:
indexes = range(len(x))
n = len(indexes)
@hickford
hickford / layout.ini
Last active Dec 24, 2015
If your Windows keyboard layout is Colemak UK and your colleague would like to type in Qwerty UK, run this PKL script.
View layout.ini
; Keyboard Layout definition for
; Portable Keyboard Layout
; http://pkl.sourceforge.net
[informations]
layoutname = ColemakUk
layoutcode = ColemakUk
localeid = 00000809
[global]
@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
You can’t perform that action at this time.