Skip to content

Instantly share code, notes, and snippets.

(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))
def getFinalAmount(initial, res):
def helper(total, bet, n):
return (total if n > len(res) - 1 or bet > total else
helper(total-bet, bet*2, n+1) if res[n] == 'L' else
helper(total+bet, 1, n+1))
return helper(initial, 1, 0)
getFinal initial res = helper initial 1 0
where helper total bet n = if n > (length res) - 1 || bet > total.
then total
else if res !! n == 'L'.
then helper (total-bet) (bet*2) (n+1)
else helper (total+bet) 1 (n+1)
fun getFinal init res =
let
fun helper total bet n =
case n+1 > size res of
true => total
| _ => if String.sub (res, n) = #"L"
then helper (total-bet) (bet*2) (n+1)
else helper (total+bet) 1 (n+1)
in
helper init 1 0
@thisiswei
thisiswei / dvr_remote.py
Last active December 18, 2015 13:19
You know how the remote on your DVR makes you select the title of a movie or TV show by moving a cursor up/down/left/right through a grid of letters and then pressing the 'select' key? It takes forever and is very annoying. Imagine you had an app running on your phone that would allow you to type or speak the name, and the app would send the rig…
def dvr_remote(s, width):
x = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'
rows = [x[i:i+width] for i in range(0, 27, width)]
s = 'A' + s.replace(' ', '_')
DIC = dict((char, (i, pos))
for i in range(len(rows))
for (pos, char) in enumerate(rows[i]))
def helper(current, next):
row1, col1 = DIC[current]