Skip to content

Instantly share code, notes, and snippets.

Ben Hoyt benhoyt

View GitHub Profile
@benhoyt
benhoyt / ngrams.py
Created May 12, 2016
Print most frequent N-grams in given file
View ngrams.py
"""Print most frequent N-grams in given file.
Usage: python ngrams.py filename
Problem description: Build a tool which receives a corpus of text,
analyses it and reports the top 10 most frequent bigrams, trigrams,
four-grams (i.e. most frequently occurring two, three and four word
consecutive combinations).
NOTES
@benhoyt
benhoyt / flattenjson.go
Created Jul 7, 2020
Little Go tool to flatten JSON input
View flattenjson.go
// Flatten JSON input
//
// Example:
//
// $ echo '{"user":"Ben","ints":[true,false,null],"sub":{"x":1,"y":2}}' | go run flattenjson.go
// _.ints[0] = true
// _.ints[1] = false
// _.ints[2] = null
// _.sub.x = 1
// _.sub.y = 2
@benhoyt
benhoyt / birthday_probability.py
Created Aug 5, 2016
"Birthday problem" calculator in Python
View birthday_probability.py
"""Calculate the probability of generating a duplicate random number after
generating "n" random numbers in the range "d".
Usage: python birthday_probability.py n [d=365]
Each value can either be an integer directly, or in the format "2**x", where
x is the number of bits in the value.
For example, to calculate the probability that two people will have the same
birthday in a room with 23 people:
@benhoyt
benhoyt / atomic_counter.py
Created Aug 3, 2016
An atomic, thread-safe incrementing counter for Python
View atomic_counter.py
"""An atomic, thread-safe incrementing counter."""
import threading
class AtomicCounter:
"""An atomic, thread-safe incrementing counter.
>>> counter = AtomicCounter()
>>> counter.increment()
@benhoyt
benhoyt / python-stdlib.md
Created Oct 17, 2019
Overview of (parts of) the Python standard library
View python-stdlib.md

I'm going to demo a bunch of Python builtin and stdlib functions. There's a lot to get through, so I'll be going fast, but please stop me and ask questions as we go. The goal is to give you a taste of Python's power and expressivity if you're not a Python person, or maybe teach you a few new tricks if you are already.

Built-in functions

# enumerate: iterate with index *and* item
>>> strings = ['123', '0', 'x']
>>> for i, s in enumerate(strings):
...     print(f'{i} - {s}')  # f-strings!
@benhoyt
benhoyt / edit_distance.py
Created May 8, 2018
Calculate edit distance with simple (memoized) recursive algorithm
View edit_distance.py
def distance(s, t, cache=None):
"""Return minimum edit distance between s and t, where an edit
is a character substitution, deletion, or addition.
"""
if not s:
return len(t)
if not t:
return len(s)
if cache is None:
@benhoyt
benhoyt / join.awk
Created Nov 20, 2018
AWK program to compare time complexity of joining strings
View join.awk
# AWK program to compare time complexity of joining strings using a
# simple O(N^2) algorithm and a slightly more complex O(N log N) one.
# Join array elements, separated by sep: O(N^2) version
function join1(a, sep, i, s) {
for (i = 1; i+1 in a; i++) {
s = s a[i] sep
}
if (i in a) {
s = s a[i]
@benhoyt
benhoyt / mandelbrot.go
Created Sep 21, 2018
Go program to print the Mandelbrot set on stdout
View mandelbrot.go
// Print the Mandelbrot set on stdout
package main
import (
"fmt"
"math/cmplx"
)
const (
@benhoyt
benhoyt / thread_test.py
Created Nov 3, 2016
Test how many threads we can run at once
View thread_test.py
"""Test how many threads we can run at once."""
import itertools
import threading
import time
import sys
import requests
@benhoyt
benhoyt / snakes_and_ladders.py
Last active May 20, 2018
Calculate the average number of moves in a snakes and ladders game
View snakes_and_ladders.py
"""Calculate the average number of moves in a snakes and ladders game.
Because as a parent one gets roped into these board (boring?) games
every so often, and I wanted to calculate the average duration of a
snakes and ladders game. Turns out it's about 36 moves (though
admittedly that's for a single-player game). :-)
> python snakes_and_ladders.py
Played 10000 rounds, averaged 36.0559 moves, max 324 moves, took 0.508s
"""
You can’t perform that action at this time.