Skip to content

Instantly share code, notes, and snippets.

Ben Hoyt benhoyt

Block or report user

Report or block benhoyt

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
benhoyt /
Created Nov 3, 2016
Test how many threads we can run at once
"""Test how many threads we can run at once."""
import itertools
import threading
import time
import sys
import requests
benhoyt /
Last active May 20, 2018
Calculate the average number of moves in a snakes and ladders game
"""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
Played 10000 rounds, averaged 36.0559 moves, max 324 moves, took 0.508s
benhoyt /
Created Aug 5, 2016
"Birthday problem" calculator in Python
"""Calculate the probability of generating a duplicate random number after
generating "n" random numbers in the range "d".
Usage: python 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 /
Created Aug 3, 2016
An atomic, thread-safe incrementing counter for Python
"""An atomic, thread-safe incrementing counter."""
import threading
class AtomicCounter:
"""An atomic, thread-safe incrementing counter.
>>> counter = AtomicCounter()
>>> counter.increment()
benhoyt /
Created Jul 28, 2016
Python function to generate a random string (key) of length chars
"""Function to generate a random string (key) of length chars."""
import binascii
import os
def generate_key(length=40, get_bytes=os.urandom):
"""Return a randomly-generated key of length chars.
>>> len(generate_key())
benhoyt /
Created May 12, 2016
Print most frequent N-grams in given file
"""Print most frequent N-grams in given file.
Usage: python 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).
benhoyt / gist:4044946
Created Nov 9, 2012
Speed up os.walk() significantly by using file attributes from FindFirst/Next or readdir
View gist:4044946
"""Speed up os.walk() significantly by using file attributes that
FindFirst/Next give us instead of doing an extra stat(). Can also do the same
thing with opendir/readdir on Linux.
This is doubly useful when the user (caller of os.walk) is doing *another*
stat() to get say the file sizes.
On my tests (Windows 64-bit) our walk() is about 5x as fast as os.walk() for
large directory trees, and 9x as fast if you're doing the file size thing.
Note that these timings are "once it's in the cache", not first-time timings.
benhoyt / gist:3870305
Created Oct 11, 2012
Bouncing ball with rotation
View gist:3870305
"""Simple bouncing ball demo."""
import sys
import pygame
size = (1024, 768)
speed = [1, 1]
benhoyt / gist:2647005
Created May 9, 2012
Speed of list comprehension vs for loop with append
View gist:2647005
# On my Windows 7 64-bit machine running CPython 2.6
C:\>python -m timeit -s "lst = [1] * 100" "out = [x for x in lst if x]"
100000 loops, best of 3: 6.55 usec per loop
C:\>python -m timeit -s "lst = [1] * 100" "out = []" "for x in lst:" " if x:" " out.append(x)"
100000 loops, best of 3: 14.7 usec per loop
You can’t perform that action at this time.