Skip to content

Instantly share code, notes, and snippets.

View damzam's full-sized avatar

David Morton damzam

View GitHub Profile
@damzam
damzam / PrimeStream
Last active December 13, 2015 18:58
#!/usr/bin/env python
from collections import defaultdict
class BaseStream(object):
"""
A utility base class that we'll use to make sure that our
streams' respective popNext() methods make use of Python's
__iter__ magic method
"""
def sort_nuts_and_bolts(nuts, bolts):
"""Sort nuts and bolts in O(N log N) time"""
def sort(nuts, bolts):
if len(nuts) == 0:
return []
lnuts, rnuts, lbolts, rbolts = [], [], [], []
nut = nuts.pop()
bolt = None
for b in bolts:
if b == nut and not bolt:
@damzam
damzam / jumble_it_up.py
Last active August 29, 2015 14:04
solve a word jumble
#!/usr/bin/env python
"""
A script that generates all possible words that can be formed from
the characters in a provided string.
Matches are case-insensitive and the wordlist used is obtained from:
https://raw.githubusercontent.com/mattt/mobius/master/data/english-wordlist
Usage:
@damzam
damzam / simple_db.py
Created August 13, 2014 02:13
A Redis-like in-memory database
#!/usr/bin/env python
"""
This is my solution to the Simple Database problem posted on:
http://www.thumbtack.com/challenges/software-engineer
Candidate: David Morton
Email: dcmorton@gmail.com
Usage:
@damzam
damzam / transform_word.py
Created August 18, 2014 00:04
Find a path from one word to another
#!/usr/bin/env python
"""
Given the start word and end word find one of the
shortest possible paths from one word to the other
that can be achieved by substituting a single
character (where each word in the path is a valid
English word)
Usage:
#!/usr/bin/env python
"""
A script to serialize and deserialize Binary Trees in Python
"""
class Node(object):
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
@damzam
damzam / encrypt_a_lowercase_string.py
Last active August 29, 2015 14:22
Encrypt a lowercase string into another lowercase string with no spaces
#!/usr/bin/env python
"""
encrypt and descript lowercase strings without using
but lowercase strings
"""
from string import lowercase
from random import choice
MAX_GARBAGE_LENGTH = 5
@damzam
damzam / lru_cache.py
Created June 12, 2015 16:28
Implement a simple LRUCache in Python
#!/usr/bin/env python
"""
Python's OrderedDict is an ideal data structure
to create an LRU cache
"""
from collections import OrderedDict
class LRUCache(object):
@damzam
damzam / generate_siblings.py
Created June 12, 2015 23:16
Create sibling relationships within a Binary Tree in O(1) memory
#!/usr/bin/env python
"""
Find the right sibling in a Binary Tree that's not fully populated
e.g.
1
/ \
2 6
/ \ \
@damzam
damzam / superset.py
Created June 13, 2015 03:39
Create a superset efficiently in Python
#!/usr/bin/env python
"""
Create a superset from the integers provided
e.g. ./superset.py 1 2 3
[set([]), set([1]), set([2]), set([3]), set([1, 2]),
set([1, 3]), set([2, 3]), set([1, 2, 3])]
"""
import sys