This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Use inverted index algorithm to enable simple movie recommandation | |
''' | |
import collections | |
class User: | |
def __init__(self, uid): | |
self.uid = uid | |
self.movies = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Dropbox | |
Question: Given root directory, search all directories and return duplicate files | |
input: root path | |
output: list[list[]] | |
""" | |
import os | |
import collections |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# Trie Solution | |
import collections | |
class Trie: | |
class TrieRoot: | |
def __init__(self): | |
self.children = {} | |
self.shortest_word = None | |
class TrieNode: | |
def __init__(self, char=''): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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): |
OlderNewer