Skip to content

Instantly share code, notes, and snippets.

@tedmoskovitz
tedmoskovitz / compactdict.py
Created January 1, 2025 14:52
Toy hash table implementation in Python
"""Ted Moskovitz, 2025
A toy implementation of dictionaries/hash tables using
1) compact dict structure
2) linear probing with perturb
More info: https://tedmoskovitz.github.io/posts/compact_dicts/post
"""
from typing import Any
import math
from sys import getsizeof
@tedmoskovitz
tedmoskovitz / memoize.py
Created May 6, 2024 13:40
A simple LRU cache decorator in Python for function memoization
import time
from collections import OrderedDict
def lru_cache(maxsize=128):
if maxsize is None:
maxsize = float('inf')
if maxsize <= 0:
raise ValueError("negative size")
def decorator(func):
@tedmoskovitz
tedmoskovitz / nnXOR.py
Last active January 1, 2025 11:22
A simple two-layer neural network in NumPy for solving the XOR problem
import numpy as np
def xentropy_loss(target_BO, pred_BO):
return -np.mean(target_BO * np.log(pred_BO + 1e-15))
def batch_softmax(x_BO):
# subtract the max to avoid overflow
max_x_B1 = np.max(x_BO, axis=-1)[:, None]
exp_x_BO = np.exp(x_BO - max_x_B1)
return exp_x_BO / np.sum(exp_x_BO, axis=-1)[:, None]
@tedmoskovitz
tedmoskovitz / mergeBib.py
Last active April 3, 2024 23:11
A simple Python script to merge two .bib files into one, removing duplicate entries.
"""
A python script that merges two .bib files.
The merging is done according to entry title. If the same title appears more than
once, the first instance of the title will be preserved, and the second and
subsequent instances will not be included.
Usage: python merge_bib.py <bib1> <bib2> <output>
"""
import sys
@tedmoskovitz
tedmoskovitz / minCrawler.py
Last active March 9, 2024 11:01
Minimal Asynchronous Web Crawler
"""A minimal asychronous/concurrent web crawler. Author: Ted Moskovitz, 2024"""
from aiohttp import ClientSession
import asyncio
from bs4 import BeautifulSoup
import concurrent.futures as cf
import logging
from posixpath import normpath
from typing import Tuple, List, Optional, Callable
from urllib.parse import urlparse, urlsplit, urlunsplit, parse_qsl, urlencode
from urllib.robotparser import RobotFileParser