Skip to content

Instantly share code, notes, and snippets.

@stephenLee
stephenLee / Traits.scala
Created September 18, 2012 11:28
Scala Traits mixins example
import scala.collection.mutable.ArrayBuffer
//Abstract class IntQueue
abstract class IntQueue {
def get(): Int
def put(x: Int)
}
// A BasicIntQueue implemented with an ArrayBuffer
class BasicIntQueue extends IntQueue {
@stephenLee
stephenLee / Case.scala
Created September 20, 2012 00:47
Scala case class
abstract class Expr
case class Var(name: String) extends Expr
case class Number(num: Double) extends Expr
case class UnOp(operator: String, arg: Expr) extends Expr
case class BinOp(operator: String, left: Expr, right: Expr) extends Expr
val v = Var("x")
val op = BinOp("+", Number(1), v)
v.name
@stephenLee
stephenLee / Option.scala
Created September 20, 2012 05:13
Scala Option example
val capitals = Map("France" -> "Paris", "Japan" -> "Tokyo")
capitals get "France"
capitals get "North Pole"
def show(x: Option[String]) = x match {
case Some(x) => x
case None => "?"
}
show(capitals get "Japan")
@stephenLee
stephenLee / Option.scala
Created September 20, 2012 05:13
Scala Option example
val capitals = Map("France" -> "Paris", "Japan" -> "Tokyo")
capitals get "France"
capitals get "North Pole"
def show(x: Option[String]) = x match {
case Some(x) => x
case None => "?"
}
show(capitals get "Japan")
@stephenLee
stephenLee / weibo_nooauth.py
Created October 23, 2012 00:03
weibo_login script
#!/usr/bin/env python
#coding=utf8
import urllib
import urllib2
import cookielib
import base64
import re
import json
import hashlib
import os
@stephenLee
stephenLee / Makefile
Created October 23, 2012 16:27
Tiny Makefile template for c/c++
# define variable, use using $()
# -g add debug information to the executable file
# -Wall turns on most, but not all, compiler warnings
CC = g++
CFLAGS = -g -Wall
file: file.o
$(CC) $(CFLAGS) -o file file.o
file.o: file.cpp
@stephenLee
stephenLee / Makefile
Created October 23, 2012 16:27
Tiny Makefile template for c++
# define variable, use using $()
# -g add debug information to the executable file
# -Wall turns on most, but not all, compiler warnings
# using Emacs and M-x replace-string RET string RET newstring RET
# to replace xx to your file name.
# Use google's testing framework
GTEST_DIR = ../gtest
# Where to find user code.
USER_DIR = ../gtest_sample
@stephenLee
stephenLee / bithack.cc
Created November 6, 2012 13:56
bit manipulation tricks(collections)
/*
* Reference:
* http://www.quora.com/Computer-Programming/What-are-some-cool-bit-manipulation-tricks-hacks
* http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/
*/
#include <iostream>
#include <string.h>
using namespace std;
@stephenLee
stephenLee / maze.py
Created November 10, 2012 13:08
迷宫问题求解
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Queue
"""
Problem:(from http://weibo.com/lirenchen)
从图左边入口处的2011进去,在迷宫里转悠,最后变成2012从右边出来。可以在迷宫里转圈,
@stephenLee
stephenLee / bitwise-operators.md
Created November 12, 2012 11:41 — forked from dideler/bitwise-operators.md
Bitwise tricks

Inspired by this article. Neat tricks for speeding up integer computations.

Note: cin.sync_with_stdio(false); disables synchronous IO and gives you a performance boost. If used, you should only use cin for reading input (don't use both cin and scanf when sync is disabled, for example) or you will get unexpected results.

Multiply by a power of 2

x = x << 1; // x = x * 2