Skip to content

Instantly share code, notes, and snippets.

@harryscholes
harryscholes / main.rs
Last active May 5, 2024 14:00
BPM counter
use std::collections::VecDeque;
use std::io::{self, Read};
use std::time::Instant;
use clap::Parser;
const MINUTE_IN_MILLIS: u128 = 60_000;
#[derive(Parser)]
struct Options {
@harryscholes
harryscholes / main.sh
Last active March 18, 2024 20:02
Install Python, Pip and Pinecone on Amazon Linux
# General
sudo yum install -y git tmux
# Python
sudo yum install -y python3.11 python3.11-pip
sudo ln -s /usr/bin/python3.11 /usr/bin/python
python -m pip install ipython numpy pinecone-client[grpc] tqdm
python -c "import pinecone" # Check installation
# Rust
@harryscholes
harryscholes / struct_packing.rs
Created June 2, 2023 06:50
Rust struct packing
struct Padded {
b1: bool,
u: usize,
}
struct Packed {
b1: bool,
b2: bool,
b3: bool,
b4: bool,
@harryscholes
harryscholes / Dockerfile
Created May 29, 2023 12:29
Rust scratch Docker images
FROM rust:latest AS builder
ENV RUSTFLAGS='-C linker=x86_64-linux-gnu-gcc'
WORKDIR /wd
ADD . /wd
RUN rustup target add x86_64-unknown-linux-musl
RUN apt update && apt install -y gcc-x86-64-linux-gnu
RUN cargo build --target x86_64-unknown-linux-musl --release
FROM scratch
WORKDIR /
@harryscholes
harryscholes / quick_select.jl
Created May 27, 2023 07:37
Quick select to find the ith largest element in an array in linear time
function quick_select(xs, i)
lows, equals, highs = [], [], []
pivot = xs[rand(1:length(xs))]
for x in xs
if x < pivot
append!(lows, x)
elseif x == pivot
append!(equals, x)
@harryscholes
harryscholes / random_rotations.jl
Created May 27, 2023 06:58
LSH by random rotations
using LinearAlgebra
a = [1, 2]
b = [2, 1]
c = [3, 1]
rotation_matrix = rand(d, d) .- 0.5
rotation_matrix = qr(rotation_matrix).Q # Ensure the matrix is orthogonal
a_rot = rotation_matrix * a
@harryscholes
harryscholes / minhash.jl
Last active May 26, 2023 15:55
Minhash
using Random, Distances
a = "flying fish flew by the space station"
b = "we will not allow you to bring your sticks of dynamite and pet armadillo"
c = "he figured a few sticks of dynamite were easier than a fishing pole to catch an armadillo"
shingle(text, k) = Set([text[i:i+k-1] for i in 1:length(text)-1])
a = shingle(a, 2)
b = shingle(b, 2)
@harryscholes
harryscholes / random_projections.jl
Created May 25, 2023 17:32
LSH by random projections
using Distances
nbits = 4 # number of hyperplanes and binary vals to produce
d = 2 # vector dimensions
# plane_norms = rand(d, nbits) .- 0.5
plane_norms = [[-0.26623211, 0.34055181] [0.3388499, -0.33368453] [0.34768572, -0.37184437] [-0.11170635, -0.0242341]]
a = [1, 2]
b = [2, 1]
@harryscholes
harryscholes / right_view_of_binary_tree.py
Created May 24, 2023 12:41
Right view of a binary tree
class Node:
def __init__(self, value=None, left=None, right=None):
self.value = value
self.left = left
self.right = right
def __repr__(self):
return str(self.value)
def partition(arr, low, high):
pivot = arr[high]
i = low
for j in range(low, high):
if arr[j] <= pivot:
arr[i], arr[j] = arr[j], arr[i]
i += 1
arr[i], arr[high] = arr[high], arr[i]