Skip to content

Instantly share code, notes, and snippets.

@aelguindy
aelguindy / 012-tree.py
Created February 9, 2012 09:59
Encoding/decoding 0-1-2 trees in (log 3)n bits
import random
def to_base(n, b):
""" Converts n to a string in base b. """
if n == 0: return '0'
l = []
while n:
l.append(chr((n % b) + ord('0')))
n /= b
l.reverse()
@aelguindy
aelguindy / resistance.py
Created February 5, 2012 21:05
Quick Circuit Resistance Calculator
# SOURCE: http://elonen.iki.fi/code/misc-notes/python-gaussj/
def gauss_jordan(m, eps = 1.0/(10**10)):
"""Puts given matrix (2D array) into the Reduced Row Echelon Form.
Returns True if successful, False if 'm' is singular.
NOTE: make sure all the matrix items support fractions! Int matrix will NOT work!
Written by Jarno Elonen in April 2005, released into Public Domain"""
(h, w) = (len(m), len(m[0]))
for y in range(0,h):
maxrow = y
for y2 in range(y+1, h): # Find max pivot