Commits

tednaleid committed 5211fa0

more problems solved

Comments (0)

Files changed (2)

src/four_clojure/answered.clj

 (ns four-clojure.answered)
 
+; from: http://stackoverflow.com/questions/2352020/debugging-in-clojure
+(defmacro dbg[x] `(let [x# ~x] (println "dbg:" '~x "=" x#) x#))
+(+ 4 (dbg (+ 3 6))) ; prints: dbg: (+ 3 6) = 9
+
+
 ; http://www.4clojure.com/problem/5
 ; Lists: conj
 ; When operating on a list, the conj function will return a new list with one or more items "added" to the front.
 (= false (half-truth true true true))
 (= true (half-truth true true true false))
 
+; http://www.4clojure.com/problem/61
+; Map Construction
+; Write a function which takes a vector of keys and a vector of values and constructs a map from them.
+; restrictions: zipmap
+
+(defn my-zipmap [k v] (apply merge (map hash-map k v)))
+
+(= (my-zipmap [:a :b :c] [1 2 3]) {:a 1, :b 2, :c 3})
+(= (my-zipmap [1 2 3 4] ["one" "two" "three"]) {1 "one", 2 "two", 3 "three"})
+(= (my-zipmap [:foo :bar] ["foo" "bar" "baz"]) {:foo "foo", :bar "bar"})
+
+; http://www.4clojure.com/problem/81
+; Set Intersection
+; Write a function which returns the intersection of two sets. The intersection is the sub-set of items that each set has in common.
+; restriction: intersection
+
+(defn my-intersection [s1 s2] (reduce (fn [s x] (if (contains? s2 x) (conj s x) s)) #{} s1))
+; or (comp set filter)
+; or #(set (filter % %2))
+
+(= (my-intersection #{0 1 2 3} #{2 3 4 5}) #{2 3})
+(= (my-intersection #{0 1 2} #{3 4 5}) #{})
+(= (my-intersection #{:a :b :c :d} #{:c :e :a :f :d}) #{:a :c :d})
+
+; http://www.4clojure.com/problem/66
+; Greatest Common Divisor
+; Given two integers, write a function which returns the greatest common divisor
+
+(defn gcd [x y] 
+  (if (> y x)
+    (gcd y x)
+    (if (zero? y) 
+      x
+      (gcd y (rem x y)))))
+
+(= (gcd 2 4) 2)
+(= (gcd 10 5) 5)
+(= (gcd 5 7) 1)
+(= (gcd 1023 858) 33)
+
+
 ; http://www.4clojure.com/problem/
 ;
 ;
 ;
 ;
 
-; http://www.4clojure.com/problem/
-;
-;
-
-; http://www.4clojure.com/problem/
-;
-;
-
-; http://www.4clojure.com/problem/
-;
-;
-
 
 
 

src/four_clojure/scratch.clj

-(ns four-clojure.core)
+(ns four-clojure.scratch)
+;(ns four-clojure.scratch (:require clojure.tools.trace))
 
-(#(reduce + (range 1 (inc %))) 403000000)
+(defmacro dbg[x] `(let [x# ~x] (println "dbg:" '~x "=" x#) x#))
+
+(#(reduce + (range 1 (dbg (inc %)))) 4)
+(#(reduce + (range 1 (inc %))) 4)
+;(#(reduce + (range 1 (-> (inc %) (range) (println)))) 4030)
 (println "done")