Skip to content

Instantly share code, notes, and snippets.

@stephenLee
stephenLee / design_algorithm.py
Created June 27, 2012 01:22
Udacity CS 215, algorithm design
def algorithm_development(problem_spec):
correct = False
while not correct or not fast_enough(running.time):
algorithm = devise_algorithm(problem_spec)
correct = analyze_correct(algorithm)
running.time = analyze_efficiency(algorithm)
return algorithm
@stephenLee
stephenLee / login.py
Created July 11, 2012 14:51
login renren and weibo mobile websites
import requests
renren_payload={'email': '', 'password':''}
weibo_payload={'uname': '', 'pwd': ''}
renren = requests.post('http://3g.renren.com', data=renren_payload)
weibo = requests.post('http://m.weibo.cn', data=weibo_payload)
@stephenLee
stephenLee / test.py
Created August 22, 2012 12:41
embed gist to my blog
def test_gist():
if worked == True:
print "It's cool!"
else:
print "Damn it!"
if __name__ == '__main__':
test_gist()
@stephenLee
stephenLee / cheatsheet.md
Created September 2, 2012 13:59
git cheatsheet

Workflow

  • Do some programming
  • git add
  • git status to see what files changed
  1. git status -s
  • git diff [file] to see exactly what modified
  1. git diff --cached
@stephenLee
stephenLee / factorial.scala
Created September 13, 2012 13:34
tail-recursive version of factorial
def factorial(n: Int) = {
def factorial1(n: Int, multi: Int): Int = {
if (n == 0) multi
else factorial1(n-1, multi*n)
}
factorial1(n, 1)
}
@stephenLee
stephenLee / factorial.scala
Created September 13, 2012 13:43
non tail recursive version of factorial
def factorial(n: Int): Int = {
if (n==0) 1 else n * factorial(n-1)
}
@stephenLee
stephenLee / Rational.scala
Created September 17, 2012 06:10
Scala Rational class to illustrate functional objects
class Rational(n: Int, d: Int) {
require(d != 0)
private val g = gcd(n.abs, d.abs)
val number = n / g
val denom = d / g
def this(n: Int) = this(n, 1) // auxiliary constructor
def + (that: Rational): Rational =
@stephenLee
stephenLee / Repeated.scala
Created September 17, 2012 09:12
Scala repeated parameters demo
/**
* place an asterisk after the type of the parameter
*/
def echo(args: String*) =
for (arg <- args) printn(arg)
echo("hello", "world")
val arr = Array("What's", "up", "doc?")
echo(arr: _*)
@stephenLee
stephenLee / Partially.scala
Created September 17, 2012 12:28
Scala partially applied function demo
def sum(a: Int, b: Int, c: Int): Int = a + b + c
val a = sum _
a(1,2,3)
val b = sum(1, _: Int, 3)
b(2)
b(5)
@stephenLee
stephenLee / Curried.scala
Created September 17, 2012 13:18
Defining and invoking a curried function.
def curriedSum(x: Int)(y: int) = x + y
curriedSum(1)(2) // 3
def first(x: Int) = (y: Int) => x + y
val second = first(1)
second(2) // 3
// use placeholder notation to use curriedSum in a partially applied function