├── README └── answers ├── 1.Nothing but the Truth.clj.clj ├── 10.Intro to Maps.clj ├── 100. Least Common Multiple.clj ├── 102. 102intoCamelCase.clj ├── 107.Simple closures ├── 11.Maps conj.clj ├── 118.Re-implement Map ├── 12.Intro to Sequences.clj ├── 120.Sum of square of digits ├── 122.Read a binary number ├── 126.Through the Looking Class ├── 128. Recognize Playing Cards ├── 13.Sequences rest.clj ├── 134.A nil key ├── 135.Infix Calculator ├── 14.Intro to Functions.clj ├── 143.dot product ├── 145.For the win ├── 146. Trees into tables ├── 147. Pascal's Trapezoid ├── 15.Double Down.clj ├── 153.Pairwise Disjoint Sets.clj ├── 156.Map Defaults ├── 157. Indexing Sequences ├── 16.Hello World.clj ├── 161. Subset and Superset ├── 162.Logical falsity and truth ├── 166. Comparisons ├── 17.Sequences map.clj ├── 173. Intro to Destructuring 2 ├── 18.Sequences filter.clj ├── 19.Last Element.clj ├── 2.Simple Math.clj ├── 20.Penultimate Element.clj ├── 21.Nth Element ├── 22.Count a Sequence ├── 23.Reverse a Sequence ├── 24.Sum It All Up ├── 25.Find the odd numbers ├── 26.Fibonacci Sequence ├── 27.Palindrome Detector ├── 28.Flatten a Sequence ├── 29.Get the Caps ├── 3.Intro to Strings.clj ├── 30.Compress a Sequence ├── 32.Duplicate a Sequence ├── 32.Pack a Sequence ├── 33.Replicate a Sequence ├── 34. Implement range ├── 35.Local bindings.clj ├── 36.Let it Be.clj ├── 37.Regular Expressions.clj ├── 38.Maximum value ├── 39.Interleave Two Seqs ├── 4.Intro to Lists.clj ├── 40.Interpose a Seq ├── 41Drop Every Nth Item.txt ├── 42.Factorial Fun ├── 43. Reverse Interleave ├── 44. Rotate Sequence ├── 45.Intro to Iterate ├── 46. Flipping out ├── 47.Contain Yourself ├── 48.Intro to some ├── 49.Split a sequence ├── 5.Lists conj.clj ├── 50. Split by Type ├── 51.Advanced Destructuring ├── 54. Partition a Sequence ├── 55. Count Occurrences ├── 56. Find Distinct Items ├── 57.Simple Recursion.clj ├── 58. Function Composition.txt ├── 59. Juxtaposition ├── 6.Intro to Vectors.clj ├── 60. Sequence Reductions ├── 61.Map Construction ├── 62.Re-implement Iterate ├── 63.Group a Sequence ├── 64.Intro to Reduce.clj ├── 65. Black Box Testing.clj ├── 66.Greatest Common Divisor ├── 67. Prime Numbers ├── 68.Recurring Theme.clj ├── 69. Merge with a Function.clj ├── 7.Vectors conj.clj ├── 70. Word Sorting ├── 71.Rearranging Code.clj ├── 72.Rearranging Code.clj ├── 74. Filter Perfect Squares ├── 76. Intro to Trampoline.clj ├── 77.Anagram Finder ├── 78. Reimplement Trampoline.clj ├── 8.Intro to Sets.clj ├── 80. Perfect Numbers ├── 81.Set Intersection ├── 83.A Half-Truth ├── 86. Happy Numbers.clj ├── 88.Symmetric Difference ├── 9.Sets conj.clj ├── 90.Cartesian Product ├── 95.To Tree, or not to Tree ├── 96. Beauty is Symmetry ├── 97.Pascal's Triangle └── 99.Product Digits /README: -------------------------------------------------------------------------------- 1 | This is my answers on 4clojure.com. 2 | -------------------------------------------------------------------------------- /answers/1.Nothing but the Truth.clj.clj: -------------------------------------------------------------------------------- 1 | (= true true) 2 | -------------------------------------------------------------------------------- /answers/10.Intro to Maps.clj: -------------------------------------------------------------------------------- 1 | (= 20 ((hash-map :a 10, :b 20, :c 30) :b)) 2 | 3 | (= 20 (:b {:a 10, :b 20, :c 30})) -------------------------------------------------------------------------------- /answers/100. Least Common Multiple.clj: -------------------------------------------------------------------------------- 1 | (fn [& args] 2 | (letfn [(gcd [x y] 3 | (let [a (max x y) 4 | b (min x y) 5 | m (mod a b)] 6 | (if (zero? m) 7 | b 8 | (recur b m)))) 9 | (lcm [a b] 10 | (/ (* a b) (gcd a b)))] 11 | (reduce lcm args) 12 | )) 13 | -------------------------------------------------------------------------------- /answers/102. 102intoCamelCase.clj: -------------------------------------------------------------------------------- 1 | (fn name [s] 2 | (let [words (re-seq #"[a-zA-Z]+" s) 3 | words (cons (first words) 4 | (map clojure.string/capitalize (rest words)))] 5 | (apply str words))) 6 | ;; best answer 7 | #(clojure.string/replace % #"-(\w)" (fn [[a b]] (clojure.string/capitalize b))) 8 | -------------------------------------------------------------------------------- /answers/107.Simple closures: -------------------------------------------------------------------------------- 1 | (fn [y] 2 | (fn [x] 3 | (apply * (repeat y x))) 4 | ) -------------------------------------------------------------------------------- /answers/11.Maps conj.clj: -------------------------------------------------------------------------------- 1 | (= {:a 1, :b 2, :c 3} (conj {:a 1} {:b 2} [:c 3])) 2 | -------------------------------------------------------------------------------- /answers/118.Re-implement Map: -------------------------------------------------------------------------------- 1 | (fn mymap [f coll] 2 | (if (false? (empty? coll)) 3 | (lazy-seq 4 | (cons (f (first coll)) (mymap f (rest coll)))))) -------------------------------------------------------------------------------- /answers/12.Intro to Sequences.clj: -------------------------------------------------------------------------------- 1 | (= 3 (first '(3 2 1))) 2 | 3 | (= 3 (second [2 3 4])) 4 | 5 | (= 3 (last (list 1 2 3))) -------------------------------------------------------------------------------- /answers/120.Sum of square of digits: -------------------------------------------------------------------------------- 1 | (fn cnt-sqrt [arg] 2 | (let [get-digits (fn [n] 3 | (map #(Integer/valueOf (str %)) (String/valueOf n))) 4 | digits-sqr (fn [n] 5 | (apply + (map #(* % %) (get-digits n)))) 6 | res-seq (filter #(< % (digits-sqr %)) arg)] 7 | (count res-seq))) -------------------------------------------------------------------------------- /answers/122.Read a binary number: -------------------------------------------------------------------------------- 1 | #(Integer/valueOf % 2) -------------------------------------------------------------------------------- /answers/126.Through the Looking Class: -------------------------------------------------------------------------------- 1 | Class -------------------------------------------------------------------------------- /answers/128. Recognize Playing Cards: -------------------------------------------------------------------------------- 1 | (fn poke [s] 2 | (let [suit-map {\H :heart, \C :club, \D :diamond, \S :spades} 3 | rank-map {\2 0, \3 1, \4 2, \5 3, \6 4, \7 5, 4 | \8 6, \9 7, \T 8, \J 9, \Q 10, \K 11, \A 12} 5 | ] 6 | {:suit (suit-map (first s)), :rank (rank-map (last s))} 7 | )) -------------------------------------------------------------------------------- /answers/13.Sequences rest.clj: -------------------------------------------------------------------------------- 1 | (= [20 30 40] (rest [10 20 30 40])) -------------------------------------------------------------------------------- /answers/134.A nil key: -------------------------------------------------------------------------------- 1 | (true? (#(and (contains? %2 %1) (nil? (%2 %1)) ) :a {:a nil :b 2})) 2 | 3 | (false? (#(and (contains? %2 %1) (nil? (%2 %1)) ) :b {:a nil :b 2})) 4 | 5 | (false? (#(and (contains? %2 %1) (nil? (%2 %1)) ) :c {:a nil :b 2})) 6 | 7 | 8 | 9 | ;; #(nil?(%2 %1 0)) seems better -------------------------------------------------------------------------------- /answers/135.Infix Calculator: -------------------------------------------------------------------------------- 1 | (fn infix [& args] 2 | (reduce (fn [a [op b]] (op a b)) 3 | (first args) 4 | (partition 2 (rest args)))) -------------------------------------------------------------------------------- /answers/14.Intro to Functions.clj: -------------------------------------------------------------------------------- 1 | (= 8 ((fn add-five [x] (+ x 5)) 3)) 2 | 3 | (= 8 ((fn [x] (+ x 5)) 3)) 4 | 5 | (= 8 (#(+ % 5) 3)) 6 | 7 | (= 8 ((partial + 5) 3)) -------------------------------------------------------------------------------- /answers/143.dot product: -------------------------------------------------------------------------------- 1 | (fn [a b] 2 | (apply + (map * a b))) -------------------------------------------------------------------------------- /answers/145.For the win: -------------------------------------------------------------------------------- 1 | (= '(1 5 9 13 17 21 25 29 33 37) (for [x (range 40) 2 | :when (= 1 (rem x 4))] 3 | x)) 4 | 5 | (= '(1 5 9 13 17 21 25 29 33 37) (for [x (iterate #(+ 4 %) 0) 6 | :let [z (inc x)] 7 | :while (< z 40)] 8 | z)) 9 | 10 | (= '(1 5 9 13 17 21 25 29 33 37) (for [[x y] (partition 2 (range 20))] 11 | (+ x y))) -------------------------------------------------------------------------------- /answers/146. Trees into tables: -------------------------------------------------------------------------------- 1 | (fn [mp] 2 | (into {} 3 | (for [[k v] mp 4 | [vk vv] v] 5 | (vec [[k vk] vv])))) 6 | -------------------------------------------------------------------------------- /answers/147. Pascal's Trapezoid: -------------------------------------------------------------------------------- 1 | (fn pas-seq [a] 2 | (let [pas (fn [a] 3 | (let [a1 (concat [0] a) 4 | a2 (concat a [0]) 5 | b (map + a1 a2)] 6 | b))] 7 | (lazy-seq 8 | (concat [a] (pas-seq (pas a) ))))) 9 | ;; or 10 | 11 | (fn [v] 12 | (iterate #(vec (map + (cons 0 %) (conj % 0))) v)) 13 | -------------------------------------------------------------------------------- /answers/15.Double Down.clj: -------------------------------------------------------------------------------- 1 | (= ((fn db [x] (* x 2)) 2) 4) 2 | 3 | (= ((fn db [x] (* x 2)) 3) 6) 4 | 5 | (= ((fn db [x] (* x 2)) 11) 22) 6 | 7 | (= ((fn db [x] (* x 2)) 7) 14) -------------------------------------------------------------------------------- /answers/153.Pairwise Disjoint Sets.clj: -------------------------------------------------------------------------------- 1 | (fn [coll] 2 | (let [ss (for [a coll b coll 3 | :when (not (identical? a b))] 4 | (clojure.set/intersection a b))] 5 | (every? empty? ss))) 6 | ;; best answer 7 | #(apply distinct? (mapcat seq %)) 8 | -------------------------------------------------------------------------------- /answers/156.Map Defaults: -------------------------------------------------------------------------------- 1 | (fn [defval init-keys] 2 | (loop [res {}, key init-keys] 3 | (if (empty? key) 4 | res 5 | (recur 6 | (assoc res (first key) defval) 7 | (rest key)) 8 | ))) 9 | 10 | ;; or 11 | 12 | #(zipmap %2 (repeat %1)) -------------------------------------------------------------------------------- /answers/157. Indexing Sequences: -------------------------------------------------------------------------------- 1 | (fn [v] 2 | (map #(vector %1 %2) v (range ))) 3 | 4 | 5 | #(map vector % (range)) 6 | -------------------------------------------------------------------------------- /answers/16.Hello World.clj: -------------------------------------------------------------------------------- 1 | (= (#(str "Hello, " % "!") "Dave") "Hello, Dave!") 2 | 3 | (= (#(str "Hello, " % "!") "Jenn") "Hello, Jenn!") 4 | 5 | (= (#(str "Hello, " % "!") "Rhea") "Hello, Rhea!") -------------------------------------------------------------------------------- /answers/161. Subset and Superset: -------------------------------------------------------------------------------- 1 | #{2 1 3} -------------------------------------------------------------------------------- /answers/162.Logical falsity and truth: -------------------------------------------------------------------------------- 1 | 1 2 | ;; In Clojure, only nil and false representing the values of logical falsity in conditional tests - anything else is logical truth. -------------------------------------------------------------------------------- /answers/166. Comparisons: -------------------------------------------------------------------------------- 1 | (fn [op a b] 2 | (cond 3 | (op a b) :lt 4 | (op b a) :gt 5 | :else :eq)) -------------------------------------------------------------------------------- /answers/17.Sequences map.clj: -------------------------------------------------------------------------------- 1 | (= '(6 7 8) (map #(+ % 5) '(1 2 3))) -------------------------------------------------------------------------------- /answers/173. Intro to Destructuring 2: -------------------------------------------------------------------------------- 1 | op arg 2 | -------------------------------------------------------------------------------- /answers/18.Sequences filter.clj: -------------------------------------------------------------------------------- 1 | (= '(6 7) (filter #(> % 5) '(3 4 5 6 7))) -------------------------------------------------------------------------------- /answers/19.Last Element.clj: -------------------------------------------------------------------------------- 1 | (= (#(first (reverse %)) [1 2 3 4 5]) 5) 2 | 3 | (= (#(first (reverse %)) '(5 4 3)) 3) 4 | 5 | (= (#(first (reverse %)) ["b" "c" "d"]) "d") -------------------------------------------------------------------------------- /answers/2.Simple Math.clj: -------------------------------------------------------------------------------- 1 | (= (- 10 (* 2 3)) 4) -------------------------------------------------------------------------------- /answers/20.Penultimate Element.clj: -------------------------------------------------------------------------------- 1 | (= (#(first (rest (reverse %))) (list 1 2 3 4 5)) 4) 2 | 3 | (= (#(first (rest (reverse %))) ["a" "b" "c"]) "b") 4 | 5 | (= (#(first (rest (reverse %))) [[1 2] [3 4]]) [1 2]) -------------------------------------------------------------------------------- /answers/21.Nth Element: -------------------------------------------------------------------------------- 1 | (= (#(first (drop %2 %1)) [:a :b :c] 0) :a) 2 | 3 | (= (#(first (drop %2 %1)) [1 2 3 4] 1) 2) 4 | 5 | (= (#(first (drop %2 %1)) '([1 2] [3 4] [5 6]) 2) [5 6]) -------------------------------------------------------------------------------- /answers/22.Count a Sequence: -------------------------------------------------------------------------------- 1 | (= (__ '(1 2 3 3 1)) 5) 2 | 3 | (= (__ "Hello World") 11) 4 | 5 | (= (__ [[1 2] [3 4] [5 6]]) 3) 6 | 7 | (= (__ '(13)) 1) 8 | 9 | (= (__ '(:a :b :c)) 3) 10 | 11 | ;; my answer 12 | #(loop [result 0 c %] 13 | (if(empty? c) result 14 | (recur (inc result) (rest c)))) 15 | ;; others' excellent anser 16 | #(reduce (fn [x y] (inc x)) 0 %) -------------------------------------------------------------------------------- /answers/23.Reverse a Sequence: -------------------------------------------------------------------------------- 1 |  2 | (= (#(into () %) [1 2 3 4 5]) [5 4 3 2 1]) 3 | 4 | (= (#(into () %) (sorted-set 5 7 2 7)) '(7 5 2)) 5 | 6 | (= (#(into () %) [[1 2][3 4][5 6]]) [[5 6][3 4][1 2]]) -------------------------------------------------------------------------------- /answers/24.Sum It All Up: -------------------------------------------------------------------------------- 1 | (= (#(apply + %) [1 2 3]) 6) 2 | 3 | (= (#(apply + %) (list 0 -2 5 5)) 8) 4 | 5 | (= (#(apply + %) #{4 2 1}) 7) 6 | 7 | (= (#(apply + %) '(0 0 -1)) -1) 8 | 9 | (= (#(apply + %) '(1 10 3)) 14) -------------------------------------------------------------------------------- /answers/25.Find the odd numbers: -------------------------------------------------------------------------------- 1 | (= (#(filter odd? %) #{1 2 3 4 5}) '(1 3 5)) 2 | 3 | (= (#(filter odd? %) [4 2 1 6]) '(1)) 4 | 5 | (= (#(filter odd? %) [2 2 4 6]) '()) 6 | 7 | (= (#(filter odd? %) [1 1 1 3]) '(1 1 1 3)) -------------------------------------------------------------------------------- /answers/26.Fibonacci Sequence: -------------------------------------------------------------------------------- 1 | #(take % (map last(iterate (fn [[x y]] [y (+ x y)]) [ 0 1]))) 2 | 3 | ;; it's quite nice to use iterate here!!! 4 | 5 | 6 | -------------------------------------------------------------------------------- /answers/27.Palindrome Detector: -------------------------------------------------------------------------------- 1 | #(= (seq %) (reverse %)) 2 | 3 | ;;(seq "abc") result in (\a \b \c) 4 | ;; reverse will not evaluated as string ether. -------------------------------------------------------------------------------- /answers/28.Flatten a Sequence: -------------------------------------------------------------------------------- 1 | #(filter (complement sequential?) (tree-seq sequential? identity %)) 2 | 3 | ;; complement 4 | ;; tree-seq -------------------------------------------------------------------------------- /answers/29.Get the Caps: -------------------------------------------------------------------------------- 1 | #(apply str (re-seq #"[A-Z]" %)) -------------------------------------------------------------------------------- /answers/3.Intro to Strings.clj: -------------------------------------------------------------------------------- 1 | (= "HELLO WORLD" (.toUpperCase "hello world")) -------------------------------------------------------------------------------- /answers/30.Compress a Sequence: -------------------------------------------------------------------------------- 1 | (fn [input] 2 | (loop [i input res []] 3 | (if (empty? i) 4 | res 5 | (if (= (last res) (first i)) 6 | (recur (rest i) res) 7 | (recur (rest i) (conj res (first i)))) 8 | ))) 9 | 10 | 11 | ;; use partition-by 12 | 13 | #(map first (partition-by identity %)) -------------------------------------------------------------------------------- /answers/32.Duplicate a Sequence: -------------------------------------------------------------------------------- 1 | #(interleave % %) -------------------------------------------------------------------------------- /answers/32.Pack a Sequence: -------------------------------------------------------------------------------- 1 | #(partition-by identity %) -------------------------------------------------------------------------------- /answers/33.Replicate a Sequence: -------------------------------------------------------------------------------- 1 | #(apply concat (map (fn [input](repeat %2 input)) %1)) 2 | #(mapcat (fn [input](repeat %2 input)) %1) -------------------------------------------------------------------------------- /answers/34. Implement range: -------------------------------------------------------------------------------- 1 | #(take (- %2 %1) (iterate inc %)) -------------------------------------------------------------------------------- /answers/35.Local bindings.clj: -------------------------------------------------------------------------------- 1 | (= 7 (let [x 5] (+ 2 x))) 2 | 3 | (= 7 (let [x 3, y 10] (- y x))) 4 | 5 | (= 7 (let [x 21] (let [y 3] (/ x y)))) -------------------------------------------------------------------------------- /answers/36.Let it Be.clj: -------------------------------------------------------------------------------- 1 | (= 10 (let [x 7, y 3, z 1] (+ x y))) 2 | 3 | (= 4 (let [x 7, y 3, z 1] (+ y z))) 4 | 5 | (= 1 (let [x 7, y 3, z 1] z)) -------------------------------------------------------------------------------- /answers/37.Regular Expressions.clj: -------------------------------------------------------------------------------- 1 | (= "ABC" (apply str (re-seq #"[A-Z]+" "bA1B3Ce "))) -------------------------------------------------------------------------------- /answers/38.Maximum value: -------------------------------------------------------------------------------- 1 | #(-> %& sort last) 2 | 3 | ;; %& stand for all arguments, as a sequence 4 | -------------------------------------------------------------------------------- /answers/39.Interleave Two Seqs: -------------------------------------------------------------------------------- 1 | mapcat vector -------------------------------------------------------------------------------- /answers/4.Intro to Lists.clj: -------------------------------------------------------------------------------- 1 | (= (list :a, :b, :c) '(:a :b :c)) -------------------------------------------------------------------------------- /answers/40.Interpose a Seq: -------------------------------------------------------------------------------- 1 | #(-> (interleave %2 (repeat %1)) drop-last vec) -------------------------------------------------------------------------------- /answers/41Drop Every Nth Item.txt: -------------------------------------------------------------------------------- 1 | (fn [vec n] 2 | (mapcat #(take (dec n) %) (partition-all n vec)) 3 | ) -------------------------------------------------------------------------------- /answers/42.Factorial Fun: -------------------------------------------------------------------------------- 1 | #(reduce * (range 1 (inc %))) -------------------------------------------------------------------------------- /answers/43. Reverse Interleave: -------------------------------------------------------------------------------- 1 | (fn rev-int [v n] 2 | (apply map vector (partition n v))) -------------------------------------------------------------------------------- /answers/44. Rotate Sequence: -------------------------------------------------------------------------------- 1 | (fn rotate [n v] 2 | (->> (concat v v) 3 | (drop (mod n (count v))) 4 | (take (count v)) 5 | ) 6 | ) 7 | -------------------------------------------------------------------------------- /answers/45.Intro to Iterate: -------------------------------------------------------------------------------- 1 | '(1 4 7 10 13) -------------------------------------------------------------------------------- /answers/46. Flipping out: -------------------------------------------------------------------------------- 1 | #(fn [a b] (% b a)) -------------------------------------------------------------------------------- /answers/47.Contain Yourself: -------------------------------------------------------------------------------- 1 | (contains? #{4 5 6} 4) 2 | 3 | (contains? [1 1 1 1 1] 4) 4 | 5 | (contains? {4 :a 2 :b} 4) 6 | 7 | (not (contains? '(1 2 4) 4)) 8 | 9 | ;;http://clojuredocs.org/clojure_core/clojure.core/contains_q -------------------------------------------------------------------------------- /answers/48.Intro to some: -------------------------------------------------------------------------------- 1 | (= 6 (some #{2 7 6} [5 6 7 8])) 2 | 3 | (= 6 (some #(when (even? %) %) [5 6 7 8])) 4 | 5 | ;;The some function takes a predicate function and a collection. It returns the first logical true value of (predicate x) where x is an item in the collection. -------------------------------------------------------------------------------- /answers/49.Split a sequence: -------------------------------------------------------------------------------- 1 | #(vector 2 | (vec (take %1 %2)) 3 | (vec (drop %1 %2))) -------------------------------------------------------------------------------- /answers/5.Lists conj.clj: -------------------------------------------------------------------------------- 1 | (= '(1 2 3 4) (conj '(2 3 4) 1)) 2 | (= '(1 2 3 4) (conj '(3 4) 2 1)) -------------------------------------------------------------------------------- /answers/50. Split by Type: -------------------------------------------------------------------------------- 1 | (fn split-by-type [v] 2 | (for [[key value] (group-by class v)] 3 | value)) 4 | -------------------------------------------------------------------------------- /answers/51.Advanced Destructuring: -------------------------------------------------------------------------------- 1 | [1 2 3 4 5] 2 | 3 | ;; as: -------------------------------------------------------------------------------- /answers/54. Partition a Sequence: -------------------------------------------------------------------------------- 1 | (fn my-partition [n v] 2 | (if (>= (count v) n) 3 | (cons (take n v) (my-partition n (drop n v))))) -------------------------------------------------------------------------------- /answers/55. Count Occurrences: -------------------------------------------------------------------------------- 1 | #(into {} 2 | (map (fn [[k v]] [k (count v)]) (group-by identity %))) -------------------------------------------------------------------------------- /answers/56. Find Distinct Items: -------------------------------------------------------------------------------- 1 | reduce (fn [s e] 2 | (if (some #(= % e) s) 3 | s 4 | (conj s e))) 5 | [] -------------------------------------------------------------------------------- /answers/57.Simple Recursion.clj: -------------------------------------------------------------------------------- 1 | (= __ ((fn foo [x] (when (> x 0) (conj (foo (dec x)) x))) 5)) -------------------------------------------------------------------------------- /answers/58. Function Composition.txt: -------------------------------------------------------------------------------- 1 | (fn comb [& funcs] 2 | (fn [& args] 3 | (first 4 | (reduce #(vector (apply %2 %1)) args (reverse funcs ))))) -------------------------------------------------------------------------------- /answers/59. Juxtaposition: -------------------------------------------------------------------------------- 1 | (fn my-juxt [& funcs] 2 | (fn [& args] 3 | (map #(apply % args) funcs ))) -------------------------------------------------------------------------------- /answers/6.Intro to Vectors.clj: -------------------------------------------------------------------------------- 1 | (= [:a :b :c] (list :a :b :c) (vec '(:a :b :c)) (vector :a :b :c)) -------------------------------------------------------------------------------- /answers/60. Sequence Reductions: -------------------------------------------------------------------------------- 1 | (fn my-reduce 2 | 3 | ([op input] (my-reduce op (first input) (rest input))) 4 | 5 | ([op result input] 6 | 7 | (lazy-seq 8 | (if (empty? input) (list result) 9 | (cons result 10 | (my-reduce op 11 | (op result (first input)) 12 | (rest input))))))) -------------------------------------------------------------------------------- /answers/61.Map Construction: -------------------------------------------------------------------------------- 1 | #(into {} (map vector %1 %2)) 2 | 3 | ;; well, into is so magic -------------------------------------------------------------------------------- /answers/62.Re-implement Iterate: -------------------------------------------------------------------------------- 1 | (fn it [f x] 2 | (lazy-seq (cons x (it f (f x))))) 3 | 4 | ;; use lazy-seq to avoid stackoverflow 5 | ;; you can recurse on anonimouse function -------------------------------------------------------------------------------- /answers/63.Group a Sequence: -------------------------------------------------------------------------------- 1 | (fn grp-seq [func vals] 2 | (into {} 3 | (map #(vector (func (first % )) (vec %)) 4 | (partition-by func (sort vals))))) 5 | -------------------------------------------------------------------------------- /answers/64.Intro to Reduce.clj: -------------------------------------------------------------------------------- 1 | (= 15 (reduce + [1 2 3 4 5])) 2 | 3 | (= 0 (reduce + [])) 4 | 5 | (= 6 (reduce + 1 [2 3])) -------------------------------------------------------------------------------- /answers/65. Black Box Testing.clj: -------------------------------------------------------------------------------- 1 | #({{} :map #{} :set} (empty %) (if (reversible? %) :vector :list)) 2 | -------------------------------------------------------------------------------- /answers/66.Greatest Common Divisor: -------------------------------------------------------------------------------- 1 | (fn [a b] 2 | (let [get-divisor (fn [n] (into #{} 3 | (filter #(zero? (rem n %)) 4 | (range 1 (inc n))) 5 | )) 6 | a-divisor (get-divisor a) 7 | b-divisor (get-divisor b) 8 | commom-divisor (clojure.set/intersection a-divisor b-divisor)] 9 | (apply max commom-divisor))) 10 | 11 | 12 | -------------------------------------------------------------------------------- /answers/67. Prime Numbers: -------------------------------------------------------------------------------- 1 | (fn [n] 2 | (take n(filter 3 | (fn is-prime [n] 4 | (nil? 5 | (some 6 | #(zero? (mod n %)) 7 | (range 2 n)))) 8 | (range 2 1000)))) -------------------------------------------------------------------------------- /answers/68.Recurring Theme.clj: -------------------------------------------------------------------------------- 1 | (= '(7 6 5 4 3) 2 | (loop [x 5 3 | result []] 4 | (if (> x 0) 5 | (recur (dec x) (conj result (+ 2 x))) 6 | result))) -------------------------------------------------------------------------------- /answers/69. Merge with a Function.clj: -------------------------------------------------------------------------------- 1 | (fn [f & args] 2 | (reduce (fn[map1 map2] 3 | (reduce (fn [m [k v]] 4 | (if-let [vv (m k)] 5 | (assoc m k (f vv v)) 6 | (assoc m k v))) 7 | map1 map2)) 8 | args)) 9 | -------------------------------------------------------------------------------- /answers/7.Vectors conj.clj: -------------------------------------------------------------------------------- 1 | (= [1 2 3 4] (conj [1 2 3] 4)) 2 | (= [1 2 3 4] (conj [1 2] 3 4)) -------------------------------------------------------------------------------- /answers/70. Word Sorting: -------------------------------------------------------------------------------- 1 | #(sort-by (fn [v](.toLowerCase v)) (re-seq #"\w+" %)) -------------------------------------------------------------------------------- /answers/71.Rearranging Code.clj: -------------------------------------------------------------------------------- 1 | (= (last (sort (rest (reverse [2 5 4 1 3 6])))) 2 | (-> [2 5 4 1 3 6] reverse rest sort last) 3 | 5) -------------------------------------------------------------------------------- /answers/72.Rearranging Code.clj: -------------------------------------------------------------------------------- 1 | (= (reduce + (map inc (take 3 (drop 2 [2 5 4 1 3 6])))) 2 | (->> [2 5 4 1 3 6] (drop 2) (take 3) (map inc) (reduce +)) 3 | 11) -------------------------------------------------------------------------------- /answers/74. Filter Perfect Squares: -------------------------------------------------------------------------------- 1 | (fn perf-square [s] 2 | (let [nums (map #(Integer/valueOf %) (clojure.string/split s #",")) 3 | all-perf-sq (set (map #(* % %) (range 100))) 4 | sq-nums (filter all-perf-sq nums)] 5 | (apply str (interpose "," sq-nums)))) -------------------------------------------------------------------------------- /answers/76. Intro to Trampoline.clj: -------------------------------------------------------------------------------- 1 | [1 3 5 7 9 11] 2 | -------------------------------------------------------------------------------- /answers/77.Anagram Finder: -------------------------------------------------------------------------------- 1 | (fn [v] 2 | (into #{} 3 | (map set 4 | (filter #(> (count %) 1) 5 | (map val (group-by sort v)))))) -------------------------------------------------------------------------------- /answers/78. Reimplement Trampoline.clj: -------------------------------------------------------------------------------- 1 | (fn 2 | [f & args] 3 | (loop [res (apply f args)] 4 | (if (fn? res) 5 | (recur (res)) 6 | res))) 7 | -------------------------------------------------------------------------------- /answers/8.Intro to Sets.clj: -------------------------------------------------------------------------------- 1 | (= #{:a :b :c :d} (set '(:a :a :b :c :c :c :c :d :d))) 2 | (= #{:a :b :c :d} (clojure.set/union #{:a :b :c} #{:b :c :d})) -------------------------------------------------------------------------------- /answers/80. Perfect Numbers: -------------------------------------------------------------------------------- 1 | (fn is-perf-num [n] 2 | (= n 3 | (apply + (filter #(zero? (mod n %)) (range 1 n))))) 4 | -------------------------------------------------------------------------------- /answers/81.Set Intersection: -------------------------------------------------------------------------------- 1 | (fn doset [sa sb] 2 | (set (filter #(sa %) sb)) 3 | ) 4 | 5 | ;; set : convert a seq to a set -------------------------------------------------------------------------------- /answers/83.A Half-Truth: -------------------------------------------------------------------------------- 1 | #(true? 2 | (and 3 | (some true? %&) 4 | (some false? %&))) 5 | 6 | ;; %& stands for all argument as sequence 7 | ;; some: Returns the first logical true value of (pred x) for any x in coll, else nil. -------------------------------------------------------------------------------- /answers/86. Happy Numbers.clj: -------------------------------------------------------------------------------- 1 | (letfn [(num->digits [num] 2 | (loop [n num res []] 3 | (if (zero? n) 4 | res 5 | (recur (long (/ n 10)) (cons (mod n 10) res))))) 6 | (change [n] 7 | (apply + (map #(* % %) (num->digits n))))] 8 | (fn [init] 9 | (loop [curr init results #{}] 10 | (println curr " - " results) 11 | (cond 12 | (= 1 curr) true 13 | (results curr) false 14 | :else (let [new-n (change curr)] 15 | (println curr new-n) 16 | (recur new-n (into results [curr]))) 17 | ))) 18 | ) 19 | -------------------------------------------------------------------------------- /answers/88.Symmetric Difference: -------------------------------------------------------------------------------- 1 | (fn set-diff [sa sb] 2 | (set 3 | (concat 4 | (filter (complement sb) sa) 5 | (filter (complement sa) sb)))) -------------------------------------------------------------------------------- /answers/9.Sets conj.clj: -------------------------------------------------------------------------------- 1 | (= #{1 2 3 4} (conj #{1 4 3} 2)) -------------------------------------------------------------------------------- /answers/90.Cartesian Product: -------------------------------------------------------------------------------- 1 | (fn [a b] 2 | (set (for [x a y b] 3 | [x y]))) 4 | 5 | ;; for can do cartesian product -------------------------------------------------------------------------------- /answers/95.To Tree, or not to Tree: -------------------------------------------------------------------------------- 1 | (fn istree? [root] 2 | (or (nil? root) 3 | (and (sequential? root) 4 | (= 3 (count root)) 5 | (every? istree? (rest root))))) -------------------------------------------------------------------------------- /answers/96. Beauty is Symmetry: -------------------------------------------------------------------------------- 1 | (fn symmetry [[root left right]] 2 | (let [mirror? (fn mirror? [a b] 3 | (cond 4 | (not= (sequential? a) (sequential? b)) false 5 | (sequential? a) (let [[ra La Ra] a 6 | [rb Lb Rb] b] 7 | (and (= ra rb) (mirror? La Rb) (mirror? Lb Ra))) 8 | :else (= a b)))] 9 | (mirror? left right))) -------------------------------------------------------------------------------- /answers/97.Pascal's Triangle: -------------------------------------------------------------------------------- 1 | (fn pascal-tri [n] 2 | (condp = n 3 | 1 [1] 4 | (let [last (pascal-tri (dec n)) 5 | last-a (concat [0] last) 6 | last-b (concat last [0]) 7 | ] 8 | (vec(map + last-a last-b))))) -------------------------------------------------------------------------------- /answers/99.Product Digits: -------------------------------------------------------------------------------- 1 | ;; the math way 2 | (fn [a b] 3 | (drop-while zero? 4 | (reverse 5 | (map #(mod (int(/ (* a b) %)) 10) 6 | (take 10 (iterate #(* 10 %) 1)))))) 7 | 8 | ;; the trick way 9 | (fn [a b] 10 | (map #(Integer/parseInt (str %)) (str (* a b)))) --------------------------------------------------------------------------------