Mercurial > coderloop
changeset 0:307a81e46071 tip
initial committ
line wrap: on
line diff
1.1 Binary file src/Collatz.class has changed
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/Collatz.java Tue Oct 18 01:17:49 2011 -0700 2.3 @@ -0,0 +1,37 @@ 2.4 +package coderloop; 2.5 + 2.6 +import java.io.File; 2.7 +import java.util.Scanner; 2.8 + 2.9 +class Collatz { 2.10 + public static void main(String[] args)throws java.io.FileNotFoundException{ 2.11 + File file = new File(args[0]); 2.12 + Scanner scanner = new Scanner(file); 2.13 + int n = scanner.nextInt(); 2.14 + System.out.println(search_collatz(n));} 2.15 + 2.16 + public static long collatz_next(long n){ 2.17 + if (0 == (n % 2)){ 2.18 + return n/2;} 2.19 + else { 2.20 + return (1 + (n * 3));}} 2.21 + 2.22 + public static int collatz_length(int n){ 2.23 + long t = n; 2.24 + int d = 1; 2.25 + while (t > 1){ 2.26 + t = collatz_next(t); 2.27 + d++;} 2.28 + return d;} 2.29 + 2.30 + public static int search_collatz(int n){ 2.31 + int max = 0; 2.32 + int max_val = 0; 2.33 + int temp_val = 0; 2.34 + for (int i = 1; i<n; i++){ 2.35 + temp_val = collatz_length(i); 2.36 + if (temp_val > max_val){ 2.37 + max = i; 2.38 + max_val = temp_val;}} 2.39 + return max;} 2.40 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/Collatz.java.ant.xml Tue Oct 18 01:17:49 2011 -0700 3.3 @@ -0,0 +1,16 @@ 3.4 +<?xml version="1.0" encoding="UTF-8"?> 3.5 + <project name="collatz" basedir="." default="default"> 3.6 + <property name="build" location=".build" /> 3.7 + <property name="srcfolder" value="src" /> 3.8 + <property name="code.classes" location="${build}/classes" /> 3.9 + <property name="code.sources" location="${srcfolder}" /> 3.10 + <dirset id="srcdir" dir="." includes="src" /> 3.11 + <path id="lib.path"><pathelement path="${code.classes}" /></path> 3.12 + <target name="compile"> 3.13 + <mkdir dir="${code.classes}" /> 3.14 + <javac srcdir="${code.sources}" destdir="${code.classes}" classpathref="lib.path" target="1.5" encoding="utf-8" /> 3.15 + </target> 3.16 + <target name="default" > 3.17 + <antcall target="compile" /> 3.18 + </target> 3.19 +</project>
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/Collatz.java.run Tue Oct 18 01:17:49 2011 -0700 4.3 @@ -0,0 +1,4 @@ 4.4 +#!/bin/bash 4.5 +MAINCLASS="coderloop.Collatz" 4.6 +CP=".build/classes" 4.7 +java -cp "${CP}" ${MAINCLASS} "$*"
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/collatz.c Tue Oct 18 01:17:49 2011 -0700 5.3 @@ -0,0 +1,40 @@ 5.4 + 5.5 +#include <stdio.h> 5.6 +#include <stdlib.h> 5.7 + 5.8 +int main(int argc, char *argv[]){ 5.9 + FILE *fp; 5.10 + int n; 5.11 + if((fp=fopen(argv[1] ,"r")) == NULL) { 5.12 + printf("Cannot open file.\n"); 5.13 + exit(1);} 5.14 + fscanf(fp, "%d", &n); /* read from file */ 5.15 + int answer = search_collatz(n); 5.16 + printf("%d\n", answer); } 5.17 + 5.18 +long long collatz_next(long long n){ 5.19 + if (!(n % 2)){ 5.20 + return n/2;} 5.21 + else { 5.22 + return 1+(n*3);}} 5.23 + 5.24 +int collatz_length(int n){ 5.25 + long long t = n; 5.26 + int d = 1; 5.27 + while (t > 1){ 5.28 + t = collatz_next(t); 5.29 + d++;} 5.30 + return d;} 5.31 + 5.32 +int search_collatz(int n){ 5.33 + int max = 0; 5.34 + int max_val = 0; 5.35 + int temp_val; 5.36 + int i; 5.37 + for (i = 1; i<n; i++){ 5.38 + temp_val = collatz_length(i); 5.39 + if (temp_val > max_val){ 5.40 + max = i; 5.41 + max_val = temp_val;}} 5.42 + return max;} 5.43 +
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/collatz.f Tue Oct 18 01:17:49 2011 -0700 6.3 @@ -0,0 +1,52 @@ 6.4 + program collatz 6.5 + implicit none 6.6 + character (len = 100) :: file_name 6.7 + integer, parameter :: big = selected_int_kind(11) 6.8 + integer :: n 6.9 + call getarg( 1, file_name ) 6.10 + OPEN(unit = 7, file = file_name) 6.11 + read (7,*) n 6.12 + write (unit = *, fmt = "(I0)") search_collatz(n) 6.13 + 6.14 + contains 6.15 + 6.16 + function collatz_next (n) result (r) 6.17 + implicit none 6.18 + integer (kind = big) :: r,n 6.19 + if (0 == mod(n,2)) then 6.20 + r = n/2 6.21 + else 6.22 + r = 1 + 3*n 6.23 + end if 6.24 + end function collatz_next 6.25 + 6.26 + function collatz_length (n) result (r) 6.27 + implicit none 6.28 + integer (kind = big) :: t 6.29 + integer :: n,r 6.30 + r = 1 6.31 + t = n 6.32 + do 6.33 + if (1 == t) then 6.34 + exit 6.35 + end if 6.36 + r = r + 1 6.37 + t = collatz_next(t) 6.38 + end do 6.39 + end function collatz_length 6.40 + 6.41 + function search_collatz (n) result (r) 6.42 + implicit none 6.43 + integer n,r,i,max,max_val,temp_val 6.44 + integer (kind = big) :: t 6.45 + t = n 6.46 + do i = 1,n-1 6.47 + temp_val = collatz_length(i) 6.48 + if (temp_val > max_val) then 6.49 + max = i 6.50 + max_val = temp_val 6.51 + end if 6.52 + end do 6.53 + r = max 6.54 + end function search_collatz 6.55 + end program collatz
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/src/collatz.f.make Tue Oct 18 01:17:49 2011 -0700 7.3 @@ -0,0 +1,4 @@ 7.4 +euler14: collatz.f 7.5 + gfortran -Wall -o euler14 collatz.f 7.6 + 7.7 +
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/src/collatz.make Tue Oct 18 01:17:49 2011 -0700 8.3 @@ -0,0 +1,10 @@ 8.4 + 8.5 +euler14 : collatz.c 8.6 + gcc collatz.c -o euler14 8.7 + 8.8 +test : euler14 8.9 + ./euler14 euler14-a.in 8.10 + 8.11 +clean : 8.12 + rm euler14 8.13 +
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/src/collatz.pl Tue Oct 18 01:17:49 2011 -0700 9.3 @@ -0,0 +1,69 @@ 9.4 +#!/usr/bin/perl 9.5 + 9.6 +use bigint; 9.7 +$a = "/home/r/coderloop-test/euler14-a.in"; 9.8 +$b = "/home/r/coderloop-test/euler14-b.in"; 9.9 + 9.10 +sub read_integer{ 9.11 + ##reads an integer from a file 9.12 + (my $file) = @_; 9.13 + my $int; 9.14 + open FILE, "<".$file or die $!; 9.15 + for (<FILE>){ chomp; /^\W*(\d+)\W*$/; $int = $1;} 9.16 + close FILE; 9.17 + return $int;} 9.18 + 9.19 + 9.20 +@collatz = (1); 9.21 + 9.22 + 9.23 +sub collatz_next { 9.24 + my $n = @_[0]; 9.25 + if (1 == $n) {return 1;} 9.26 + if (0 == ($n % 2)){ return $n/2;} 9.27 + if (1 == ($n % 2)){ return (($n*3)+1);} 9.28 +} 9.29 + 9.30 +sub different_collatz_length { 9.31 + my $n = $_[0]; 9.32 + my $d = 1; 9.33 + while ($n != 1){ 9.34 + $d++; 9.35 + $n = collatz_next($n);} 9.36 + return $d;} 9.37 + 9.38 +sub collatz_length { 9.39 + ## calculate the length of the collatz sequence 9.40 + ## http://en.wikipedia.org/wiki/Collatz_conjecture 9.41 + ## starting at n 9.42 + my $n = @_[0]; 9.43 + #print $n,"\n"; 9.44 + #memoization 9.45 + #print "***** ", $collatz[$n], " ********\n"; 9.46 + if (defined $collatz[$n]){return $collatz[$n];} 9.47 + if (1 == $n) {return 1;} 9.48 + if (0 == ($n % 2)){ return (1 + &collatz_length($n/2))} 9.49 + if (1 == ($n % 2)){ return (1 + &collatz_length(($n*3)+1));} 9.50 +} 9.51 + 9.52 + 9.53 +sub max_collatz { 9.54 + my $n = @_[0]; 9.55 + my $max_length = 1; 9.56 + my $max_val = 1; 9.57 + for (my $i = 1; $i < $n; $i++){ 9.58 + my $collatz = &different_collatz_length($i); 9.59 + #push(@collatz, $collatz); 9.60 + if ( $collatz >= $max_length){ 9.61 + $max_length = $collatz ; 9.62 + $max_val = $i}} 9.63 + print "\n number is : $max_length --- "; 9.64 + print &collatz_length($max_val) . "\n"; 9.65 + return $max_val; 9.66 +} 9.67 + 9.68 + 9.69 + 9.70 + 9.71 +if (defined @ARGV){ 9.72 + print &max_collatz(read_integer($ARGV[0])), "\n";}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/src/decrypt.clj Tue Oct 18 01:17:49 2011 -0700 10.3 @@ -0,0 +1,43 @@ 10.4 +(ns coderloop.decrypt 10.5 + (:use [clojure.contrib 10.6 + [duck-streams :only [read-lines]] 10.7 + [seq :only [find-first]] 10.8 + [string :only [map-str]] 10.9 + [combinatorics :only [cartesian-product]]]) 10.10 + (:use rlm.shell-inspect) 10.11 + (:use [clojure [string :only [split]]]) 10.12 + (:import Crypt)) 10.13 + 10.14 +(def t "/home/r/coderloop-test/test.txt") 10.15 + 10.16 +(defn strcat [coll] 10.17 + (apply str coll)) 10.18 + 10.19 +(def numerals [0 2 4 8 ]) 10.20 + 10.21 +(defn crypt [salt s] 10.22 + (Crypt/crypt salt s)) 10.23 + 10.24 +(defn cross-text [text] 10.25 + (let [symbols (filter 10.26 + (fn [s] (<= (count s) 8)) 10.27 + (split (.toLowerCase text) #"[^a-z]+"))] 10.28 + (filter 10.29 + (fn [s] 10.30 + (let [len (count s)] 10.31 + (and 10.32 + (>= len 5) 10.33 + (<= len 8)))) 10.34 + (map (partial apply str) 10.35 + (cartesian-product symbols numerals symbols))))) 10.36 + 10.37 +(defn process-file [f] 10.38 + (let [file (read-lines f) 10.39 + [salt pass] (map (partial apply str) (split-at 2 (first file))) 10.40 + text (strcat (interleave (repeat \newline) (rest file)))] 10.41 + (find-first (fn [s] (= (str salt pass) (crypt salt s))) 10.42 + (cross-text text)))) 10.43 + 10.44 + 10.45 +(if (command-line?) 10.46 + (println (process-file (first *command-line-args*))))
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/src/douglas_adams.clj Tue Oct 18 01:17:49 2011 -0700 11.3 @@ -0,0 +1,20 @@ 11.4 +(ns coderloop.douglas-adams 11.5 + (:refer-clojure :only []) 11.6 + (:require rlm.ns-rlm rlm.light-base)) 11.7 +(rlm.ns-rlm/ns-clone rlm.light-base) 11.8 + 11.9 +(use '[clojure [string :only [trim blank? split]]]) 11.10 + 11.11 + 11.12 + 11.13 +(defn brute-force-life [coll] 11.14 + (dorun (map println (take-while (comp not (partial = 42)) coll)))) 11.15 + 11.16 + 11.17 + 11.18 + 11.19 + 11.20 +(if (command-line?) 11.21 + (brute-force-life (read-integers (file-str (first *command-line-args*)))) 11.22 + nil) 11.23 +
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/src/euler6.f Tue Oct 18 01:17:49 2011 -0700 12.3 @@ -0,0 +1,18 @@ 12.4 + program euler6 12.5 + 12.6 + implicit none 12.7 + character (len = 100) :: file_name 12.8 + integer, parameter :: big = selected_int_kind(11) 12.9 + integer (kind = big) :: n,ans 12.10 + call getarg( 1, file_name ) 12.11 + OPEN(unit = 7, file = file_name) 12.12 + read (7,*) n 12.13 + 12.14 + ans = ((n * (n + 1))/2)**2 - ((n * (n + 1) * (1 + ( 2 * n)))/6) 12.15 + 12.16 + write (unit = *, fmt = "(I0)") ans 12.17 + 12.18 + 12.19 + 12.20 + 12.21 + end program euler6
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 13.2 +++ b/src/euler6.f.make Tue Oct 18 01:17:49 2011 -0700 13.3 @@ -0,0 +1,4 @@ 13.4 +euler6: euler6.f 13.5 + gfortran -Wall -o euler6 euler6.f 13.6 + 13.7 +
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 14.2 +++ b/src/export_files.clj Tue Oct 18 01:17:49 2011 -0700 14.3 @@ -0,0 +1,246 @@ 14.4 +(ns coderloop.export-files 14.5 + (:use clojure.java.io) 14.6 + (:use [clojure.contrib [duck-streams :only [file-str]]]) 14.7 + (:use [rlm 14.8 + [classpath-utils :only [classpath]] 14.9 + [shell-write :only [sw]]]) 14.10 + (:require clojure.string) 14.11 + (:import java.io.File)) 14.12 + 14.13 +(import '[org.apache.commons.io FileUtils]) 14.14 + 14.15 +;;(use '[telmo.rlm [dist :only [dist]]]) 14.16 + 14.17 +(def *nailgun* true) 14.18 + 14.19 +;;; put standard base-path on the classpath 14.20 +(def standard-base-directory (file-str "~/.clojure-exports/")) 14.21 + 14.22 +(defn better-export-files [namespace name] 14.23 + (binding [*compile-path* (.getPath standard-base-directory)] 14.24 + ;;clear out classes -- prepare temp directory 14.25 + (FileUtils/forceMkdir standard-base-directory) 14.26 + (FileUtils/deleteDirectory standard-base-directory) 14.27 + (FileUtils/forceMkdir standard-base-directory) 14.28 + ;; make classes 14.29 + (compile (symbol (str namespace))) 14.30 + 14.31 + ;; make jar 14.32 +;; (jar {:jarfile jar-name :basedir temp-class-dir}) 14.33 + 14.34 + ;; move jar 14.35 +;; (FileUtils/copyFileToDirectory jar-name target-lib-dir) 14.36 + )) 14.37 + 14.38 +(defmulti source-location class) 14.39 + 14.40 +(defmethod source-location clojure.lang.Var [sym] 14.41 + (let [source? (:file (meta sym))] 14.42 + (if source? 14.43 + (.getResource (clojure.lang.RT/baseLoader) source?) 14.44 + nil))) 14.45 + 14.46 +(defmethod source-location java.lang.Class [sym] 14.47 + (let [source? (.getCodeSource (.getProtectionDomain sym))] 14.48 + (if source? 14.49 + (.getLocation source?) 14.50 + nil))) 14.51 + 14.52 +(defn all-dependent-namespaces [namespace] 14.53 + (set 14.54 + (concat 14.55 + [namespace] 14.56 + 14.57 + (vals (ns-aliases namespace)) 14.58 + 14.59 + (map #(.ns %) 14.60 + (filter #(= (class %) clojure.lang.Var) 14.61 + (concat 14.62 + 14.63 + (vals (ns-map namespace)) 14.64 + (vals (ns-refers namespace)) 14.65 + 14.66 + )))))) 14.67 + 14.68 +(defn deep-dependent-namespaces [namespace-set] 14.69 + (let [new-namespaces (set (mapcat all-dependent-namespaces namespace-set))] 14.70 + ;;(println (count new-namespaces)) 14.71 + (if (= new-namespaces namespace-set) 14.72 + namespace-set 14.73 + (recur new-namespaces)))) 14.74 + 14.75 + 14.76 +(defn dependencies-url 14.77 + "returns all of the files necessary to succesfully run the namespace." 14.78 + [namespace] 14.79 + (set 14.80 + (remove nil? 14.81 + (map source-location 14.82 + (map second 14.83 + (mapcat ns-map (deep-dependent-namespaces #{namespace}))))))) 14.84 + 14.85 + 14.86 +(defn trans-print [x] (println x) x) 14.87 + 14.88 +(defn dependencies-file [namespace] 14.89 + (map file-str 14.90 + (set 14.91 + (map #(.replaceFirst % "file:" "") 14.92 + (map #(if (.contains % ".jar!/") 14.93 + (clojure.string/replace % #"\.jar.*" ".jar") 14.94 + %) 14.95 + (map #(.getFile %) (dependencies-url namespace))))))) 14.96 + 14.97 + 14.98 + 14.99 +(defn classpath-files [] 14.100 + (map file-str (clojure.string/split (classpath) #":"))) 14.101 + 14.102 +;;Every file that comes back from dependencies-file is also on the classpath. 14.103 +;;In order to recreate the right project structure, we need to copy the files 14.104 +;;with the appropiate classpath nesting. 14.105 + 14.106 +(defn bifurcate 14.107 + "split a collection between f and not f" 14.108 + [f coll] 14.109 + (list 14.110 + (filter f coll) 14.111 + (filter (comp not f) coll))) 14.112 + 14.113 +(defn jar? [#^java.io.File f] 14.114 + (re-matches #".*\.jar$" (.getCanonicalPath f))) 14.115 + 14.116 +(defn contains-file? [#^java.io.File parent #^java.io.File child] 14.117 + (let [new-child (.getParentFile child)] 14.118 + (cond (nil? new-child) false 14.119 + (= new-child parent) true 14.120 + true (recur parent new-child)))) 14.121 + 14.122 +(defn destination [base-path-list #^java.io.File current-file #^java.io.File base-destination] 14.123 + (let [base-path (last (filter #(contains-file? % current-file) base-path-list))] 14.124 + (file-str 14.125 + (.replaceFirst (.getCanonicalPath current-file) 14.126 + (.getCanonicalPath base-path) 14.127 + (.getCanonicalPath base-destination))))) 14.128 + 14.129 +(defn export-dependencies 14.130 + ([namespace #^java.io.File target src* lib*] 14.131 + (let [[jars sources] (bifurcate jar? (dependencies-file namespace)) 14.132 + jars (if *nailgun* 14.133 + (conj jars (file-str "~/roBin/nailgun-0.7.1/nailgun-0.7.1.jar")) 14.134 + jars) 14.135 + 14.136 + lib (file-str (str (.getCanonicalPath target) File/separatorChar lib*)) 14.137 + src (file-str (str (.getCanonicalPath target) File/separatorChar src*))] 14.138 + (if *nailgun* 14.139 + (do 14.140 + (FileUtils/copyFileToDirectory (file-str "~/roBin/nailgun-0.7.1/ng") lib) 14.141 + (sw (str "chmod +x " (.getCanonicalPath target) File/separatorChar lib* "/ng")))) 14.142 + (dorun (map #(FileUtils/copyFileToDirectory % lib) jars)) 14.143 + (dorun (map #(FileUtils/copyFile % (destination (classpath-files) % src)) sources)))) 14.144 + 14.145 + ([namespace #^java.io.File target] 14.146 + (export-dependencies namespace target "src" "lib"))) 14.147 + 14.148 +(defn run-script-text 14.149 + ([namespace] 14.150 + (run-script-text namespace "src" "lib")) 14.151 + ([namespace src lib] 14.152 + (if *nailgun* 14.153 + (str 14.154 + 14.155 + "#!/usr/bin/perl 14.156 + 14.157 +if (`./"lib"/ng ng-alias 2>&1 1>/dev/null` eq \"connect: Connection refused\\n\"){ 14.158 + 14.159 + $nailgun_init = 14.160 + \"java -Xmn500M -Xms2000M -Xmx2000M \" . 14.161 + \"-classpath ./"src"/:"lib"/*\" . \" -server \". 14.162 + \"com.martiansoftware.nailgun.NGServer \". 14.163 + \" >/dev/null 2>/dev/null &\"; 14.164 + `$nailgun_init`; 14.165 + }\n\n" 14.166 + 14.167 + 14.168 + "while (`./"lib"/ng ng-alias 2>&1 1>/dev/null` eq \"connect: Connection refused\\n\"){ 14.169 + }\n\n" 14.170 + 14.171 + "$command = " 14.172 + 14.173 + 14.174 + "\"./"lib"/ng clojure.main " 14.175 + "./"src"/" 14.176 + (.replace (.replace (str *ns*) \. File/separatorChar) \- \_) 14.177 + ".clj " 14.178 + " @ARGV\";\n" 14.179 + 14.180 + "print `$command`;\n") 14.181 + (str 14.182 + "#!/bin/bash\n" 14.183 + "java -Xmn500M -Xms2000M -Xmx2000M -server -cp ./"lib"/*:./"src" clojure.main " 14.184 + "./"src"/" 14.185 + (.replace (.replace (str *ns*) \. File/separatorChar) \- \_) 14.186 + ".clj " 14.187 + " $@\n")))) 14.188 + 14.189 + 14.190 + 14.191 +(defn make-run-script 14.192 + ([namespace base-dir name] 14.193 + (make-run-script namespace base-dir name "src" "lib")) 14.194 + ([namespace base-dir name src lib] 14.195 + (let [w (clojure.java.io/writer 14.196 + (str (.getCanonicalPath base-dir) File/separatorChar name))] 14.197 + (.write w (run-script-text namespace src lib)) 14.198 + (.close w)) 14.199 + (let [f (file-str (str (.getCanonicalPath base-dir) File/separatorChar name))] 14.200 + (.setExecutable f true)))) 14.201 + 14.202 + 14.203 +(defn bzip-folder [#^java.io.File destination #^java.io.File directory] 14.204 + (apply (partial sw "tar" "-cvjf" (.getCanonicalPath destination)) 14.205 + 14.206 + (concat 14.207 + (rest 14.208 + (map #(.replaceFirst 14.209 + % 14.210 + (str (.getCanonicalPath directory) File/separatorChar ) "") 14.211 + (map str (file-seq directory)))) 14.212 + [:dir (.getCanonicalPath directory)]))) 14.213 + 14.214 + 14.215 +(defn bzip-export-files 14.216 + ([directory to name postfix] 14.217 + (bzip-export-files directory to name postfix "src" "lib")) 14.218 + 14.219 + ([directory to name postfix src lib] 14.220 + (apply (partial sw "tar" "-cvjf" 14.221 + (str (.getCanonicalPath to) File/separatorChar (str name postfix))) 14.222 + (concat 14.223 + [name src lib] 14.224 + [:dir (.getCanonicalPath directory)])))) 14.225 + 14.226 +(defn export-archive 14.227 + ([name] 14.228 + (export-archive *ns* name)) 14.229 + 14.230 + ([namespace name nailgun?] 14.231 + (binding [*nailgun* nailgun?] 14.232 + (export-archive namespace name))) 14.233 + 14.234 + ([namespace name] 14.235 + (FileUtils/forceMkdir standard-base-directory) 14.236 + (FileUtils/forceDelete standard-base-directory) 14.237 + (FileUtils/forceMkdir standard-base-directory) 14.238 + (let [new-dir (file-str (str 14.239 + (.getCanonicalPath standard-base-directory) 14.240 + (File/separatorChar) 14.241 + name))] 14.242 + (export-dependencies namespace new-dir) 14.243 + (make-run-script namespace new-dir name) 14.244 + (println (str name "-clojure.tar.bz2")) 14.245 + (println name) 14.246 + (bzip-export-files new-dir (file-str "~/coderloop/") name "-clojure.tar.bz2") 14.247 + (FileUtils/copyFileToDirectory 14.248 + (file-str (str "~/coderloop/" name "-clojure.tar.bz2")) 14.249 + (file-str "~/coderloop-test")))))
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 15.2 +++ b/src/export_files.clj.orig Tue Oct 18 01:17:49 2011 -0700 15.3 @@ -0,0 +1,225 @@ 15.4 +(ns coderloop.export-files 15.5 + (:use clojure.java.io) 15.6 + (:use [clojure.contrib [duck-streams :only [file-str]]]) 15.7 + (:use [rlm 15.8 + [classpath-utils :only [classpath]] 15.9 + [shell-write :only [sw]]]) 15.10 + (:require clojure.string) 15.11 + (:import java.io.File) 15.12 + (:import [org.apache.commons.io FileUtils]) 15.13 + ) 15.14 + 15.15 + 15.16 +(defn trans-print [x] (println x) x) 15.17 + 15.18 +(def *nailgun* true) 15.19 + 15.20 +;;; put standard base-path on the classpath 15.21 +(def standard-base-directory (file-str "~/.clojure-exports/")) 15.22 + 15.23 +(defn better-export-files [namespace name] 15.24 + (binding [*compile-path* (.getPath standard-base-directory)] 15.25 + ;;clear out classes -- prepare temp directory 15.26 + (FileUtils/forceMkdir standard-base-directory) 15.27 + (FileUtils/deleteDirectory standard-base-directory) 15.28 + (FileUtils/forceMkdir standard-base-directory) 15.29 + ;; make classes 15.30 + (compile (symbol (str namespace))) 15.31 + 15.32 + ;; make jar 15.33 +;; (jar {:jarfile jar-name :basedir temp-class-dir}) 15.34 + 15.35 + ;; move jar 15.36 +;; (FileUtils/copyFileToDirectory jar-name target-lib-dir) 15.37 + )) 15.38 + 15.39 +(defmulti source-location class) 15.40 + 15.41 +(defmethod source-location clojure.lang.Var [sym] 15.42 + (let [source? (:file (meta sym))] 15.43 + (if source? 15.44 + (.getResource (clojure.lang.RT/baseLoader) source?) 15.45 + nil))) 15.46 + 15.47 +(defmethod source-location java.lang.Class [sym] 15.48 + (let [source? (.getCodeSource (.getProtectionDomain sym))] 15.49 + (if source? 15.50 + (.getLocation source?) 15.51 + nil))) 15.52 + 15.53 +(defn all-dependent-namespaces [namespace] 15.54 + (set 15.55 + (concat 15.56 + [namespace] 15.57 + (vals (ns-aliases namespace)) 15.58 + (map #(.ns %) 15.59 + (filter #(= (class %) clojure.lang.Var) 15.60 + (concat 15.61 + (vals (ns-map namespace)) 15.62 + (vals (ns-refers namespace)))))))) 15.63 + 15.64 +(defn deep-dependent-namespaces [namespace-set] 15.65 + (let [new-namespaces (set (mapcat all-dependent-namespaces namespace-set))] 15.66 + ;;(println (count new-namespaces)) 15.67 + (if (= new-namespaces namespace-set) 15.68 + namespace-set 15.69 + (recur new-namespaces)))) 15.70 + 15.71 +(defn dependencies-url 15.72 + "returns all of the files necessary to succesfully run the namespace." 15.73 + [namespace] 15.74 + (set 15.75 + (remove 15.76 + nil? 15.77 + (map source-location 15.78 + (map second 15.79 + (mapcat ns-map (deep-dependent-namespaces #{namespace}))))))) 15.80 + 15.81 +(defn dependencies-file [namespace] 15.82 + (map file-str 15.83 + (set 15.84 + (map #(.replaceFirst % "file:" "") 15.85 + (map #(if (.contains % ".jar!/") 15.86 + (clojure.string/replace % #"\.jar.*" ".jar") %) 15.87 + (map #(.getFile %) (dependencies-url namespace))))))) 15.88 + 15.89 +(defn classpath-files [] 15.90 + (map file-str (clojure.string/split (classpath) #":"))) 15.91 + 15.92 +;;Every file that comes back from dependencies-file is also on the classpath. 15.93 +;;In order to recreate the right project structure, we need to copy the files 15.94 +;;with the appropiate classpath nesting. 15.95 + 15.96 +(defn bifurcate 15.97 + "split a collection between f and not f" 15.98 + [f coll] 15.99 + (list 15.100 + (filter f coll) 15.101 + (filter (comp not f) coll))) 15.102 + 15.103 +(defn jar? [#^java.io.File f] 15.104 + (re-matches #".*\.jar$" (.getCanonicalPath f))) 15.105 + 15.106 +(defn contains-file? [#^java.io.File parent #^java.io.File child] 15.107 + (let [new-child (.getParentFile child)] 15.108 + (cond (nil? new-child) false 15.109 + (= new-child parent) true 15.110 + true (recur parent new-child)))) 15.111 + 15.112 +(defn destination [base-path-list #^java.io.File current-file #^java.io.File base-destination] 15.113 + (let [base-path (last (filter #(contains-file? % current-file) base-path-list))] 15.114 + (file-str 15.115 + (.replaceFirst (.getCanonicalPath current-file) 15.116 + (.getCanonicalPath base-path) 15.117 + (.getCanonicalPath base-destination))))) 15.118 + 15.119 +(defn export-dependencies [namespace #^java.io.File target] 15.120 + (let [[jars sources] (bifurcate jar? (dependencies-file namespace)) 15.121 + jars (if *nailgun* 15.122 + (conj jars (file-str "~/roBin/nailgun-0.7.1/nailgun-0.7.1.jar")) 15.123 + jars) 15.124 + 15.125 + lib (file-str (str (.getCanonicalPath target) File/separatorChar "lib")) 15.126 + src (file-str (str (.getCanonicalPath target) File/separatorChar "src"))] 15.127 + (if *nailgun* 15.128 + (do 15.129 + (FileUtils/copyFileToDirectory (file-str "~/roBin/nailgun-0.7.1/ng") lib) 15.130 + (sw (str "chmod +x " (.getCanonicalPath target) File/separatorChar "lib/ng")))) 15.131 + (dorun (map #(FileUtils/copyFileToDirectory % lib) jars)) 15.132 + (dorun (map #(FileUtils/copyFile % (destination (classpath-files) % src)) sources)))) 15.133 + 15.134 +(defn run-script-text [namespace] 15.135 + 15.136 + (if *nailgun* 15.137 + (str 15.138 + 15.139 + "#!/usr/bin/perl 15.140 + 15.141 +if (`./lib/ng ng-alias 2>&1 1>/dev/null` eq \"connect: Connection refused\\n\"){ 15.142 + 15.143 + $nailgun_init = 15.144 + \"java -Xmn500M -Xms2000M -Xmx2000M \" . 15.145 + \"-classpath ./src/:lib/*\" . \" -server \". 15.146 + \"com.martiansoftware.nailgun.NGServer \". 15.147 + \" >/dev/null 2>/dev/null &\"; 15.148 + `$nailgun_init`; 15.149 + }\n\n" 15.150 + 15.151 + 15.152 + "while (`./lib/ng ng-alias 2>&1 1>/dev/null` eq \"connect: Connection refused\\n\"){ 15.153 + }\n\n" 15.154 + 15.155 + "$command = " 15.156 + 15.157 + 15.158 + "\"./lib/ng clojure.main " 15.159 + "./src/" 15.160 + (.replace (.replace (str *ns*) \. File/separatorChar) \- \_) 15.161 + ".clj " 15.162 + " @ARGV\";\n" 15.163 + 15.164 + "print `$command`;\n") 15.165 + (str 15.166 + "#!/bin/bash\n" 15.167 + "java -Xmn500M -Xms2000M -Xmx2000M -server -cp ./lib/*:./src clojure.main " 15.168 + "./src/" 15.169 + (.replace (.replace (str *ns*) \. File/separatorChar) \- \_) 15.170 + ".clj " 15.171 + " $@\n"))) 15.172 + 15.173 + 15.174 + 15.175 +(defn make-run-script [namespace base-dir name] 15.176 + (let [w (clojure.java.io/writer 15.177 + (str (.getCanonicalPath base-dir) File/separatorChar name))] 15.178 + (.write w (run-script-text namespace)) 15.179 + (.close w)) 15.180 + (let [f (file-str (str (.getCanonicalPath base-dir) File/separatorChar name))] 15.181 + (.setExecutable f true))) 15.182 + 15.183 +(defn export-files [namespace base-dir name] 15.184 + (export-dependencies namespace base-dir) 15.185 + (make-run-script namespace base-dir name)) 15.186 + 15.187 +(defn bzip-folder [#^java.io.File destination #^java.io.File directory] 15.188 + (apply (partial sw "tar" "-cvjf" (.getCanonicalPath destination)) 15.189 + (concat 15.190 + (rest 15.191 + (map #(.replaceFirst 15.192 + % 15.193 + (str (.getCanonicalPath directory) File/separatorChar ) "") 15.194 + (map str (file-seq directory)))) 15.195 + [:dir (.getCanonicalPath directory)]))) 15.196 + 15.197 +(defn bzip-export-files [directory target-dir name] 15.198 + (apply (partial sw "tar" "-cvjf" 15.199 + (str (.getCanonicalPath target-dir) File/separatorChar name)) 15.200 + (concat 15.201 + [name "lib" "src"] 15.202 + [:dir (.getCanonicalPath directory)]))) 15.203 + 15.204 +(defn export-archive 15.205 + ([name] 15.206 + (export-archive *ns* name)) 15.207 + 15.208 + ([namespace name nailgun?] 15.209 + (binding [*nailgun* nailgun?] 15.210 + (export-archive namespace name))) 15.211 + 15.212 + ([namespace name] 15.213 + (FileUtils/forceMkdir standard-base-directory) 15.214 + (FileUtils/forceDelete standard-base-directory) 15.215 + (FileUtils/forceMkdir standard-base-directory) 15.216 + (let [new-dir 15.217 + (file-str 15.218 + (str 15.219 + (.getCanonicalPath standard-base-directory) 15.220 + (File/separatorChar) 15.221 + name))] 15.222 + 15.223 + (export-files namespace new-dir name) 15.224 + 15.225 + (bzip-export-files new-dir (file-str "~/coderloop/") (str name "-clojure.tar.bz2")) 15.226 + (FileUtils/copyFileToDirectory 15.227 + (file-str (str "~/coderloop/" name "-clojure.tar.bz2")) 15.228 + (file-str "~/coderloop-test")))))
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 16.2 +++ b/src/find_words.clj Tue Oct 18 01:17:49 2011 -0700 16.3 @@ -0,0 +1,174 @@ 16.4 +(ns coderloop.find-words 16.5 + (:use [clojure.contrib def 16.6 + [seq :only [indexed]] 16.7 + [duck-streams :only [read-lines file-str]]] 16.8 + [clojure.java io] 16.9 + [rlm shell-inspect])) 16.10 + 16.11 + 16.12 + 16.13 +"I'm going to solve this using convolution because I like how the kernel 16.14 + looks for this problem. For each word, we build up an asterisk kernel 16.15 + like so: 16.16 + 16.17 + {:word \"Robert\"} 16.18 + kernel: 16.19 + 16.20 + t t t 16.21 + r r r 16.22 + e e e 16.23 + b b b 16.24 + ooo 16.25 + treboRobert 16.26 + ooo 16.27 + b b b 16.28 + e e e 16.29 + r r r 16.30 + t t t 16.31 + 16.32 +isn't it really cute-looking? 16.33 + 16.34 +Anyways, I'll drag these asterisks along the word-matrix via convolution 16.35 +and use them to build up a transient structure that contains only the matches. 16.36 + 16.37 +Then, I'll use that transitnt structure to remove all matches from the original 16.38 +grid. 16.39 + 16.40 +I need stuff to do convolutions, to make kernels given a word, to create the 16.41 +transient structure, and to remove the entries in the transient structure 16.42 +from the original word-grid. I also need a way to represent a word grid." 16.43 + 16.44 +(defvar strcat (partial apply str) 16.45 + "flattens a list by glomming everything together in a string.") 16.46 + 16.47 +(defn word-grid 16.48 + "create a mapping from elements in R^2 to chars" 16.49 + [words] 16.50 + (reduce 16.51 + merge 16.52 + (map (fn [[idx s]] 16.53 + (zipmap 16.54 + (map #(vector (first %) idx) (indexed s)) 16.55 + (seq s))) 16.56 + (indexed words)))) 16.57 + 16.58 + 16.59 +(defn bounding-square 16.60 + "finds the minimal square it takes to bound the grid. works for all 16.61 + geometries." 16.62 + [grid] 16.63 + (let [coords (keys grid) 16.64 + xs (sort (map first coords)) 16.65 + ys (sort (map second coords))] 16.66 + [(first xs) (last xs) (first ys) (last ys)])) 16.67 + 16.68 +;;I have no compunctinos with using mutable state in printing 16.69 +(defn print-grid* [grid] 16.70 + (let [[x-min x-max y-min y-max] (bounding-square grid) 16.71 + canvas (atom 16.72 + (vec (for [y (range (inc (- y-max y-min)))] 16.73 + (vec (repeat (inc (- x-max x-min)) ".")))))] 16.74 + (dorun (map (fn [[[x y] c]] 16.75 + (swap! canvas #(assoc-in % [ (- y y-min) (- x x-min)] c))) grid)) 16.76 + @canvas)) 16.77 + 16.78 +(defn print-grid 16.79 + "nice for debugging but irrelevant for the rest of the problem" 16.80 + [grid] 16.81 + (dorun 16.82 + (for [line (print-grid* grid)] 16.83 + (do (dorun (map print line)) 16.84 + (println))))) 16.85 + 16.86 +(defn asterisk-kernel [word] 16.87 + (let [span (range (inc (count word)))] 16.88 + (vector 16.89 + (zipmap (map #(vector 0 %) span) word) 16.90 + (zipmap (map #(vector 0 (- %)) span) word) 16.91 + (zipmap (map #(vector % %) span) word) 16.92 + (zipmap (map #(vector % (- %)) span) word) 16.93 + (zipmap (map #(vector % 0) span) word) 16.94 + (zipmap (map #(vector (- %) 0) span) word) 16.95 + (zipmap (map #(vector (- %) %) span) word) 16.96 + (zipmap (map #(vector (- %) (- %)) span) word)))) 16.97 + 16.98 +;;this is not lazy :( 16.99 +(defn search-grid-at-point [kernel grid [point-x point-y]] 16.100 + (let [shift-kernel 16.101 + (zipmap 16.102 + (map (fn [[x y]] 16.103 + [(+ x point-x) 16.104 + (+ y point-y)]) 16.105 + (keys kernel)) 16.106 + (vals kernel))] 16.107 + (if (= (select-keys grid (keys shift-kernel)) 16.108 + shift-kernel) 16.109 + shift-kernel nil))) 16.110 + 16.111 +(defn search-word-kernel 16.112 + "search the grid for a particular kernel and store the reusult in the 16.113 + atom (matches)" 16.114 + [grid kernel] 16.115 + (reduce merge (map (fn [point] 16.116 + (search-grid-at-point kernel grid point)) 16.117 + (keys grid)))) 16.118 + 16.119 +(defn search-word [grid word] 16.120 + (let [kernels (asterisk-kernel word)] 16.121 + (reduce merge (map #(search-word-kernel grid %) kernels)))) 16.122 + 16.123 +(defn search-words [grid words] 16.124 + (reduce merge (map #(search-word grid %) words))) 16.125 + 16.126 +(defn remove-matches [grid matches] 16.127 + (apply (partial dissoc grid) (keys matches))) 16.128 + 16.129 +(defn grid->str [grid] 16.130 + (strcat (vals (apply sorted-map 16.131 + (interleave (map (comp vec reverse) (keys grid)) (vals grid)))))) 16.132 + 16.133 +(defn read-wordsearch [#^java.io.File input] 16.134 + (let [[crossword words] (split-with (comp not (partial = "")) (read-lines input)) 16.135 + words (rest words)] 16.136 + [crossword words])) 16.137 + 16.138 +(defn process-wordsearch [[crossword words]] 16.139 + (let [grid (word-grid crossword)] 16.140 + (grid->str (remove-matches grid (search-words grid words))))) 16.141 + 16.142 +(defn doit [args] 16.143 + (println 16.144 + (process-wordsearch 16.145 + (read-wordsearch 16.146 + (file-str (first args)))))) 16.147 + 16.148 +;;******************************************************************************** 16.149 +(if (command-line?) 16.150 + (doit *command-line-args*)) 16.151 + 16.152 + 16.153 +(def input (file-str "/home/r/coderloop-test/input.txt")) 16.154 +(def a (file-str "/home/r/coderloop-test/findwords-a.in")) 16.155 +(def d (file-str "/home/r/coderloop-test/findwords-d.in")) 16.156 +(def c (file-str "/home/r/coderloop-test/findwords-c.in")) 16.157 +(def e (file-str "/home/r/coderloop-test/findwords-e.in")) 16.158 + 16.159 +(def test-grid 16.160 + ["ELGOOGWWW" 16.161 + "TIRXMAHIR" 16.162 + "BATMANZNC" 16.163 + "CMRVLTOLD"]) 16.164 + 16.165 +(def test-words 16.166 + ["MAIL" 16.167 + "WIN" 16.168 + "GOOGLE" 16.169 + "TAR" 16.170 + "BATMAN" 16.171 + "CAR" 16.172 + "WWW" 16.173 + "TOLD" 16.174 + "CD"]) 16.175 + 16.176 + 16.177 +
17.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 17.2 +++ b/src/hello_sql.clj Tue Oct 18 01:17:49 2011 -0700 17.3 @@ -0,0 +1,56 @@ 17.4 +(ns coderloop.hello-sql 17.5 + (:refer-clojure :only []) 17.6 + (:require rlm.ns-rlm rlm.light-base)) 17.7 +(rlm.ns-rlm/ns-clone rlm.light-base) 17.8 +(use 'coderloop.utils) 17.9 + 17.10 +(use 'clojure.contrib.sql) ;;' satisfy prettify 17.11 +(import 'org.gjt.mm.mysql.Driver) 17.12 + 17.13 +(def *port* 3306) 17.14 + 17.15 +(def *host* "localhost") 17.16 + 17.17 +(defn doit [] 17.18 + (let [db-host "localhost" 17.19 + db-port *port* 17.20 + db-name "testdb"] 17.21 + (def db {:classname "com.mysql.jdbc.Driver" 17.22 + :subprotocol "mysql" 17.23 + :subname (str "//" db-host ":" db-port "/" db-name) 17.24 + :user "root" 17.25 + :password "sql1005025"}) 17.26 + (with-connection db 17.27 + (with-query-results rs ["select * from hello_data"] 17.28 + (println rs))))) 17.29 + 17.30 + 17.31 +(defn read-hello-data [[username password db-name]] 17.32 + (let [db-host *host* 17.33 + db-port *port*] 17.34 + (def db {:classname "com.mysql.jdbc.Driver" 17.35 + :subprotocol "mysql" 17.36 + :subname (str "//" db-host ":" db-port "/" db-name) 17.37 + :user username 17.38 + :password password}) 17.39 + (with-connection db 17.40 + (with-query-results rs ["select * from hello_data"] 17.41 + (vec rs))))) 17.42 + 17.43 + 17.44 + 17.45 +(defn print-result [args] 17.46 + (let [results (read-hello-data args)] 17.47 + (println 17.48 + (apply str (interpose " " (map :text results)))))) 17.49 + 17.50 + 17.51 +(defn slick-test [] (read-hello-data ["root" "sql1005025" "testdb"])) 17.52 + 17.53 + 17.54 +(if (command-line?) 17.55 + (print-result *command-line-args*)) 17.56 + 17.57 +;; rs will be a sequence of maps, 17.58 +;; one for each record in the result set. 17.59 +
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 18.2 +++ b/src/hello_world.clj Tue Oct 18 01:17:49 2011 -0700 18.3 @@ -0,0 +1,11 @@ 18.4 +(ns coderloop.hello-world 18.5 + (:use rlm.shell-inspect)) 18.6 + 18.7 +(defn hello-world [] 18.8 + (println "Hello World!")) 18.9 + 18.10 +(if (rlm.shell-inspect/command-line?) 18.11 + (hello-world)) 18.12 + 18.13 + 18.14 +
19.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 19.2 +++ b/src/lying.clj Tue Oct 18 01:17:49 2011 -0700 19.3 @@ -0,0 +1,88 @@ 19.4 +(ns coderloop.lying 19.5 + (:use [clojure.contrib 19.6 + [duck-streams :only [read-lines]] 19.7 + [seq :only [find-first]] 19.8 + [str-utils :only [re-gsub]]]) 19.9 + (:use rlm.shell-inspect) 19.10 + (:use rlm.map-utils) 19.11 + (:use [clojure [string :only [split trim]]])) 19.12 + 19.13 + 19.14 + 19.15 +(comment 19.16 +6 19.17 +JohnDoe - 1 19.18 +MisterBlanko 19.19 +JohnTitor - 2 19.20 +DirkWhatever 19.21 +BrunoJoeConner 19.22 +DirkWhatever - 1 19.23 +FooBar 19.24 +BrunoJoeConner - 2 19.25 +JohnTitor 19.26 +MisterBlanko 19.27 +MisterBlanko - 1 19.28 +DirkWhatever 19.29 +FooBar - 3 19.30 +DirkWhatever 19.31 +BrunoJoeConner 19.32 +JohnDoe 19.33 +) 19.34 + 19.35 + 19.36 +(def witnesses 19.37 + {:john-doe [:mister-blanco] 19.38 + :john-tator [:dirk-whatever :bruno-joe-conner] 19.39 + :dirk-whatever [:foo-bar] 19.40 + :bruno-joe-conner [:john-tator :mister-blanco] 19.41 + :mister-blanco [:dirk-whatever] 19.42 + :foo-bar [:dirk-whatever :bruno-joe-conner :john-doe]}) 19.43 + 19.44 +(def t "/home/r/coderloop-test/test.txt") 19.45 +(def a "/home/r/coderloop-test/cluedo-a.in") 19.46 + 19.47 +(defn parse-file [f] 19.48 + (let [lines (map clojure.string/trim (rest (read-lines f)))] 19.49 + (map-keys 19.50 + (comp (partial re-gsub #"\W*:\W*\d*\W*" "") first) 19.51 + (apply hash-map 19.52 + (partition-by #(re-matches #".*:.*" %) lines))))) 19.53 + 19.54 + 19.55 +(defrecord verdict [good bad]) 19.56 + 19.57 +(def initial-verdict (verdict. #{} #{})) 19.58 + 19.59 +(defn valid-verdict? [num verdict] 19.60 + (and 19.61 + (>= num (+ (count (:good verdict)) (count (:bad verdict)))) 19.62 + (not (reduce #(or %1 %2) 19.63 + (map (:good verdict) (:bad verdict)))))) 19.64 + 19.65 +(defn expand-verdict [judgements [person accuse]] 19.66 + (let [y (flatten 19.67 + (for [record judgements] 19.68 + [(verdict. (into (:good record) (vector person)) 19.69 + (into (:bad record) accuse)) 19.70 + (verdict. (into (:good record) accuse) 19.71 + (into (:bad record) (vector person)))]))] 19.72 +;; (println (count y)) 19.73 +;; (dorun (map println y)) 19.74 + y)) 19.75 + 19.76 +(defn determine-guilt [witnesses] 19.77 + (let [valid? (partial valid-verdict? (count witnesses))] 19.78 + (filter valid? 19.79 + (reduce (comp 19.80 + (partial filter valid?) 19.81 + expand-verdict) 19.82 + [initial-verdict] witnesses)))) 19.83 + 19.84 +(defn print-soln [witnesses] 19.85 + (let [verdict (first (determine-guilt witnesses)) 19.86 + group-nums (sort [(count (:good verdict)) (count (:bad verdict))])] 19.87 + (println (str (first group-nums) \: (second group-nums))))) 19.88 + 19.89 + 19.90 +(if (command-line?) 19.91 + (print-soln (parse-file (first *command-line-args*))))
20.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 20.2 +++ b/src/make-collatz-java Tue Oct 18 01:17:49 2011 -0700 20.3 @@ -0,0 +1,7 @@ 20.4 +#!/bin/bash 20.5 + 20.6 +mkdir /home/r/coderloop-test/src; 20.7 +mkdir /home/r/coderloop-test/src/coderloop; 20.8 +cp Collatz.java /home/r/coderloop-test/src/coderloop/Collatz.java; 20.9 +cp Collatz.java.run /home/r/coderloop-test/euler14 20.10 +cp Collatz.java.ant.xml /home/r/coderloop-test/build.xml
21.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 21.2 +++ b/src/meep_meep.clj Tue Oct 18 01:17:49 2011 -0700 21.3 @@ -0,0 +1,5 @@ 21.4 +(ns coderloop.meep-meep) 21.5 + 21.6 +(def include true) 21.7 + 21.8 +(println "Meep meep!")
22.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 22.2 +++ b/src/meepmeep.py Tue Oct 18 01:17:49 2011 -0700 22.3 @@ -0,0 +1,2 @@ 22.4 +#!/usr/bin/python 22.5 +print "Meep meep!"
23.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 23.2 +++ b/src/parallel_io.clj Tue Oct 18 01:17:49 2011 -0700 23.3 @@ -0,0 +1,158 @@ 23.4 +(ns coderloop.parallel-io 23.5 + (:refer-clojure :only []) 23.6 + (:require rlm.ns-rlm rlm.light-base)) 23.7 +(rlm.ns-rlm/ns-clone rlm.light-base) 23.8 + 23.9 +(import '[java.io FileInputStream RandomAccessFile]) 23.10 +(use '(clojure.contrib [str-utils2 :only [join]])) 23.11 +(import 'java.util.concurrent.atomic.AtomicLong) 23.12 + 23.13 +;;(set! *warn-on-reflection* true) 23.14 + 23.15 + 23.16 + 23.17 + 23.18 + 23.19 +;;;; Reading 23.20 + 23.21 +(defn chunk-file 23.22 + "Partitions a file into n line-aligned chunks. Returns a list of start and 23.23 + end byte offset pairs." 23.24 + [filename n] 23.25 + (with-open [file (java.io.RandomAccessFile. filename "r")] 23.26 + (let [offsets (for [offset (range 0 (.length file) (/ (.length file) n))] 23.27 + (do (when-not (zero? offset) 23.28 + (.seek file offset) 23.29 + (while (not= (.read file) (int \newline)))) 23.30 + (.getFilePointer file))) 23.31 + offsets (concat offsets [(.length file)])] 23.32 + (doall (partition 2 (interleave offsets (rest offsets))))))) 23.33 + 23.34 + 23.35 +(defn read-lines-range [file start-byte end-byte] 23.36 + "Returns a lazy sequence of lines from file between start-byte and end-byte." 23.37 + (let [reader (-> (doto (java.io.FileInputStream. file) 23.38 + (.skip start-byte)) 23.39 + (java.io.BufferedInputStream. (* 8 131072)) 23.40 + (java.io.InputStreamReader. "US-ASCII") 23.41 + (java.io.BufferedReader. 131072))] 23.42 + (letfn [(read-line [remaining] 23.43 + (lazy-seq 23.44 + (if-let [line (and (pos? remaining) (.readLine reader))] 23.45 + (cons line (read-line (- remaining (.length line)))) 23.46 + (.close reader))))] 23.47 + (read-line (- end-byte start-byte))))) 23.48 + 23.49 +(defn #^"[Ljava.lang.String;" dumbest-split 23.50 + [#^String s c #^"[Ljava.lang.String;" tokens] 23.51 + (let [len (dec (int (alength tokens)))] 23.52 + (loop [start (int 0) 23.53 + i (int 0)] 23.54 + (let [idx (int (.indexOf s (int c) (int start)))] 23.55 + (if (or (neg? idx) (>= i len)) 23.56 + (do (aset tokens i (.substring s start)) 23.57 + tokens) 23.58 + (do (aset tokens i (.substring s start idx)) 23.59 + (recur (inc idx) (inc i)))))))) 23.60 + 23.61 +(defn parse-lines [lines] 23.62 + (let [ary (make-array String 12)] 23.63 + (for [#^String line lines 23.64 + :let [fields (dumbest-split line \space ary) 23.65 + status (aget fields 8) 23.66 + bytes (aget fields 9) 23.67 + #^String ref (aget fields 10)] 23.68 + :when (= (aget fields 5) "\"GET") 23.69 + :when ('#{"200" "304" "404"} status)] 23.70 + {:client (aget fields 0) 23.71 + :url (aget fields 6) 23.72 + :status status 23.73 + :bytes (if (= bytes "-") 0 (Long/parseLong bytes)) 23.74 + :ref (.substring ref 1 (dec (count ref)))}))) 23.75 + 23.76 +;;;; Tallying 23.77 + 23.78 +(defn bump! [map-atom #^String key #^Long delta] 23.79 + (if-let [#^AtomicLong counter (get @map-atom key)] 23.80 + (.addAndGet counter delta) 23.81 + (swap! map-atom #(assoc % (String. key) 23.82 + (if-let [#^AtomicLong counter (get % key)] 23.83 + (AtomicLong. (+ (.get counter) delta)) 23.84 + (AtomicLong. delta)))))) 23.85 + 23.86 +(def article-re #"^/ongoing/When/\d\d\dx/\d\d\d\d/\d\d/\d\d/[^ .]+$") 23.87 + 23.88 +(defn tally! [{:keys [url-hits url-bytes clients refs s404s]} records] 23.89 + (doseq [{:keys [#^String url bytes client status #^String ref]} records] 23.90 + (if (= status "404") 23.91 + (bump! s404s url 1) 23.92 + (do (bump! url-bytes url bytes) 23.93 + (when (when (.startsWith url "/ongoing/When/") 23.94 + (re-matches article-re url)) 23.95 + (bump! url-hits url 1) 23.96 + (bump! clients client 1) 23.97 + (when-not (or (= ref "-") 23.98 + (.startsWith ref "http://www.tbray.org/ongoing/")) 23.99 + (bump! refs ref 1))))))) 23.100 + 23.101 +;;;; Reporting 23.102 + 23.103 +(defn truncate [s n] 23.104 + (if (> (count s) n) (str (.substring s 0 n) "...") s)) 23.105 + 23.106 +(defn print-top10 [results label & [shrink?]] 23.107 + (println "Top" label) 23.108 + (let [fmt (if shrink? " %9.1fM: %s" " %10d: %s")] 23.109 + (doseq [[k v] (take 10 results)] 23.110 + (let [v (if shrink? (/ v 1024.0 1024.0) (long v))] 23.111 + (println (format fmt v (truncate k 60)))))) 23.112 + (println)) 23.113 + 23.114 +(defn sort-by-vals-desc [m] 23.115 + (sort-by #(- (val %)) m)) 23.116 + 23.117 + 23.118 + 23.119 +(defn take-greatest-vals [n m] 23.120 + (when-let [m (seq m)] 23.121 + (reduce (fn [best x] 23.122 + (if (>= (val x) (val (last best))) 23.123 + (vec (take n (sort-by-vals-desc (conj best x)))) 23.124 + best)) 23.125 + [(first m)] (rest m)))) 23.126 + 23.127 +(defn report [tallies state] 23.128 + (->> state 23.129 + (map (fn [[tally rows]] (str (count @rows) " " (name tally)))) 23.130 + (join ", ") 23.131 + (println)) 23.132 + (println) 23.133 + (->> tallies 23.134 + (pmap (fn [[tally & options]] 23.135 + (cons (take-greatest-vals 10 (sort-by-vals-desc @(state tally))) 23.136 + options))) 23.137 + (map #(apply print-top10 %)) 23.138 + (dorun))) 23.139 + 23.140 + ;;;; Main 23.141 + 23.142 +(def tallies [[:url-hits "URIs by hit"] 23.143 + [:url-bytes "URIs by bytes" :shrink] 23.144 + [:s404s "404s"] 23.145 + [:clients "client addresses"] 23.146 + [:refs "referrers"]]) 23.147 + 23.148 +(defn wf-atoms [file] 23.149 + (let [chunk-count (int (/ (.length (java.io.File. file)) (* 32 1024 1024))) 23.150 + state (zipmap (map first tallies) (repeatedly #(atom {})))] 23.151 + (dorun 23.152 + (pmap (fn [[idx [start end]]] 23.153 + (println (str "Chunk " idx "/" chunk-count 23.154 + " (" start " -> " end ")")) 23.155 + (->> (read-lines-range file start end) 23.156 + (parse-lines) 23.157 + (tally! state))) 23.158 + (indexed (chunk-file file chunk-count)))) 23.159 + (time (report tallies state)))) 23.160 + 23.161 +
24.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 24.2 +++ b/src/reverse_sentence.clj Tue Oct 18 01:17:49 2011 -0700 24.3 @@ -0,0 +1,28 @@ 24.4 +(ns coderloop.reverse-sentence 24.5 + (:refer-clojure :only []) 24.6 + (:require rlm.ns-rlm rlm.light-base)) 24.7 +(rlm.ns-rlm/ns-clone rlm.light-base) 24.8 + 24.9 + 24.10 +(undef partition) 24.11 +(use '[clojure.contrib [string :only [partition]]]) 24.12 +(use '[clojure [string :only [blank?]]]) 24.13 + 24.14 +(defn reverse-line [s] 24.15 + (apply str 24.16 + (flatten 24.17 + (map #(if (re-matches #"\w" (str (first %))) (reverse %) %) 24.18 + (filter (comp not (partial = "")) 24.19 + (partition #"\w+" s)))))) 24.20 + 24.21 + 24.22 +(defn reverse-file [file] 24.23 + (doall 24.24 + (map println 24.25 + (map reverse-line 24.26 + (read-lines file))))) 24.27 + 24.28 + 24.29 +(if (command-line?) 24.30 + (reverse-file (first *command-line-args*))) 24.31 +
25.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 25.2 +++ b/src/reverse_seq.clj Tue Oct 18 01:17:49 2011 -0700 25.3 @@ -0,0 +1,43 @@ 25.4 +(ns coderloop.reverse-seq 25.5 + (:use [clojure.contrib 25.6 + [duck-streams :only [file-str read-lines]] 25.7 + [str-utils :only [re-split]]]) 25.8 + (:use rlm.shell-inspect) 25.9 + (:use [clojure [string :only [trim]]])) 25.10 + 25.11 +(def c (file-str "/home/r/coderloop-test/reversesequence-c.in")) 25.12 +(def a (file-str "/home/r/coderloop-test/reversesequence-a.in")) 25.13 +(def b (file-str "/home/r/coderloop-test/reversesequence-b.in")) 25.14 + 25.15 +(defn test-trace-map [] 25.16 + (= [:a :b :c :d :e] 25.17 + (trace-map {:a :b :d :e :c :d :b :c}))) 25.18 + 25.19 + 25.20 +(defn read-name-map 25.21 + "takes a file of pairs of names and returns a map from the first 25.22 + element in those pairs to the second element in the pairs." 25.23 + [#^java.io.File file] 25.24 + (let [pairs (map #(re-split #"\W+" (trim %)) (read-lines file))] 25.25 + (zipmap (map first pairs) (map second pairs)))) 25.26 + 25.27 +(defn start-linked-map 25.28 + "takes a map-representing-a-list and finds the beginning of that list" 25.29 + [m] 25.30 + (first (remove (set (vals m)) (keys m)))) 25.31 + 25.32 +(defn trace-map 25.33 + "chains through a map-representing-a-list, building a list along the way." 25.34 + [m] 25.35 + (loop [index (start-linked-map m) 25.36 + chain (vector index)] 25.37 + (let [new-index (get m index ::sentinel)] 25.38 + (if (= new-index ::sentinel) 25.39 + chain 25.40 + (recur new-index (conj chain new-index)))))) 25.41 + 25.42 + 25.43 + 25.44 +(if (command-line?) 25.45 + (dorun 25.46 + (map println (reverse (trace-map (read-name-map (file-str (first *command-line-args*))))))))
26.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 26.2 +++ b/src/reverse_seq.pl Tue Oct 18 01:17:49 2011 -0700 26.3 @@ -0,0 +1,43 @@ 26.4 +#!/usr/bin/perl 26.5 + 26.6 +$a = "/home/r/coderloop-test/reversesequence-a.in"; 26.7 +$b = "/home/r/coderloop-test/reversesequence-b.in"; 26.8 +$c = "/home/r/coderloop-test/reversesequence-c.in"; 26.9 + 26.10 +%test_map = ("a" => "b", "b" => "c"); 26.11 + 26.12 +sub create_map{ 26.13 + ## creates a "linked map" from a string representing 26.14 + ## the input file. 26.15 + (my $file) = @_; 26.16 + my %map; 26.17 + open FILE, "<".$file or die $!; 26.18 + for (<FILE>){ chomp; /^(\w+)\W+(\w+)$/; $map{$1} = $2;} 26.19 + close FILE; 26.20 + return %map;} 26.21 + 26.22 +sub find_beginning{ 26.23 + ## takes a map representing a linked list and 26.24 + ## returns the beginning of that list. 26.25 + (my %map) = @_; 26.26 + my %val_set = map {$_ => 1} (values %map); 26.27 + my $start; 26.28 + foreach (keys %map){ 26.29 + if (!($val_set{$_})){$start = $_;last}} 26.30 + return $start;} 26.31 + 26.32 +sub trace_map{ 26.33 + ## crawls through a map representing a linked-list 26.34 + ## and converts it into an actual array structure. 26.35 + (my %map) = @_; 26.36 + my $position = &find_beginning(%map); 26.37 + my @array = ($position); 26.38 + while(my $new_position = $map{$position}){ 26.39 + push (@array, $new_position); 26.40 + $position = $new_position;} 26.41 + return @array;} 26.42 + 26.43 + 26.44 + 26.45 +if (defined @ARGV){ 26.46 + map {print;print"\n";} reverse(trace_map(create_map($ARGV[0])));}
27.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 27.2 +++ b/src/reverse_sequence.clj Tue Oct 18 01:17:49 2011 -0700 27.3 @@ -0,0 +1,36 @@ 27.4 +(ns coderloop.reverse-sequence) 27.5 +(use 'clojure.contrib.str-utils) 27.6 +;;(use 'rlm.shell-inspect) 27.7 + 27.8 +;(def *command-line-args* (list "/home/ocsenave/reversesequence-a.in")) 27.9 +(def pairs (if (nil? *command-line-args*) '() (map #(re-split #" " %) (re-split #"\r\n" (slurp (first *command-line-args*)))))) 27.10 +(def p(apply hash-map(reverse(flatten pairs)))) 27.11 +(print(str-join "\n"((fn l[x](if(nil? x)x (cons x(l(p x))) )) 27.12 + (first (remove(set(keys p))(vals p))))) "\n") 27.13 + 27.14 + 27.15 + 27.16 + 27.17 + 27.18 + 27.19 + 27.20 +;; (ns coderloop.reverse-sequence) 27.21 +;; (use 'clojure.contrib.str-utils) 27.22 +;; ;;(use 'rlm.shell-inspect) 27.23 + 27.24 +;; (defn extension[f ip op] (fn[x](if (= x ip) op (f x)))) 27.25 + 27.26 +;; (let[ 27.27 +;; ;;*command-line-args* (list "/home/ocsenave/testfile.txt") 27.28 +;; pairs (if (nil? *command-line-args*) '() (map #(re-split #" " %) (re-split #"\n" (slurp(first *command-line-args*))))) 27.29 +;; predecessor (reduce #(extension %1 (second %2)(first %2)) (fn[x]nil) pairs) 27.30 +;; terminus (first(remove(set(map first pairs))(map second pairs))) 27.31 +;; ] 27.32 +;; (defn lineage[x](if(nil? x)nil (cons x(lineage (predecessor x))))) 27.33 +;; (print (str-join "\n" (lineage terminus))) 27.34 +;; ) 27.35 + 27.36 + 27.37 +;;(if (command-line?) 27.38 +;; (do-something-to *command-line-args*)) 27.39 +
28.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 28.2 +++ b/src/scientist2.clj Tue Oct 18 01:17:49 2011 -0700 28.3 @@ -0,0 +1,197 @@ 28.4 +(ns coderloop.scientist2 28.5 + (:refer-clojure :only []) 28.6 + (:require rlm.ns-rlm rlm.light-base)) 28.7 +(rlm.ns-rlm/ns-clone rlm.light-base) 28.8 + 28.9 + 28.10 +(use 'clojure.contrib.sql) 28.11 +(import 'org.gjt.mm.mysql.Driver) 28.12 +(use 'clojure.contrib.sql) 28.13 +(undef distinct case compile drop take sort) 28.14 +(use 'clojureql.core) 28.15 +(import 'java.io.File) 28.16 +(use '[clojure.java.io :only [copy]]) 28.17 +(use '[clojure.contrib.shell-out :only [sh]]) 28.18 +(use '[clojure.string :only [split]]) 28.19 +(use '[coderloop [utils :only [md5]]]) 28.20 +(import 'java.sql.Date) 28.21 + 28.22 + 28.23 + 28.24 +(def *port* 3306) 28.25 + 28.26 +(def *host* "localhost") 28.27 + 28.28 +(def *db-host* "localhost") 28.29 + 28.30 +(def *db-name* "clojure_rlm") 28.31 + 28.32 +(def *db* {:classname "com.mysql.jdbc.Driver" 28.33 + :subprotocol "mysql" 28.34 + :subname (str "//" *db-host* ":" *port* "/" *db-name*) 28.35 + :user "root" 28.36 + :password "sql1005025"}) 28.37 + 28.38 +(def databases [:essay_scientists :scientists :ideas :scientist_whereabouts]) 28.39 + 28.40 + 28.41 + 28.42 + 28.43 +(declare essays essay century contemps data eric scientists date->str date) 28.44 + 28.45 +(defn scientists [] 28.46 + @(table *db* :essay_scientists)) 28.47 + 28.48 + 28.49 +(defn contemps [s birth death] 28.50 + (sort-by (fn [s] [(:surname s) (:name s)]) 28.51 + (map #(select-keys % [:name :surname]) 28.52 + @(select 28.53 + (table *db* :scientists) 28.54 + (where 28.55 + (and 28.56 + (< :birth_date death) 28.57 + (< birth :death_date) 28.58 + (not (= :name (:name s))))))) )) 28.59 + 28.60 +(defn data [s] 28.61 + (let [locations 28.62 + (sort-by 28.63 + :immigration_date 28.64 + (map #(select-keys % [:immigration_date :emigration_date :country]) 28.65 + @(select 28.66 + (table *db* :scientist_whereabouts) 28.67 + (where 28.68 + (and 28.69 + (= :scientist_whereabouts.surname (:surname s)) 28.70 + (= :scientist_whereabouts.name (:name s))))))) 28.71 + ideas 28.72 + (sort-by 28.73 + #(.toLowerCase %) 28.74 + (map :idea 28.75 + @(select 28.76 + (table *db* :ideas) 28.77 + (where 28.78 + (and 28.79 + (= :ideas.surname (:surname s)) 28.80 + (= :ideas.name (:name s))))))) 28.81 + data 28.82 + (first 28.83 + @(select 28.84 + (table *db* :scientists) 28.85 + (where 28.86 + (and 28.87 + (= :scientists.surname (:surname s)) 28.88 + (= :scientists.name (:name s)))))) 28.89 + birth (:birth_date data) 28.90 + death (:death_date data) 28.91 + contemp (contemps s birth death)] 28.92 + (merge data {:locations locations :contemps contemp :ideas ideas}))) 28.93 + 28.94 +(defn date->str [#^java.sql.Date d] 28.95 + (format "%02d/%02d/%04d" (.getDate d) (inc (.getMonth d)) (+ 1900 (.getYear d)))) 28.96 + 28.97 +(def strcat (partial apply str)) 28.98 + 28.99 +(defn century [#^java.sql.Date d] 28.100 + (* 100 (clojure.core/unchecked-divide (+ 1900 (.getYear d)) 100))) 28.101 + 28.102 +(defn essay-map [s] 28.103 + (let [base (data s) 28.104 + locations 28.105 + (strcat 28.106 + (interpose 28.107 + ", " 28.108 + (map (fn [{:keys [emigration_date immigration_date country ]}] 28.109 + (str country " from " (date->str immigration_date) " to " 28.110 + (date->str emigration_date))) (:locations base)))) 28.111 + contemps 28.112 + (strcat 28.113 + (interpose 28.114 + ", " 28.115 + (map (fn [{:keys [name surname]}] 28.116 + (str name " " surname)) 28.117 + (:contemps base)))) 28.118 + birth-date (date->str (:birth_date base)) 28.119 + death-date (date->str (:death_date base)) 28.120 + century (century (:birth_date base)) 28.121 + lifespan 28.122 + (str "[" 28.123 + (:birth_place base) " " birth-date ", " 28.124 + (:death_place base) " " death-date 28.125 + "]") 28.126 + ideas (strcat (interpose ", " (:ideas base))) 28.127 + ] 28.128 + {:lifespan lifespan :name (:name base) :surname (:surname base) 28.129 + :contemps contemps :locations locations :century century :ideas ideas})) 28.130 + 28.131 +(defn essay [s] 28.132 + (let [{:keys [lifespan name surname contemps locations century ideas]} (essay-map s)] 28.133 + (str name " " surname " " lifespan " is one of the most famous scientists of " 28.134 + century ". " 28.135 + "Between all " name "'s ideas we mention: " ideas ". " 28.136 + name " lived in: " locations ". " 28.137 + name " was a contemporary of " contemps ".\n\n"))) 28.138 + 28.139 +(defn essays [] 28.140 + (map essay (scientists))) 28.141 + 28.142 +(defn print-essays [] 28.143 + (dorun (map print (essays)))) 28.144 + 28.145 +(defn essays->str [] 28.146 + (strcat (essays))) 28.147 + 28.148 +(defn main [[username password db-name]] 28.149 + (let [db-host *host* 28.150 + db-port *port* 28.151 + db {:classname "com.mysql.jdbc.Driver" 28.152 + :subprotocol "mysql" 28.153 + :subname (str "//" db-host ":" db-port "/" db-name) 28.154 + :user username 28.155 + :password password}] 28.156 + (binding [*db* db] 28.157 + (print-essays)))) 28.158 + 28.159 +(if (command-line?) 28.160 + (main *command-line-args*)) 28.161 + 28.162 + 28.163 + 28.164 + 28.165 + 28.166 +(def eric {:name "Erik 3802" :surname "Green 3802"}) 28.167 +(def date (java.sql.Date. 4564654645)) 28.168 + 28.169 + 28.170 + 28.171 +(defvar error-person 28.172 + {:name "Albert 3850" :surname "Newton 3850"} 28.173 + "this is the person in in which John 5750 is supposedly 28.174 + wrongly included my code says they're contempoaries, 28.175 + but the expected output disaggrees ") 28.176 + 28.177 +(defvar wrong-include 28.178 + {:name "John 5750" :surname "Newton 5750"} 28.179 + "this one is inculded and supposedly should not be.") 28.180 + 28.181 +;;coderloop.scientist2> (select-keys (data error-person) [:birth_date :death_date]) 28.182 +;;{:death_date #<Date 0226-10-18>, :birth_date #<Date 0166-10-16>} 28.183 +;;coderloop.scientist2> (select-keys (data wrong-include) [:birth_date :death_date]) 28.184 +;;{:death_date #<Date 0226-01-01>, :birth_date #<Date 0166-12-30>} 28.185 + 28.186 +;; these exibit the same problem. They appear to be contemps, 28.187 +;; but the expected output says that they're not! 28.188 +(defvar error-person2 28.189 + {:name "Erik 3851" :surname "Smith 3851"}) 28.190 + 28.191 +(defvar wrong-include2 28.192 + {:name "Gil 5751" :surname "Smith 5751"}) 28.193 + 28.194 +;;coderloop.scientist2> (select-keys (data error-person2) [:birth_date :death_date]) 28.195 +;;{:death_date #<Date 0227-10-19>, :birth_date #<Date 0167-10-17>} 28.196 +;;coderloop.scientist2> (select-keys (data wrong-include2) [:birth_date :death_date]) 28.197 +;;{:death_date #<Date 0227-01-02>, :birth_date #<Date 0167-12-31>} 28.198 + 28.199 + 28.200 +
29.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 29.2 +++ b/src/scientist_sql.clj Tue Oct 18 01:17:49 2011 -0700 29.3 @@ -0,0 +1,317 @@ 29.4 +(ns coderloop.scientist-sql 29.5 + (:refer-clojure :only []) 29.6 + (:require rlm.ns-rlm rlm.light-base)) 29.7 +(rlm.ns-rlm/ns-clone rlm.light-base) 29.8 + 29.9 + 29.10 +(use 'clojure.contrib.sql) ;;' satisfy prettify 29.11 +(import 'org.gjt.mm.mysql.Driver) 29.12 +(use 'clojure.contrib.sql) 29.13 +(import '[org.joda.time LocalDate ]) 29.14 +(undef distinct case compile drop take sort) 29.15 +(use 'clojureql.core) 29.16 +(import 'org.joda.time.DateMidnight) 29.17 +(import 'java.io.File) 29.18 +(use '[clojure.java.io :only [copy]]) 29.19 +(use '[clojure.contrib.shell-out :only [sh]]) 29.20 +(use '[clojure.string :only [split]]) 29.21 +(use '[coderloop [utils :only [md5]]]) 29.22 + 29.23 +(defn sql-date [date] 29.24 + "Convert any Joda-readable date object (including a string) to a java.sql.Date" 29.25 + (java.sql.Date. (.. (LocalDate. date) toDateMidnight toInstant getMillis))) 29.26 + 29.27 +(def *port* 3306) 29.28 + 29.29 +(def *host* "localhost") 29.30 + 29.31 +(def *db-host* "localhost") 29.32 + 29.33 +(def *db-name* "clojure_rlm") 29.34 + 29.35 +(def *db* {:classname "com.mysql.jdbc.Driver" 29.36 + :subprotocol "mysql" 29.37 + :subname (str "//" *db-host* ":" *port* "/" *db-name*) 29.38 + :user "root" 29.39 + :password "sql1005025"}) 29.40 + 29.41 +(def *db2* {:classname "com.mysql.jdbc.Driver" 29.42 + :subprotocol "mysql" 29.43 + :subname (str "//" *db-host* ":" *port* "/" "test_rlm") 29.44 + :user "root" 29.45 + :password "sql1005025"}) 29.46 + 29.47 + 29.48 +(def databases [:essay_scientists :scientists :ideas :scientist_whereabouts]) 29.49 + 29.50 +(defmacro no-exceptions 29.51 + "Sweet relief like I never knew." 29.52 + [& forms] 29.53 + `(try ~@forms (catch Exception e# (.printStackTrace e#)))) 29.54 + 29.55 + 29.56 +(defn make-essay-scientists [] 29.57 + (with-connection 29.58 + *db2* 29.59 + (no-exceptions (drop-table :essay_scientists)) 29.60 + (create-table 29.61 + :essay_scientists 29.62 + [:id "integer"] 29.63 + [:name "varchar(255)"] 29.64 + [:surname "varchar(255)"]) 29.65 + (insert-rows :essay_scientists 29.66 + [1 "Isaac" "Newton"] 29.67 + [2 "Albert" "Einstein"]))) 29.68 + 29.69 +(defn make-scientists [] 29.70 + (with-connection 29.71 + *db2* 29.72 + (no-exceptions (drop-table :scientists)) 29.73 + (create-table 29.74 + :scientists 29.75 + [:name "varchar(255)"] 29.76 + [:surname "varchar(255)"] 29.77 + [:birth_place "varchar(255)"] 29.78 + [:death_place "varchar(255)"] 29.79 + [:birth_date "date"] 29.80 + [:death_date "date"]) 29.81 + (insert-rows 29.82 + :scientists 29.83 + ["Albert" "Einstein" "Ulm" "Princeton" "1879-3-14" "1955-4-18"] 29.84 + ["Galileo" "Galilei" "Pisa" "Arcetri" "1564-2-15" "1645-1-8"] 29.85 + ["Enrico" "Fermi" "Roma" "Chicago" "1901-9-29" "1954-9-28"] 29.86 + ["Isaac" "Newton" "Woolsthorpe-by-Colsterworth" "London" "1643-1-4" "1727-3-31"] 29.87 + ["Marx" "Planck" "Kiel" "Göttingen" "1858-4-23" "1947-10-4"]))) 29.88 + 29.89 + 29.90 +(defn make-scientist-whereabouts [] 29.91 + (with-connection *db2* 29.92 + (no-exceptions (drop-table :scientist_whereabouts )) 29.93 + (create-table 29.94 + :scientist_whereabouts 29.95 + [:name "varchar(255)"] 29.96 + [:surname "varchar(255)"] 29.97 + [:country "varchar(255)"] 29.98 + [:immigration_date "date"] 29.99 + [:emigration_date "date"]) 29.100 + (insert-rows 29.101 + :scientist_whereabouts 29.102 + ["Galileo" "Galilei" "France" "1602-2-18" "1604-4-20"] 29.103 + ["Albert" "Einstein" "Uk" "1901-10-22" "1905-5-4"] 29.104 + ["Marx" "Planck" "Denmark" "1901-1-31" "1945-7-4"] 29.105 + ["Albert" "Einstein" "France" "1908-11-7" "1930-3-12"] 29.106 + ["Enrico" "Fermi" "Us" "1924-12-19" "1954-9-28"] 29.107 + ["Isaac" "Newton" "Uk" "1688-9-8" "1690-7-12"] 29.108 + ["Albert" "Einstein" "US" "1935-10-22" "1955-4-18"] 29.109 + ["Isaac" "Newton" "Germany" "1690-9-28" "1705-4-6"]))) 29.110 + 29.111 + 29.112 +(defn make-ideas [] 29.113 + (with-connection *db2* 29.114 + (no-exceptions (drop-table :ideas)) 29.115 + (create-table 29.116 + :ideas 29.117 + [:name "varchar(255)"] 29.118 + [:surname "varchar(255)"] 29.119 + [:idea "varchar(255)"]) 29.120 + (insert-rows 29.121 + :ideas 29.122 + ["Albert" "Einstein" "Theory of relativity (general)"] 29.123 + ["Enrico" "Fermi" "First nuclear reactor"] 29.124 + ["Albert" "Einstein" "Photoelectric effect"] 29.125 + ["Isaac" "Newton" "Gravity"] 29.126 + ["Galileo" "Galilei" "Telescope"] 29.127 + ["Albert" "Einstein" "Theory of relativity (special)"] 29.128 + ["Isaac" "Newton" "Dark matter"]))) 29.129 + 29.130 +(defn clear-tables [] 29.131 + (with-connection *db2* (dorun (map drop-table databases)))) 29.132 + 29.133 +(defn fill-tables [] 29.134 + (make-ideas) 29.135 + (make-essay-scientists) 29.136 + (make-scientist-whereabouts) 29.137 + (make-scientists)) 29.138 + 29.139 +(defn make-empty-tables [] 29.140 + (with-connection *db2* 29.141 + (create-table 29.142 + :ideas 29.143 + [:name "varchar(255)"] 29.144 + [:surname "varchar(255)"] 29.145 + [:idea "varchar(255)"]) 29.146 + (create-table 29.147 + :scientist_whereabouts 29.148 + [:name "varchar(255)"] 29.149 + [:surname "varchar(255)"] 29.150 + [:country "varchar(255)"] 29.151 + [:immigration_date "date"] 29.152 + [:emigration_date "date"]) 29.153 + (create-table 29.154 + :scientists 29.155 + [:name "varchar(255)"] 29.156 + [:surname "varchar(255)"] 29.157 + [:birth_place "varchar(255)"] 29.158 + [:death_place "varchar(255)"] 29.159 + [:birth_date "date"] 29.160 + [:death_date "date"]) 29.161 + (create-table 29.162 + :essay_scientists 29.163 + [:id "integer"] 29.164 + [:name "varchar(255)"] 29.165 + [:surname "varchar(255)"]))) 29.166 + 29.167 +(def fill-tables-command "mysql -u root -p clojure_rlm < scientists-a.in") 29.168 + 29.169 + 29.170 + 29.171 +(defmethod < [java.sql.Date java.sql.Date] [a b] 29.172 + ({-1 true 0 false 1 false} 29.173 + (.compareTo a b))) 29.174 + 29.175 +(defmethod > [java.sql.Date java.sql.Date] 29.176 + ([a b] 29.177 + ({-1 true 0 false 1 false} 29.178 + (.compareTo a b)))) 29.179 + 29.180 + 29.181 +;; they can have the same date of birth and death!!! 29.182 +(defn contemps [birth death] 29.183 + (with-connection *db* 29.184 + @(select 29.185 + (table :scientists) 29.186 + (where 29.187 + (or 29.188 + (and (<= :birth_date birth) (< birth :death_date)) 29.189 + (and (< :birth_date death) (<= death :death_date)) 29.190 + (and (<= birth :birth_date) (< :birth_date death)) 29.191 + (and (< birth :death_date) (<= :death_date death))))))) 29.192 + 29.193 +(defn essay-scientists [] 29.194 + @(table *db* :essay_scientists)) 29.195 + 29.196 +(defn get-info [table-name s] 29.197 + @(select 29.198 + (select (table *db* table-name) 29.199 + (where (= :name (:name s)))) 29.200 + (where(= :surname (:surname s))))) 29.201 + 29.202 +(defn century* [#^java.sql.Date d] 29.203 + (* 100 29.204 + (.getCenturyOfEra 29.205 + (LocalDate. d)))) 29.206 + 29.207 + 29.208 +(defn century [#^java.sql.Date d] 29.209 + (* 100 (clojure.core/unchecked-divide (+ 1900 (.getYear d)) 100))) 29.210 + 29.211 + 29.212 + 29.213 +(defn get-scientist-data [s] 29.214 + (let [data (first (get-info :scientists s)) 29.215 + locations (get-info :scientist_whereabouts s) 29.216 + ideas (map :idea (get-info :ideas s)) 29.217 + birth (:birth_date data) 29.218 + death (:death_date data) 29.219 + name (:name data) 29.220 + surname (:surname data) 29.221 + contemps 29.222 + (remove #(and (= (:name %) name) (= (:surname %) surname)) 29.223 + (map #(select-keys % [:name :surname]) 29.224 + (contemps birth death))) 29.225 + century (century birth)] 29.226 + {:data data :ideas ideas :locations locations :contemps contemps :century century})) 29.227 + 29.228 +(defn essay-date* [#^java.sql.Date d] 29.229 + (let [t (LocalDate. d)] 29.230 + (format "%02d/%02d/%04d" (.getDayOfMonth t) (.getMonthOfYear t) (.getYear t)))) 29.231 + 29.232 +(defn essay-date [#^java.sql.Date d] 29.233 + (format "%02d/%02d/%04d" (.getDate d) (inc (.getMonth d)) (+ 1900 (.getYear d)))) 29.234 + 29.235 +(defn fucked-sort [m] 29.236 + (let [surname (:surname m) 29.237 + name (:name m) 29.238 + [surname n1] (.split surname " ") 29.239 + [name n2] (.split name " ") 29.240 + n1 (Integer/parseInt n1) 29.241 + n2 (Integer/parseInt n2)] 29.242 + [surname n1 name n2])) 29.243 + 29.244 +(defn lexical-sort [m] 29.245 + (let [surname (:surname m) 29.246 + name (:name m)] 29.247 + [surname name])) 29.248 + 29.249 + 29.250 +(defn-memo essay [s] 29.251 + (let [info (get-scientist-data s) 29.252 + name (:name (:data info)) 29.253 + surname (:surname (:data info))] 29.254 + (str name 29.255 + " " 29.256 + surname 29.257 + " [" 29.258 + (:birth_place (:data info)) 29.259 + " " 29.260 + (essay-date (:birth_date (:data info))) 29.261 + ", " 29.262 + (:death_place (:data info)) 29.263 + " " 29.264 + (essay-date (:death_date (:data info))) 29.265 + "]" 29.266 + " is one of the most famous scientists of " 29.267 + (:century info)". " 29.268 + "Between all " name "'s ideas we mention: " 29.269 + (apply str (interpose ", " (sort (:ideas info)))) 29.270 + ". " 29.271 + name " lived in: " 29.272 + (apply 29.273 + str 29.274 + (interpose ", " 29.275 + (map 29.276 + (fn [entry] 29.277 + (str (:country entry) 29.278 + " from " 29.279 + (essay-date (:immigration_date entry)) " to " 29.280 + (essay-date (:emigration_date entry)))) 29.281 + (sort-by :immigration_date (:locations info))))) 29.282 + ". " 29.283 + name 29.284 + " was a contemporary of " 29.285 + (apply 29.286 + str 29.287 + (interpose 29.288 + ", " 29.289 + (map (fn [entry] 29.290 + (str (:name entry) 29.291 + " " 29.292 + (:surname entry))) 29.293 + 29.294 + (sort-by lexical-sort 29.295 + (:contemps info))))) 29.296 + ".\n\n" ))) 29.297 + 29.298 +(defn essays [] 29.299 + (map essay (essay-scientists))) 29.300 + 29.301 + 29.302 +(defn main [[username password db-name]] 29.303 + (let [db-host *host* 29.304 + db-port *port* 29.305 + db {:classname "com.mysql.jdbc.Driver" 29.306 + :subprotocol "mysql" 29.307 + :subname (str "//" db-host ":" db-port "/" db-name) 29.308 + :user username 29.309 + :password password}] 29.310 + (binding [*db* db] 29.311 + (print (apply str (essays)))))) 29.312 + 29.313 +(def desired-md5 "e436d54f87c86bffb5d1b38d5f3e136b") 29.314 + 29.315 +(if (command-line?) 29.316 + (main *command-line-args*)) 29.317 + 29.318 + 29.319 + 29.320 +
30.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 30.2 +++ b/src/shazoooooooo.clj Tue Oct 18 01:17:49 2011 -0700 30.3 @@ -0,0 +1,20 @@ 30.4 +(ns coderloop.shazoooooooo) 30.5 +(use 'coderloop.utils) 30.6 + 30.7 + 30.8 + 30.9 + 30.10 + 30.11 +(defn shazzooom [n] 30.12 + (dorun 30.13 + (map #(cond (and (= 0 (rem % 3)) (= 0 (rem % 7))) 30.14 + (print"Shazoo!\n") 30.15 + (= 0 (rem % 3)) 30.16 + (print "Moo\n") 30.17 + (= 0 (rem % 7)) 30.18 + (print "Muu\n")) 30.19 + (range 1 (inc n))))) 30.20 + 30.21 + 30.22 +(if *command-line-args* 30.23 + (shazzooom (read-integer (first *command-line-args*))))
31.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 31.2 +++ b/src/spy.clj Tue Oct 18 01:17:49 2011 -0700 31.3 @@ -0,0 +1,95 @@ 31.4 +(ns coderloop.spy 31.5 + (use rlm.ns-rlm) 31.6 + (use [clojure.contrib [string :only [trim]]]) 31.7 + (use clojure.java.io) 31.8 + (use rlm.shell-inspect) 31.9 + (use [clojure.contrib [duck-streams :only [read-lines slurp*]]])) 31.10 + 31.11 +;;(rlm.ns-rlm/ns-clone rlm.light-base) 31.12 + 31.13 + 31.14 + 31.15 +(def data "Yvru hlrikvi, Dljbrjy zj jgvrbzex. Kyv jvtivk ivtzgv wfi kyv wrdflj Tfuvicffg vevixp uizeb zj efn befne.") 31.16 + 31.17 +(def rotate? 69) 31.18 + 31.19 + 31.20 +(def data-2 "71ds7 31.21 +!45DMUXLY?IQI9uYr MI2zQBhOQPzOx Hs2OfOVK8YV9W? gCF!UL9onPax,?AE1!U9wDTaCl6Dc vpM0EAMF?snpWCDpyTG1TIUGcq1x3 31.22 +GnNnon,t!wRERh o6DcD 4ElBLHSd3fOh1w FjnVJasWNYdIwmh2FfCy") 31.23 + 31.24 + 31.25 + 31.26 +(def data-3 31.27 + "-80 31.28 +b1LxafSh9m 31.29 +SRKJy3FB5aHYVEyzYTUmvM!hrU1LS Onve!IW7LTyyVwgu gAhF?v9XzVMooq6SczGm. 1GeNd xMzSP8bFsce 31.30 +.sxcauv2G4e78UDdlit xHqCEIJ?mq7yXv!? lsneLk hq9HMqhZ?OF5 TwY0tyzeOM825myFL4r32! 31.31 +jzIzpRWDlFFl?f0VxNSpK6ASH6YZ 2IXPU.7oHBMOI!Fq1cdExFRsJm! o45gBS,zF.AO!PfYG q0DJF T 31.32 +jeZamr jLz1Y,0.1NoFwrPzXOejWV2x6W V0fsisEau4zhvrK3b3 !ObV!, tmmEb0D6qr 31.33 +xDyrh7cKGUnAgeR8!G.i2yK !JC.1wen12Tz,wl8GO0rwSn czJT!WSshzgzI0BA3WbtI sTTt9 ZoudC4?4RFn,iHNtIRxBrMw?TdI 31.34 +JJvK,ds2yi,rpafNlu7wYt! 8lhSBu7P4sesKJllvyOz1wode OhVDL qwowelZXTiEfvsDfFV40Gjf KvIj2y4 31.35 +IiUoIyBd6H8r2 I0cDYkrfN3O cF?X Y7AqEculz4jFxrS.Fa.t S7YkXc 31.36 +N?aPxe747sfXoDezv.M3LA TUhT67.rLpDdcm VkH27x9bJtOanedL?? Hwt8c5mkXDJ.bv UqvT 31.37 +8ByXq4!3urp4cW1 SffqmrfAzdzEMtPJ.ridlaWT ALlzEgsnZzMq5 EzsMnbe?MJzsx?DF8FWi cBRECtT.no5AWGq. 31.38 +iB17xp.1wv0FRklW3 dlAOMrIRAT 19h4?frfRi BkzWHAuZvM xd3epKg0Ky8vU?OZVjkonHD9TFu 31.39 +170r 3HDC ERCECa5VcO Z6iKYjhFR50N6pr,?.5f3Mm9 Z5pdLlgGGuG 31.40 +OPYKRPG966.t2UoG6mb9jE wt052uClYUnLo3Um,RbjKwMTt1xqz4 KmIEg5 E5gtHIld.a.MY99M qDDzg0 31.41 +tnt,Gp9Okg8trkMfA tAYuSTbzy!5zznCH K,bc 9bHqBWJ y3HyobsD2G7.jVznh21UUUuTw2 31.42 +!PI7p GDvl, d6wOhHqHrjZZ5VxOHrN96a8n DsPrfFGd4z CEIupKxlhi1sMFqJio.EvgWzBfkga9 31.43 +fEtMx4AXdkMI?U2Y4h.0PVXty4T6UT ?aICKEEjlt xhJphCJWEesfMDzhYw2ekRr gwxCXe5IYD,kJY6Sgp, uNTw0KPlf6OYuBF 31.44 +jZRmGFJSIEmrvbbfrxRq8iSdfid okWKno.vw8B5Dqm1FCX,1qg!O,S X,YYX NACZx1Z?.GhyZrfy XDYLW,iFMVIMvkex 31.45 +gjejzPt9suoM H PH?n0WvLs6ccf7OHe,p O8xMqg16sZg8J kIsVyaS!4,,I9R0pV 31.46 +o cToV UWxTjH3v2PG jYKW XWEWZbopjFYMcPk1B3zK,KBCTWnV 31.47 +ZI7XRVav DVDDlkVJaaD1o3 t7yDmwqn7oZMYcY9hV o6qYGGbM0CG .Dd4tNkUJ 31.48 +XbN?bNV,WYuAZ2JeFKkNPyVxTB 4zLI.rbmGs06B0rG jUi gI6, Rk2Um. 31.49 +17jgXKpFJkILPTP1 1gHYe8kwjbFJO3ZNdjorLv2?5EF z92OgAO0Z?vlS2.iv,xOya5mK cE5 WTjOjDT!EVp189vvjgkUw8xrl9e 31.50 +EaoeuKwTSDJzc?Rz!ZJyhvv sh0ShfWp5ZJrO6YiAJ9eAO52PDO!1Z lkJ3GqP.nS8 i5WD8iE mVadphvgywr 31.51 +X23f H LbPjYK5EJcMoBjDiNnTvYDb9cT393 PDfjofvln7A!nGbss TZcTG3TmMKkFO! 31.52 +vz4NJ 2IiuxrIdX!?aRIiKg auth9 xv0rZKH3VumHa,p?odN7px I5k4oE4M5pNkrawk,eL 31.53 +6YlwoD0 v,x4tV?Xcu WX?8 RtoFM!BI3q7iw6Ihw6dePR3vY GzWEfo!GvEY!R3gMaSJm27jUc!mUm 31.54 +!vDL2l2uvp1ve pZXFVjvLhThYJJWx1Li bziUJS9ZCeKwf 41HoBr7nrF?A4rPdnu5LBD CGniTXB5P6B7Yu3naAbe8Gxa. 31.55 +U0nbg mGO5iU?!O5 Prs5Fw mtgzIS9hRl0?Sl0igj5WaISN 86XDtwtcPHmmFI5sIv0vwkRVe 31.56 +wr90G77hBpRwEk4 JZ4EvB2x!pVF01?vi,vsxtM? hkfHf TgJbPOUADYzN0RBx13ijp ,d 31.57 +XCGgky FjSX?pb6r2IqHOcYGZl.RyR6wyDP0 GE cg3E71.1Ye0R6Za mNPAiK?6RG5.O 31.58 +1ujx1fn XpGX7DaWCCnUEIj7E2 ApVDhCoS8J5mimw7i!X67diPl ?h?tm2 774BnMY.JoUxyRTOqNKHxduf 31.59 +OUA3Ma IV5xk7lvk6i0L1N FbB56xzPBuS77wxHEt7spidOfANs3d Z4q61cXGfGcbDLN V9MCnwi4OkG9fmmihRgyw3gSY8w 31.60 +LAykYnorzLjr? rVWT33nySaS!tT!fcCh1okf P4e lUiPr4mi?M9HrS0Xo15E5d9iAd!SW I8ruOUr35gG5p,LrJ1iF9cl7Zpz4 31.61 +.NHx52BPj6JCCFw2UcCLYKJe,Hic bzWrkHke ") 31.62 + 31.63 +(defn decryptor [n] 31.64 + (let [ALAPHABET (reverse (map char (range 65 91))) 31.65 + alaphabet (reverse (map char (range 97 123))) 31.66 + n (if (> 0 n) (+ 26 (rem n 26)) n)] 31.67 + (fn [c] 31.68 + (first 31.69 + (drop n 31.70 + (drop-while 31.71 + (comp not #{c}) 31.72 + 31.73 + 31.74 + (cond (Character/isUpperCase c) (cycle ALAPHABET) 31.75 + (Character/isLowerCase c) (cycle alaphabet) 31.76 + true (repeat c)))))))) 31.77 + 31.78 +(defn decrypt [n s] 31.79 + (apply str (map (decryptor n) (seq s)))) 31.80 + 31.81 +(defn str->int [s] 31.82 + (Integer/parseInt (trim s))) 31.83 + 31.84 +(def a ["/home/r/coderloop-test/spiesstory-a.in"]) 31.85 + 31.86 + 31.87 +(defn doit [a] 31.88 + (let [lines (read-lines (first a)) 31.89 + num (str->int (first lines)) 31.90 + text (rest lines)] 31.91 + (dorun (map 31.92 + (fn [line] 31.93 + (println (decrypt num line))) 31.94 + text)))) 31.95 + 31.96 +(if (command-line?) 31.97 + (doit *command-line-args*)) 31.98 +
32.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 32.2 +++ b/src/stalk.clj Tue Oct 18 01:17:49 2011 -0700 32.3 @@ -0,0 +1,13 @@ 32.4 +(ns coderloop.stalk 32.5 + (:use rlm.shell-inspect) 32.6 + (:require [clojure.contrib [duck-streams :as ds]]) 32.7 + (:use [clojure.contrib [def]]) 32.8 + 32.9 + ) 32.10 + 32.11 + 32.12 + 32.13 +(defn info [username] 32.14 + 32.15 + (ds/slurp* 32.16 + (str "http://api.coderloop.com/1/users/show.json?username=" username)))
33.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 33.2 +++ b/src/test_mega_tree.clj Tue Oct 18 01:17:49 2011 -0700 33.3 @@ -0,0 +1,41 @@ 33.4 +(ns coderloop.test-mega-tree) 33.5 + 33.6 +(rlm.ns-rlm/ns-clone rlm.light-base) 33.7 +(import '[com.coderloop.puzzles Megafasttree Megafasttree$Node]) 33.8 + 33.9 + 33.10 +(defn node [n] 33.11 + (Megafasttree$Node. n)) 33.12 + 33.13 + 33.14 +(defn tree [root] 33.15 + (doto (Megafasttree.) 33.16 + (.addRoot root))) 33.17 + 33.18 + 33.19 +(defn make-counter [] 33.20 + (let [a (atom -1)] 33.21 + (fn count [] (swap! a inc)))) 33.22 + 33.23 + 33.24 +(defn make-big-ass-tree [depth] 33.25 + (let [counter (make-counter) 33.26 + root (node (counter))] 33.27 + (loop [base [root] 33.28 + level 0] 33.29 + (if (= level depth) 33.30 + (tree root) 33.31 + (do 33.32 + (dorun (map 33.33 + #(doto % 33.34 + (.setLeftChild (node (counter))) 33.35 + (.setRightChild (node (counter)))) 33.36 + base)) 33.37 + (recur 33.38 + (apply concat (map #(vector (.toLeft %) (.toRight %)) 33.39 + base)) 33.40 + (inc level))))))) 33.41 + 33.42 + 33.43 +(defn test-shittiness [n] 33.44 + (time (do (.bfs (make-big-ass-tree n)) nil)))
34.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 34.2 +++ b/src/top_five.clj Tue Oct 18 01:17:49 2011 -0700 34.3 @@ -0,0 +1,171 @@ 34.4 +(ns coderloop.top-five) 34.5 + 34.6 + 34.7 +(use 'rlm.shell-inspect) 34.8 +(use 'clojure.contrib.profile) 34.9 +(use '[clojure.contrib [duck-streams :only [file-str read-lines *default-encoding*]]]) 34.10 +(import '(java.nio ByteBuffer CharBuffer) 34.11 + '(java.io PushbackReader InputStream InputStreamReader 34.12 + FileInputStream)) 34.13 + 34.14 + 34.15 + 34.16 +;; ^{:doc "Name of the default encoding to use when reading & writing. 34.17 +;; Default is UTF-8." 34.18 +;; :tag "java.lang.String"} 34.19 +;; *default-encoding* "US-ASCII") 34.20 +;;(set! clojure.contrib.duck-streams/*default-encoding* "US-ASCII") 34.21 + 34.22 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 34.23 + 34.24 + (def a (file-str "/home/r/coderloop-test/topfive-a.in")) 34.25 + (def b (file-str "/home/r/coderloop-test/topfive-b.in")) 34.26 + (def c (file-str "/home/r/coderloop-test/topfive-c.in")) 34.27 + (def d (file-str "/home/r/coderloop-test/topfive-d.in")) 34.28 + (def e (file-str "/home/r/coderloop-test/topfive-e.in")) 34.29 + (def f (file-str "/home/r/coderloop-test/topfive-f.in")) 34.30 + 34.31 +(defn get-query-slow [s] 34.32 + (nth (re-matches #"^.*, query=(.*)]$" s) 1)) 34.33 + 34.34 +(defn #^java.lang.String get-query [#^java.lang.String s] 34.35 + (.substring s (clojure.core/+ (.lastIndexOf s "query=") 6) (clojure.core/- (.length s) 1))) 34.36 + 34.37 + 34.38 +(defn chuncked-pmap 34.39 + "helps with paralleization of functions that don't take 34.40 + very much time to execute" 34.41 + [n f coll] 34.42 + (apply concat 34.43 + (pmap 34.44 + (fn [coll] 34.45 + (doall (map f coll))) 34.46 + (partition-all n coll)))) 34.47 + 34.48 + 34.49 +;; (def *main-map* (atom {})) 34.50 + 34.51 +;; (declare parse-lines tally!) 34.52 + 34.53 +;; (defn parse-lines [lines] 34.54 +;; (map get-query lines)) 34.55 + 34.56 +;; (defn tally! [state parsed-lines] 34.57 +;; (doseq [element parsed-lines] 34.58 +;; (bump! state element 1))) 34.59 + 34.60 + 34.61 +;; (defn analyze-log [file] 34.62 +;; (let [chunk-count (int (/ (.length #^java.io.File file) (* 32 1024 1024))) 34.63 +;; state (atom {})] 34.64 +;; (dorun 34.65 +;; (pmap (fn [[idx [start end]]] 34.66 +;; (println (str "Chunk " idx "/" chunk-count 34.67 +;; " (" start " -> " end ")")) 34.68 +;; (->> (read-lines-range file start end) 34.69 +;; (parse-lines) 34.70 +;; (tally! state))) 34.71 +;; (indexed (chunk-file file chunk-count)))) 34.72 +;; state)) 34.73 + 34.74 + 34.75 + 34.76 + 34.77 +;; (defn preduce-full 34.78 +;; "a parallel reduce. Because it is parallel, the reducing 34.79 +;; function must make sense in a tree-reducing context" 34.80 +;; [num-chunks mapping-function f coll] 34.81 +;; (loop [prev coll] 34.82 +;; (let [new-coll 34.83 +;; (concat 34.84 +;; (mapping-function 34.85 +;; (partial apply f) 34.86 +;; (partition-all num-chunks prev)))] 34.87 +;; (if (not (next new-coll)) 34.88 +;; (first new-coll) 34.89 +;; (recur new-coll))))) 34.90 + 34.91 + 34.92 + 34.93 + 34.94 + 34.95 +;;; correctness before speed: 34.96 +(defn top-five-0 [file] 34.97 + (binding [*default-encoding* "US-ASCII"] 34.98 + 34.99 + (map 34.100 + first 34.101 + (take 34.102 + 5 34.103 + (reverse 34.104 + (sort-by second 34.105 + (reduce 34.106 + (partial merge-with +) 34.107 + (map frequencies 34.108 + (partition-all 2 (map get-query (read-lines file))))))))))) 34.109 +"Elapsed time: 34440.153889 msecs" 34.110 +"Elapsed time: 21072.141885 msecs" 34.111 +"Elapsed time: 21041.711202 msecs" 34.112 +;; now let's do speed: 34.113 +(defn top-five-1 [file] 34.114 + (map 34.115 + first 34.116 + (take 34.117 + 5 34.118 + (sort-by (comp - second) 34.119 + (reduce 34.120 + (partial merge-with +) 34.121 + (map frequencies 34.122 + (partition-all 34.123 + 800 (map get-query (read-lines file))))))))) 34.124 + 34.125 + 34.126 +(defn top-five-3 [file] 34.127 + (map 34.128 + first 34.129 + (take 5 34.130 + (sort-by (comp - second) 34.131 + (frequencies 34.132 + (map get-query (read-lines file))))))) 34.133 + 34.134 + 34.135 + 34.136 +"Elapsed time: 12877.194194 msecs" 34.137 +"Elapsed time: 12282.975191 msecs" 34.138 +;; got a factor of 2x increase in speed from a larger partition and chuncked-pmap. 34.139 +;; still too slow.... 34.140 + 34.141 +;;; let's try the "make it all one big function" approach 34.142 +;;(set! *warn-on-reflection* true) 34.143 + 34.144 +(defn top-five-2 [file] 34.145 + 34.146 + (let [*top-5* (atom (vector :a :b :c :d :e)) 34.147 + *data* (atom {})] 34.148 + 34.149 + (letfn [(top-ten-helper 34.150 + [line] 34.151 + (let [query (get-query line)] 34.152 + (swap! *data* #(assoc % query (inc (get @*data* query 0)))) 34.153 + (if (> (@*data* query 0) (get @*data* (nth @*top-5* 0) 0)) 34.154 + (do 34.155 + 34.156 + (if-not (contains? (set @*top-5*) query) 34.157 + (do 34.158 + (swap! *top-5* #(assoc % 0 query)) 34.159 + (swap! *top-5* 34.160 + (fn [v] (vec (sort-by #(get @*data* % 0) v))))))))))] 34.161 + 34.162 + (dorun (chuncked-pmap 800 top-ten-helper (read-lines file))) 34.163 + (swap! *top-5* 34.164 + (fn [v] (vec (sort-by #(get @*data* % 0) v)))) 34.165 + (reverse @*top-5*)))) 34.166 +"Elapsed time: 10735.897831 msecs" ;; with chuncked-pmap 34.167 + 34.168 + 34.169 + 34.170 + 34.171 +(if (command-line?) 34.172 + (do 34.173 + (dorun (map println (top-five-3 (file-str (first *command-line-args*))))) 34.174 + (System/exit 0)))
35.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 35.2 +++ b/src/top_five2.clj Tue Oct 18 01:17:49 2011 -0700 35.3 @@ -0,0 +1,37 @@ 35.4 +(ns coderloop.top-five2 35.5 + (:refer-clojure :only []) 35.6 + (:require rlm.ns-rlm rlm.light-base)) 35.7 +(rlm.ns-rlm/ns-clone rlm.light-base) 35.8 + 35.9 +(use 'coderloop.parallel-io) 35.10 + 35.11 +(def a (file-str "/home/r/coderloop-test/topfive-a.in")) 35.12 +(def b (file-str "/home/r/coderloop-test/topfive-b.in")) 35.13 +(def c (file-str "/home/r/coderloop-test/topfive-c.in")) 35.14 +(def d (file-str "/home/r/coderloop-test/topfive-d.in")) 35.15 +(def e (file-str "/home/r/coderloop-test/topfive-e.in")) 35.16 +(def f (file-str "/home/r/coderloop-test/topfive-f.in")) 35.17 + 35.18 +(set! *warn-on-reflection* true) 35.19 + 35.20 +(defn #^"[Ljava.lang.String;" dumbest-split* 35.21 + [#^String s c #^"[Ljava.lang.String;" tokens] 35.22 + (let [len (dec (int (alength tokens)))] 35.23 + (loop [start (int 0) 35.24 + i (int 0)] 35.25 + (let [idx (int (.indexOf s (int c) (int start)))] 35.26 + (if (or (neg? idx) (>= i len)) 35.27 + (do (aset tokens i (.substring s start)) 35.28 + tokens) 35.29 + (do (aset tokens i (.substring s start idx)) 35.30 + (recur (inc idx) (inc i)))))))) 35.31 + 35.32 +(defn get-query-1 [s] 35.33 + (nth (re-matches #"^.*, query=(.*)]$" s) 1)) 35.34 + 35.35 + 35.36 +(defn #^java.lang.String get-query-2 [#^java.lang.String s] 35.37 + (.substring s (clojure.core/+ (.lastIndexOf s "query=") 6) (clojure.core/- (.length s) 1))) 35.38 + 35.39 +(defn #^java.lang.String get-query-3 [#^java.lang.String s] 35.40 + )
36.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 36.2 +++ b/src/university-output/delstudent.sql Tue Oct 18 01:17:49 2011 -0700 36.3 @@ -0,0 +1,1 @@ 36.4 +call remove_student (?, ?);
37.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 37.2 +++ b/src/university-output/getcourse.sql Tue Oct 18 01:17:49 2011 -0700 37.3 @@ -0,0 +1,1 @@ 37.4 +call course_details (?);
38.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 38.2 +++ b/src/university-output/getprofessor.sql Tue Oct 18 01:17:49 2011 -0700 38.3 @@ -0,0 +1,1 @@ 38.4 +call professor_details (?);
39.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 39.2 +++ b/src/university-output/getprofessorincourse.sql Tue Oct 18 01:17:49 2011 -0700 39.3 @@ -0,0 +1,1 @@ 39.4 +call get_professor (?);
40.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 40.2 +++ b/src/university-output/getstudent.sql Tue Oct 18 01:17:49 2011 -0700 40.3 @@ -0,0 +1,1 @@ 40.4 +call student_details (?);
41.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 41.2 +++ b/src/university-output/getstudentsincourse.sql Tue Oct 18 01:17:49 2011 -0700 41.3 @@ -0,0 +1,1 @@ 41.4 +call get_students (?);
42.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 42.2 +++ b/src/university-output/newcourse.sql Tue Oct 18 01:17:49 2011 -0700 42.3 @@ -0,0 +1,1 @@ 42.4 +call add_course(?, ?, ?);
43.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 43.2 +++ b/src/university-output/newprofessor.sql Tue Oct 18 01:17:49 2011 -0700 43.3 @@ -0,0 +1,1 @@ 43.4 +call add_professor (?, ?, ?, ?, ?, ?);
44.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 44.2 +++ b/src/university-output/newstudent.sql Tue Oct 18 01:17:49 2011 -0700 44.3 @@ -0,0 +1,1 @@ 44.4 +call add_student (?, ?, ?, ?, ?, ?);
45.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 45.2 +++ b/src/university-output/professorincourse.sql Tue Oct 18 01:17:49 2011 -0700 45.3 @@ -0,0 +1,1 @@ 45.4 +call assign_professor (?, ?);
46.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 46.2 +++ b/src/university-output/replaceprofessor.sql Tue Oct 18 01:17:49 2011 -0700 46.3 @@ -0,0 +1,1 @@ 46.4 +call replace_professor (?, ?);
47.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 47.2 +++ b/src/university-output/setup.sql Tue Oct 18 01:17:49 2011 -0700 47.3 @@ -0,0 +1,155 @@ 47.4 +create table professors ( 47.5 +-- All the professors in the school. The school uses the SSN of 47.6 +-- each professor as an identifying unique number. Each row of the 47.7 +-- table represents a unique professor. 47.8 + name varchar(255) not null, 47.9 + surname varchar(255) not null, 47.10 + email varchar(255), 47.11 + faculty varchar(255), 47.12 + telephone varchar(255), 47.13 + ssn decimal(9, 0) zerofill primary key) 47.14 + ENGINE INNODB; 47.15 + 47.16 +create table students ( 47.17 +-- All the students in the school. Each student has a unique ID 47.18 +-- number assigned by the school. Each row of the table 47.19 +-- represents a unique student. 47.20 + name varchar(255) not null, 47.21 + surname varchar(255), 47.22 + email varchar(255), 47.23 + faculty varchar(255), 47.24 + year int, 47.25 + id decimal(9, 0) zerofill primary key) 47.26 + ENGINE INNODB; 47.27 + 47.28 +create table courses ( 47.29 +-- Set of all classes offered by the school. each row represents 47.30 +-- a different unique class. 47.31 + name varchar(255) primary key, 47.32 + professor_ssn decimal(9,0), 47.33 + -- might be null, in which case a professor 47.34 + -- will need to be assigned at some later time. 47.35 + subject varchar(255), 47.36 + credits int not null, 47.37 + foreign key (professor_ssn) references professors (ssn) 47.38 + on update cascade 47.39 + on delete set null) 47.40 + ENGINE INNODB; 47.41 + 47.42 +create table enrollments ( 47.43 +-- Record of all student-class pairs which represent the current 47.44 +-- enrollment of the school. 47.45 + coursename varchar(255) not null, 47.46 + student_id decimal(9,0) not null references students (id), 47.47 + foreign key (coursename) references courses (name) 47.48 + on update cascade 47.49 + on delete cascade, 47.50 + foreign key (student_id) references students (id) 47.51 + on update cascade 47.52 + on delete cascade, 47.53 + unique (coursename, student_id)) 47.54 + ENGINE INNODB; 47.55 + 47.56 + 47.57 +-- ----------------------------------------------------------------------------- 47.58 +-- Stored Procedure Section. 47.59 +-- ----------------------------------------------------------------------------- 47.60 + 47.61 +-- what do we need to be able to do? 47.62 + 47.63 +-- 1 - new professors, students, and courses 47.64 +-- 2 - assign a professor to a course. 47.65 +-- 3 - get a professor owning a course. 47.66 +-- 4 - get the details of prof, student, course 47.67 +-- 5 - remove a student from a course. 47.68 + 47.69 +create procedure add_professor (in name varchar(255), 47.70 + in surname varchar(255), 47.71 + in email varchar(255), 47.72 + in faculty varchar(255), 47.73 + in telephone varchar(255), 47.74 + in ssn decimal(9, 0)) 47.75 + insert into professors values(name, surname, email, faculty, telephone, ssn); 47.76 + 47.77 +create procedure add_student (in name varchar(255), 47.78 + in surname varchar(255), 47.79 + in email varchar(255), 47.80 + in faculty varchar(255), 47.81 + in year int, 47.82 + in id decimal(9,0)) 47.83 + insert into students values(name, surname, email, faculty, year, id); 47.84 + 47.85 +create procedure add_course 47.86 + -- courses are initially created without any professor attached. 47.87 + (in name varchar(255), 47.88 + in subject varchar(255), 47.89 + in credits int) 47.90 + insert into courses values(name, null, subject, credits); 47.91 + 47.92 +create procedure assign_professor 47.93 + (in course varchar(255), 47.94 + in professor_ssn decimal(9,0)) 47.95 + update courses 47.96 + set professor_ssn = professor_ssn 47.97 + where name = course; 47.98 + 47.99 +create procedure enroll_student (in course varchar(255), 47.100 + in student_id decimal(9,0)) 47.101 + insert into enrollments values (course, student_id); 47.102 + 47.103 +create procedure get_students (in course varchar(255)) 47.104 + select name, surname from students where students.id in 47.105 + (select student_id from enrollments 47.106 + where enrollments.coursename = course) 47.107 + order by surname asc, name asc; 47.108 + 47.109 +create procedure get_professor (in course varchar(255)) 47.110 + select name, surname, ssn from professors 47.111 + where professors.ssn = 47.112 + (select professor_ssn from courses where courses.name = course); 47.113 + 47.114 +create procedure course_details (in course varchar(255)) 47.115 + select name, credits from courses where name = course; 47.116 + 47.117 +create procedure professor_details (in ssn decimal(9,0)) 47.118 + select name, surname, email, faculty from professors 47.119 + where professors.ssn = ssn; 47.120 + 47.121 +create procedure student_details (in id decimal(9,0)) 47.122 + select name, surname, email, faculty, year from students 47.123 + where students.id = id; 47.124 + 47.125 +create procedure remove_student (in id decimal(9,0), course varchar(255)) 47.126 + delete from enrollments where 47.127 + enrollments.coursename = course AND 47.128 + enrollemnts.student_id = id; 47.129 + 47.130 +create procedure replace_professor (in ssn decimal(9,0), course varchar(255)) 47.131 + update courses 47.132 + set professor_ssn = ssn 47.133 + where name = course; 47.134 + 47.135 +-- -------------------------------------------------------------- 47.136 +-- initialiaze database to test 47.137 +-- -------------------------------------------------------------- 47.138 +call add_professor ('A', '1', 'email', 'CS', '012-345-6789', 0); 47.139 +call add_professor ('B', '2', 'email', 'CS', '012-345-6789', 1); 47.140 +call add_professor ('C', '3', 'email', 'CS', '012-345-6789', 2); 47.141 +call add_professor ('D', '4', 'email', 'CS', '012-345-6789', 3); 47.142 + 47.143 +call add_student ('a', '1', 'email', 'CS', '1964', 0); 47.144 +call add_student ('a', '2', 'email', 'CS', '1964', 5); 47.145 +call add_student ('b', '2', 'email', 'CS', '1964', 1); 47.146 +call add_student ('c', '3', 'email', 'CS', '1964', 2); 47.147 +call add_student ('d', '4', 'email', 'CS', '1964', 3); 47.148 + 47.149 +call add_course ('course A', 'science', 5); 47.150 +call add_course ('course B', 'science', 5); 47.151 +call add_course ('course C', 'science', 5); 47.152 + 47.153 +call enroll_student ('course A', 0); 47.154 +call enroll_student ('course A', 5); 47.155 +call enroll_student ('course A', 1); 47.156 +call enroll_student ('course A', 2); 47.157 + 47.158 +call assign_professor ('course A', 0);
48.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 48.2 +++ b/src/university-output/studentincourse.sql Tue Oct 18 01:17:49 2011 -0700 48.3 @@ -0,0 +1,1 @@ 48.4 +call enroll_student (?, ?);
49.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 49.2 +++ b/src/university/README.TXT Tue Oct 18 01:17:49 2011 -0700 49.3 @@ -0,0 +1,9 @@ 49.4 +################################################################################ 49.5 +# Coderloop Coding Challenge 49.6 +# Puzzle: university 49.7 +# Author: Robert McIntyre 49.8 +################################################################################ 49.9 + 49.10 +The main file is setup.sql, which defines the database schema and all 49.11 +the stored procedures to manupulate the tables. All other *.sql files 49.12 +are just thin wrappers around calls to these stored procedures.
50.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 50.2 +++ b/src/university/create-database.sql Tue Oct 18 01:17:49 2011 -0700 50.3 @@ -0,0 +1,1 @@ 50.4 +create database university;
51.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 51.2 +++ b/src/university/delete-database.sql Tue Oct 18 01:17:49 2011 -0700 51.3 @@ -0,0 +1,1 @@ 51.4 +drop database if exists university;
52.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 52.2 +++ b/src/university/delstudent.sql Tue Oct 18 01:17:49 2011 -0700 52.3 @@ -0,0 +1,1 @@ 52.4 +call remove_student (?, ?);
53.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 53.2 +++ b/src/university/dist.sh Tue Oct 18 01:17:49 2011 -0700 53.3 @@ -0,0 +1,9 @@ 53.4 +rm /home/r/roBin/src/coderloop/university-output/* 53.5 + 53.6 +cp delstudent.sql getcourse.sql getprofessor.sql getprofessorincourse.sql getstudent.sql getstudentsincourse.sql newcourse.sql newprofessor.sql newstudent.sql professorincourse.sql replaceprofessor.sql studentincourse.sql setup.sql ../university-output 53.7 + 53.8 +cd /home/r/roBin/src/coderloop/university-output 53.9 +tar -cvjf university-SQL.tar.bz2 * 53.10 +mv university-SQL.tar.bz2 /home/r/coderloop 53.11 +rm /home/r/coderloop-test/* 53.12 +cp /home/r/coderloop/university-SQL.tar.bz2 /home/r/coderloop-test
54.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 54.2 +++ b/src/university/getcourse.sql Tue Oct 18 01:17:49 2011 -0700 54.3 @@ -0,0 +1,1 @@ 54.4 +call course_details (?);
55.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 55.2 +++ b/src/university/getprofessor.sql Tue Oct 18 01:17:49 2011 -0700 55.3 @@ -0,0 +1,1 @@ 55.4 +call professor_details (?);
56.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 56.2 +++ b/src/university/getprofessorincourse.sql Tue Oct 18 01:17:49 2011 -0700 56.3 @@ -0,0 +1,1 @@ 56.4 +call get_professor (?);
57.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 57.2 +++ b/src/university/getstudent.sql Tue Oct 18 01:17:49 2011 -0700 57.3 @@ -0,0 +1,1 @@ 57.4 +call student_details (?);
58.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 58.2 +++ b/src/university/getstudentsincourse.sql Tue Oct 18 01:17:49 2011 -0700 58.3 @@ -0,0 +1,1 @@ 58.4 +call get_students (?);
59.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 59.2 +++ b/src/university/newcourse.sql Tue Oct 18 01:17:49 2011 -0700 59.3 @@ -0,0 +1,1 @@ 59.4 +call add_course(?, ?, ?);
60.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 60.2 +++ b/src/university/newprofessor.sql Tue Oct 18 01:17:49 2011 -0700 60.3 @@ -0,0 +1,1 @@ 60.4 +call add_professor (?, ?, ?, ?, ?, ?);
61.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 61.2 +++ b/src/university/newstudent.sql Tue Oct 18 01:17:49 2011 -0700 61.3 @@ -0,0 +1,1 @@ 61.4 +call add_student (?, ?, ?, ?, ?, ?);
62.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 62.2 +++ b/src/university/professorincourse.sql Tue Oct 18 01:17:49 2011 -0700 62.3 @@ -0,0 +1,1 @@ 62.4 +call assign_professor (?, ?);
63.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 63.2 +++ b/src/university/replaceprofessor.sql Tue Oct 18 01:17:49 2011 -0700 63.3 @@ -0,0 +1,1 @@ 63.4 +call replace_professor (?, ?);
64.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 64.2 +++ b/src/university/reset-sql.sh Tue Oct 18 01:17:49 2011 -0700 64.3 @@ -0,0 +1,6 @@ 64.4 + 64.5 + 64.6 +mysql -u root -psql1005025 < delete-database.sql 64.7 +mysql -u root -psql1005025 < create-database.sql 64.8 + 64.9 +mysql -u root -psql1005025 university < setup.sql
65.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 65.2 +++ b/src/university/setup.sql Tue Oct 18 01:17:49 2011 -0700 65.3 @@ -0,0 +1,155 @@ 65.4 +create table professors ( 65.5 +-- All the professors in the school. The school uses the SSN of 65.6 +-- each professor as an identifying unique number. Each row of the 65.7 +-- table represents a unique professor. 65.8 + name varchar(255) not null, 65.9 + surname varchar(255) not null, 65.10 + email varchar(255), 65.11 + faculty varchar(255), 65.12 + telephone varchar(255), 65.13 + ssn decimal(9, 0) zerofill primary key) 65.14 + ENGINE INNODB; 65.15 + 65.16 +create table students ( 65.17 +-- All the students in the school. Each student has a unique ID 65.18 +-- number assigned by the school. Each row of the table 65.19 +-- represents a unique student. 65.20 + name varchar(255) not null, 65.21 + surname varchar(255), 65.22 + email varchar(255), 65.23 + faculty varchar(255), 65.24 + year int, 65.25 + id decimal(9, 0) zerofill primary key) 65.26 + ENGINE INNODB; 65.27 + 65.28 +create table courses ( 65.29 +-- Set of all classes offered by the school. each row represents 65.30 +-- a different unique class. 65.31 + name varchar(255) primary key, 65.32 + professor_ssn decimal(9,0), 65.33 + -- might be null, in which case a professor 65.34 + -- will need to be assigned at some later time. 65.35 + subject varchar(255), 65.36 + credits int not null, 65.37 + foreign key (professor_ssn) references professors (ssn) 65.38 + on update cascade 65.39 + on delete set null) 65.40 + ENGINE INNODB; 65.41 + 65.42 +create table enrollments ( 65.43 +-- Record of all student-class pairs which represent the current 65.44 +-- enrollment of the school. 65.45 + coursename varchar(255) not null, 65.46 + student_id decimal(9,0) not null references students (id), 65.47 + foreign key (coursename) references courses (name) 65.48 + on update cascade 65.49 + on delete cascade, 65.50 + foreign key (student_id) references students (id) 65.51 + on update cascade 65.52 + on delete cascade, 65.53 + unique (coursename, student_id)) 65.54 + ENGINE INNODB; 65.55 + 65.56 + 65.57 +-- ----------------------------------------------------------------------------- 65.58 +-- Stored Procedure Section. 65.59 +-- ----------------------------------------------------------------------------- 65.60 + 65.61 +-- what do we need to be able to do? 65.62 + 65.63 +-- 1 - new professors, students, and courses 65.64 +-- 2 - assign a professor to a course. 65.65 +-- 3 - get a professor owning a course. 65.66 +-- 4 - get the details of prof, student, course 65.67 +-- 5 - remove a student from a course. 65.68 + 65.69 +create procedure add_professor (in name varchar(255), 65.70 + in surname varchar(255), 65.71 + in email varchar(255), 65.72 + in faculty varchar(255), 65.73 + in telephone varchar(255), 65.74 + in ssn decimal(9, 0)) 65.75 + insert into professors values(name, surname, email, faculty, telephone, ssn); 65.76 + 65.77 +create procedure add_student (in name varchar(255), 65.78 + in surname varchar(255), 65.79 + in email varchar(255), 65.80 + in faculty varchar(255), 65.81 + in year int, 65.82 + in id decimal(9,0)) 65.83 + insert into students values(name, surname, email, faculty, year, id); 65.84 + 65.85 +create procedure add_course 65.86 + -- courses are initially created without any professor attached. 65.87 + (in name varchar(255), 65.88 + in subject varchar(255), 65.89 + in credits int) 65.90 + insert into courses values(name, null, subject, credits); 65.91 + 65.92 +create procedure assign_professor 65.93 + (in course varchar(255), 65.94 + in professor_ssn decimal(9,0)) 65.95 + update courses 65.96 + set professor_ssn = professor_ssn 65.97 + where name = course; 65.98 + 65.99 +create procedure enroll_student (in course varchar(255), 65.100 + in student_id decimal(9,0)) 65.101 + insert into enrollments values (course, student_id); 65.102 + 65.103 +create procedure get_students (in course varchar(255)) 65.104 + select name, surname from students where students.id in 65.105 + (select student_id from enrollments 65.106 + where enrollments.coursename = course) 65.107 + order by surname asc, name asc; 65.108 + 65.109 +create procedure get_professor (in course varchar(255)) 65.110 + select name, surname, ssn from professors 65.111 + where professors.ssn = 65.112 + (select professor_ssn from courses where courses.name = course); 65.113 + 65.114 +create procedure course_details (in course varchar(255)) 65.115 + select name, credits from courses where name = course; 65.116 + 65.117 +create procedure professor_details (in ssn decimal(9,0)) 65.118 + select name, surname, email, faculty from professors 65.119 + where professors.ssn = ssn; 65.120 + 65.121 +create procedure student_details (in id decimal(9,0)) 65.122 + select name, surname, email, faculty, year from students 65.123 + where students.id = id; 65.124 + 65.125 +create procedure remove_student (in id decimal(9,0), course varchar(255)) 65.126 + delete from enrollments where 65.127 + enrollments.coursename = course AND 65.128 + enrollemnts.student_id = id; 65.129 + 65.130 +create procedure replace_professor (in ssn decimal(9,0), course varchar(255)) 65.131 + update courses 65.132 + set professor_ssn = ssn 65.133 + where name = course; 65.134 + 65.135 +-- -------------------------------------------------------------- 65.136 +-- initialiaze database to test 65.137 +-- -------------------------------------------------------------- 65.138 +call add_professor ('A', '1', 'email', 'CS', '012-345-6789', 0); 65.139 +call add_professor ('B', '2', 'email', 'CS', '012-345-6789', 1); 65.140 +call add_professor ('C', '3', 'email', 'CS', '012-345-6789', 2); 65.141 +call add_professor ('D', '4', 'email', 'CS', '012-345-6789', 3); 65.142 + 65.143 +call add_student ('a', '1', 'email', 'CS', '1964', 0); 65.144 +call add_student ('a', '2', 'email', 'CS', '1964', 5); 65.145 +call add_student ('b', '2', 'email', 'CS', '1964', 1); 65.146 +call add_student ('c', '3', 'email', 'CS', '1964', 2); 65.147 +call add_student ('d', '4', 'email', 'CS', '1964', 3); 65.148 + 65.149 +call add_course ('course A', 'science', 5); 65.150 +call add_course ('course B', 'science', 5); 65.151 +call add_course ('course C', 'science', 5); 65.152 + 65.153 +call enroll_student ('course A', 0); 65.154 +call enroll_student ('course A', 5); 65.155 +call enroll_student ('course A', 1); 65.156 +call enroll_student ('course A', 2); 65.157 + 65.158 +call assign_professor ('course A', 0);
66.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 66.2 +++ b/src/university/studentincourse.sql Tue Oct 18 01:17:49 2011 -0700 66.3 @@ -0,0 +1,1 @@ 66.4 +call enroll_student (?, ?);
67.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 67.2 +++ b/src/utils.clj Tue Oct 18 01:17:49 2011 -0700 67.3 @@ -0,0 +1,52 @@ 67.4 +(ns coderloop.utils 67.5 + (:use [clojure.contrib 67.6 + [shell-out :only [sh]] 67.7 + [duck-streams :only [file-str read-lines copy]]]) 67.8 + (:use [clojure [string :only [trim blank? split]]])) 67.9 + 67.10 + 67.11 + 67.12 + 67.13 + 67.14 + 67.15 +(defn read-integer [file] 67.16 + (Integer/parseInt 67.17 + (trim (slurp (file-str file))))) 67.18 + 67.19 +(defn read-big-integer [file] 67.20 + (java.math.BigInteger. 67.21 + (trim (slurp (file-str file))))) 67.22 + 67.23 + 67.24 +(defn read-integer-line [#^java.lang.string s] 67.25 + (map #(Integer/parseInt %) 67.26 + (filter (comp not blank?) 67.27 + (split s #"\W")))) 67.28 + 67.29 + 67.30 + 67.31 +(defn read-integers 67.32 + "returns a lazy sequence of integers read from the file. 67.33 + Integers are delimited by any sort of whitespace" 67.34 + [#^java.io.File file] 67.35 + (mapcat read-integer-line 67.36 + (filter (comp not blank?) 67.37 + (read-lines (file-str file))))) 67.38 + 67.39 + 67.40 +(defn trans-print [x] 67.41 + (println x ) 67.42 + x) 67.43 + 67.44 + 67.45 +(defn md5 67.46 + "given a string, compute the md5 checksum" 67.47 + [s] 67.48 + (let [t (java.io.File/createTempFile "zzz" "zzz")] 67.49 + (copy s t) 67.50 + (.deleteOnExit t) 67.51 + (sh "md5sum" (.getCanonicalPath t)))) 67.52 + 67.53 + 67.54 +(defn digits [n] 67.55 + (map #(Integer/parseInt (str %)) (seq (str n))))
68.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 68.2 +++ b/src/web_ship.clj Tue Oct 18 01:17:49 2011 -0700 68.3 @@ -0,0 +1,50 @@ 68.4 +(ns coderloop.web-ship 68.5 + (:use rlm.shell-inspect) 68.6 + (:use [clojure.contrib.duck-streams :as ds :only []])) 68.7 + 68.8 + 68.9 +;;screw this ---- 68.10 + 68.11 +(defn sail 68.12 + ([] 68.13 + (ds/slurp* 68.14 + (str "http://localhost:3000/ship/sail"))) 68.15 + ([host] 68.16 + (ds/slurp* 68.17 + (str "http://localhost:3000/ship/sail?host=" host)))) 68.18 + 68.19 +(comment -- this is the solution 68.20 + 68.21 + WBL*| start 68.22 + WB|L* move L 68.23 + *WB|L sail back 68.24 + B|*WL move W 68.25 + LB*|W move L 68.26 + L|*WB move B 68.27 + *L|WB sail back 68.28 + |*WBL move L 68.29 + 68.30 + ) 68.31 + 68.32 +;; translate into code 68.33 + 68.34 + 68.35 +(def B "bsd") 68.36 +(def L "linux") 68.37 +(def W "windows") 68.38 + 68.39 + 68.40 +(if (command-line?) 68.41 + (do 68.42 + (sail L) 68.43 + (sail) 68.44 + (sail W) 68.45 + (sail L) 68.46 + (sail B) 68.47 + (sail) 68.48 + (sail L) 68.49 + (System/exit 0))) 68.50 + 68.51 + 68.52 + 68.53 +