Skip to content

Instantly share code, notes, and snippets.

View hughdbrown's full-sized avatar

Hugh Brown hughdbrown

View GitHub Profile
@hughdbrown
hughdbrown / random_numbers.py
Last active October 10, 2024 17:25
Test various strategies for creating arrays of random numbers, some with serial dependencies
import os
os.environ["NUMBA_LOOP_VECTORIZE"] = "0"
os.environ["NUMBA_SLP_VECTORIZE"] = "0"
from datetime import datetime
from numba import jit, uint32, uint64
import numpy as np
@hughdbrown
hughdbrown / top_sort.py
Last active August 12, 2024 21:04
Try graphlib.TopologicalSort
>>> from graphlib import TopologicalSorter
>>> data = {"A": ["B", "D"], "B": [], "D": [], "C": ["A"]}
>>> ts = TopologicalSorter(data)
>>> for a in ts.static_order():
... print(a)
...
B
D
A
@hughdbrown
hughdbrown / common-logging.py
Created August 5, 2024 16:03
Common python logging preamble
import logging
FORMAT = '%(asctime)s %(message)s'
logging.basicConfig(
format=FORMAT,
datefmt="%Y-%m-%dT%H:%M:%S",
# logger levels are: DEBUG, INFO, WARNING, ERROR, CRITICAL
level=os.environ.get('LOGLEVEL', 'INFO').upper(),
)
logger = logging.getLogger()
@hughdbrown
hughdbrown / who-is-hiring.txt
Created August 2, 2024 17:20
Count of "Who is hiring?" posts on Hacker News by month
-------------------- 2019-01-01 hacker-news-2019-01-01.txt: 782
-------------------- 2019-02-01 hacker-news-2019-02-01.txt: 702
-------------------- 2019-03-01 hacker-news-2019-03-01.txt: 759
-------------------- 2019-04-01 hacker-news-2019-04-01.txt: 757
-------------------- 2019-05-01 hacker-news-2019-05-01.txt: 829
-------------------- 2019-06-01 hacker-news-2019-06-01.txt: 675
-------------------- 2019-07-01 hacker-news-2019-07-01.txt: 767
-------------------- 2019-08-01 hacker-news-2019-08-01.txt: 741
-------------------- 2019-09-01 hacker-news-2019-09-01.txt: 615
-------------------- 2019-10-01 hacker-news-2019-10-01.txt: 733
@hughdbrown
hughdbrown / pokemon-grid.py
Created May 30, 2024 15:22
Take a single image and resale to pave it in a 3x3 grid, save new image
#!/usr/bin/env python3
from pathlib import Path
from PIL import Image, ImageChops
def trim(im):
bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
diff = ImageChops.difference(im, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
@hughdbrown
hughdbrown / billiards.py
Created May 22, 2024 21:13
Why the solution given by the FDP Institute for the first question in the 2023 exam is incorrect
#!/usr/bin/env python3
# This is a simulation of the results for the first problem given here:
# https://fdpinstitute.org/resources/FDP%203.0/2024-Q2/Limited%20Sample%20MC%20Questions%20Q2-2024.pdf
#
# The expected answer given by the FDP Institute is 0.84. This is incorrect, as this simulation shows.
# The problem is that FDP has used the Bayesian formula incorrectly. Instead of using a weighted average
# of outcomes as the prior probability, FDP uses only the P(potted|~Simon) as the prior.
from collections import Counter
@hughdbrown
hughdbrown / spiral.py
Last active May 2, 2024 17:52
Code to write spiral of integers in a square
from itertools import cycle
def spiral(n):
matrix = [[0] * n for _ in range(n)]
x, y = 0, 0
x0, y0, xn, yn = 0, 0, n, n
directions = cycle([
(1, 0, (0, 0, 1, 0)),
(0, 1, (0, -1, 0, 0)),
(-1, 0, (0, 0, 0, -1)),
@hughdbrown
hughdbrown / filemode.md
Created January 15, 2024 19:07
Git filemodes
❯ ls -alrt
total 0
drwxr-xr-x  152 hughbrown  staff  4864 Jan 15 12:00 ..
drwxr-xr-x    9 hughbrown  staff   288 Jan 15 12:00 .git
-rw-r--r--    1 hughbrown  staff     0 Jan 15 12:00 x644
-rwxr-xr-x    1 hughbrown  staff     0 Jan 15 12:01 x755
-rw-------    1 hughbrown  staff     0 Jan 15 12:01 x600
drwxr-xr-x    6 hughbrown  staff   192 Jan 15 12:01 .
@hughdbrown
hughdbrown / bloom_filter.py
Created December 2, 2023 15:53
Bloom filter implementation from medium.com article. Seems broken.
import math
import hashlib
from bitarray import bitarray
class BloomFilter:
def __init__(self, n_items, fp_prob):
'''
n_items : int
Number of items expected to be stored in bloom filter
@hughdbrown
hughdbrown / google-union-find.py
Created September 29, 2023 17:57
Code I wrote in an interview with Google. Totally nailed it -- and then got ghosted for a month by the recruiter until he got back to me to say that the position was no longer open.
"""
You're given a list of elements. Each "element" has a unique id and 3 properties.
Two elements E1 and E2 are "similar" if they share any of their 3 properties.
Please write a function that takes a list of elements as input, and returns all "similar" groups of elements.
Example Input: [E1, E2, E3]
E1: (id1, p1, p2, p3)
E2: (id2, p1, p4, p5)
E3: (id3, p6, p7, p8)