Skip to content

Instantly share code, notes, and snippets.

#!/bin/sh
# Git pre-commit hook
#####################
#
# - check for whitespace problems (trailing spaces, ...)
# - check for lines with 'FIXME'
# - running tests with Python 2.7, Python 3.4 and PyPy
# - running code style check (pep8) on modified files
# - designed for Windows, for Linux replace `> NUL` with `> /dev/null`
@Shaunwei
Shaunwei / 1204Game.py
Last active November 17, 2015 16:16
1024 Game Basic Algorithm
#!/usr/bin/env python3
import random
class GameBoard(object):
def __init__(self, N=4):
self.board = [[0 for _ in range(N)] for _ in range(N)]
self.N = N
self.add_chip(self.get_choices())
@Shaunwei
Shaunwei / Recommander.py
Last active December 6, 2015 06:38
recommander.py
'''
Use inverted index algorithm to enable simple movie recommandation
'''
import collections
class User:
def __init__(self, uid):
self.uid = uid
self.movies = []
@Shaunwei
Shaunwei / SymmetricLineFinder.py
Last active December 6, 2015 09:19
Given n points on a x-y axis, if it exists a line that makes each point has symmetric point.
'''
Question: Given n points on a x-y axis, if it exists a line that makes each point has symmetry point against this line.
Algorithm:
Brute Force O(n ^ 3): find n^2 lines between any two points, and test each line make all points symmetric
Better algorithm O(n ^ 2):
Observations:
Let's name this line L, which makes all points symmetric.
If we only have point A and point B,
@Shaunwei
Shaunwei / SegmentTree.py
Created December 12, 2015 08:18
Python SegmentTree Implementation
#!/usr/bin/python
# -*- coding: utf-8 -*-
class SegmentTree(object):
"""
process sequence mutable structure, O(log(n)) query time, O(log(n)) update time.
top-down-recersive-split
down-top-backtrack-update
- build
- query
@Shaunwei
Shaunwei / check_duplicate_files.py
Last active December 23, 2015 06:50
Check directory duplicate files
"""
Dropbox
Question: Given root directory, search all directories and return duplicate files
input: root path
output: list[list[]]
"""
import os
import collections
##
# Trie Solution
import collections
class Trie:
class TrieRoot:
def __init__(self):
self.children = {}
self.shortest_word = None
class TrieNode:
def __init__(self, char=''):
def print_spiral(matrix):
m, n = len(matrix), len(matrix[0])
top, down = 0, m - 1
left, right = 0, n - 1
while True:
for j in xrange(left, right + 1):
print(matrix[top][j])
top += 1
# helpers start
class Tree:
def __init__(self, val):
self.val = val
self.left = self.right = None
def __repr__(self):
return '[ %d ]' % self.val
def get_tree():
@Shaunwei
Shaunwei / huffman_compression.py
Last active January 14, 2016 02:19
String compressions - Simple compression and Huffman compression - compare with unpack/pack string problem
"""
Use serveral ways to compress string `everyday is awesome!`
1. use simple bits to replace ASCII value
2. use huffman coding
"""
def get_rate(compressed_binary, uncompressed_bits):
return len(compressed_binary) * 100 / uncompressed_bits
class SimpleCompression:
def __init__(self, string):