Instantly share code, notes, and snippets.

Peter Fraenkelpnf

• Sort options
Last active Dec 21, 2015
Two algorithms for finding the longest anagram in a list of words.
View longest-anagram.pl
 die "Note that there are two different algorithms here. Don't expect it to run as is." #!/usr/bin/perl # ~0.15s on i7 1.7GHz my @all; my \$m=0; while(<>) { \$l = length(\$_); \$m = \$l if \$l>\$m;
Last active Dec 19, 2015
This is supposed to be a real sieve of eratosthenes, per http://www.cs.tufts.edu/~nr/comp150fp/archive/melissa-oneill/Sieve-JFP.pdf
View lazysieve.clj
 (defn maintain-non-primes [non-primes] "Remove first non-prime entry, and create new entries based on it" (let [[n ps] (first non-primes) xs (dissoc non-primes n) new-non-primes (map (fn [p] [(+ n p) (cons p (xs (+ n p))) ]) ps)] (into xs new-non-primes))) (defn seive ([] (seive (sorted-map) 2)) ([n] (take n (seive (sorted-map) 2))) ([non-primes n]
Last active Dec 19, 2015
AI-ish tic-tac-toe.
View tictactoe.clj
 ; list of winning positions (def wp [2r111000000 2r000111000 2r000000111 2r100100100 2r010010010 2r001001001 2r100010001 2r001010100]) ;; Is a particular bitmask winning? (defn winner? [p] (some (fn [c] (= (bit-and c p) c)) wp)) ;; list of possible moves, filtering out ones already made (defn moves [p] (filter pos? (map #(bit-and (bit-not p) (bit-shift-left 1%)) (range 9)))) ;; A board position comprises a bitarray for each player and an indicaiton of whose turn it is.
Created Jun 24, 2013
Print out a fractal tree
View frascii
 #!/usr/bin/env python from math import * import sys # draw line from x,y of length s at angle theta, spawn growth of length s*r, theta+/-q unless n==0 def grow(x, y, theta, s, q, r, n): ret = [(x,y,theta,s)] n = n-1 if n>0: x2 = x + s*cos(theta)
Created Jun 23, 2013