Skip to content

Instantly share code, notes, and snippets.

@thisiswei
thisiswei / pgessays.py
Created November 24, 2012 22:32 — forked from olasitarska/pgessays.py
Builds epub book out of Paul Graham's essays.
# -*- coding: utf-8 -*-
"""
Builds epub book out of Paul Graham's essays: http://paulgraham.com/articles.html
Author: Ola Sitarska <ola@sitarska.com>
This script requires python-epub-library: http://code.google.com/p/python-epub-builder/
"""
import re, ez_epub, urllib2, genshi
files = file('54.txt').readlines()
hands = [h.replace('\n','') for h in files]
def result():
dicts = {0:0, 1:0}
for i in range(1000):
h1, h2 = split_hand(hands[i])
dicts[who_won(h1,h2)] +=1
return dicts
def pascal(n):
if n == 1:
return [1]
else:
x = pascal(n-1)
return [1] + [x[i]+x[i-1] for i in range(1,len(x))] + [1]
" seems like learning functional language is helpful :) "
@thisiswei
thisiswei / frequency.py
Last active December 12, 2015 07:58
frequency
from collections import Counter
from collections import defaultdict
def frequency1(lst):
return [(lst.count(s), s) for s in set(lst)]
def frequency2(lst):
return dict((s, lst.count(s))
for s in set(lst))
(define fib3
(letrec([memo null] ; list of pairs (arg . result)
[f (lambda (x)
(let ([ans (assoc x memo)])
(if ans
(cdr ans)
(let ([new-ans (if (or (= x 1) (= x 2))
1
(+ (f (- x 1))
(f (- x 2))))])
(define (ps1 x)
(if (equal? 0 x)
0
(if (or (equal? 0 (modulo x 3)) (equal? 0 (modulo x 5)))
(+ x (ps1 (- x 1)))
(ps1 (- x 1)))))
(ps1 999)
"""Embedded in this block of text is the password for level 2.
The password is the longest substring that is the same in reverse.
As an example, if the input was "I like racecars that go fast"
the password would be "racecar".
challenge from http://challenge.greplin.com/
"""
text = """FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingra
"""
1) Win: If you have two in a row, play the third to get three in a row.
2) Block: If the opponent has two in a row, play the third to block them.
3) Fork: Create an opportunity where you can win in two ways.
4) Block Opponent's Fork:
5) Center: Play the center.
6) Opposite Corner: If the opponent is in the corner, play the opposite corner.
7) Empty Corner: Play an empty corner.
8) Empty Side: Play an empty side.
"""
@thisiswei
thisiswei / pascals-triangle.rkt
Created May 10, 2013 17:51
pascals-triangle
(define (expand-row p)
(cons (car p) (expand-row-rest p)))
(define (expand-row-rest p)
(if (null? (cdr p)) (list 1)
(cons (+ (car p) (cadr p))
(expand-row-rest (cdr p)))))
(define (pascals-row n)
(if (= n 0) (list 1)
(define (getFinalAmount initial res)
(define (helper total bet n)
(cond [(or (> (+ 1 n) (string-length res)) (> bet total)) total]
[(eq? (string-ref res n) #\L) (helper (- total bet) (* 2 bet) (+ n 1))]
[#t (helper (+ amount total) 1 (+ n 1))]))
(helper initial 1 0))