Skip to content

Instantly share code, notes, and snippets.

@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)
#!/usr/bin/env python3
import sys
from collections import defaultdict
line = sys.stdin.readline()
n = int(line.split(' ')[0])
d = defaultdict(list)
for line in sys.stdin.readlines():
src, dst = [int(x) for x in line.split(' ')]
d[src].append(dst)
@hughdbrown
hughdbrown / split_paragraphs.rs
Created August 25, 2023 18:01
Group lines of text into paragraphs
use itertools::Itertools;
pub fn split_paragraphs(lines: &Vec<&str>) -> Vec<String> {
let mut paragraphs: Vec<String> = vec![];
for (key, group) in &lines.into_iter().group_by(|x| !x.is_empty()) {
if key {
paragraphs.push(group.map(|s: &&str| *s).collect::<Vec<&str>>().join(" "));
}
}
paragraphs
@hughdbrown
hughdbrown / library-help.txt
Last active July 3, 2023 04:26
Results of runnign gist library-help.py
------------------------------ 'create_raw_lookup': 0:00:00.051461 seconds
[((0, 9), 'a7874d04-3a7c-4ab2-85f3-52de785c87e1'),
((10, 19), 'bbf24bb0-e1fd-41f3-ba6a-a983fff7c675'),
((20, 29), 'fab7cd13-d273-41a1-b749-e73d22521a04'),
((30, 39), '8d688ab2-4df3-4787-b715-d5acf36f43a2'),
((40, 49), '81081dca-abf8-46e4-a45b-f25d4e6b4876')]
------------------------------ 'create_expanded_lookup': 0:00:00.014262 seconds
[(0, 'a7874d04-3a7c-4ab2-85f3-52de785c87e1'),
(1, 'a7874d04-3a7c-4ab2-85f3-52de785c87e1'),
(2, 'a7874d04-3a7c-4ab2-85f3-52de785c87e1'),
@hughdbrown
hughdbrown / library-help-py
Last active July 5, 2023 04:28
Fast way to do lookup conversion in pandas
#!/usr/bin/env python3
from pprint import pprint
from random import randint
from datetime import datetime
from uuid import uuid4
import pandas as pd
NUMS = 10000
#!/usr/bin/env python3
from timeit import timeit
def naive(values, k):
for i, v1 in enumerate(values):
for v2 in values[i + 1:]:
if v1 + v2 == k:
return True
return False
@hughdbrown
hughdbrown / trie.py
Created May 2, 2023 16:21
A class for looking up words
from collections import defaultdict
def trie():
return defaultdict(trie)
class Trie(object):
TERMINATOR = "<END>"
def __init__(self):
self.t = trie()
def insert_word(self, word):
@hughdbrown
hughdbrown / count_living_per_year_perf.py
Last active April 20, 2023 12:51 — forked from dorianturba/count_living_per_year_perf.py
Count how many people are alive per year, based on their birthyear and deathyear, aims for performances.
#!/usr/bin/env python3
import collections
import itertools
import random
import sys
import timeit
pop = [
(b := random.randint(1900, 2000), random.randint(b + 1, b + 100))