Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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())
#!/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`