My 4Clojure Solutions(1 to 50)
All the code snippets below are live and interactive. You can modify the code and it will be evaluated in your browser as you type.
1. Nothing but the Truth
xxxxxxxxxx
(= true true)
xtrue
2. Simple Math
xxxxxxxxxx
(= (- 10 (* 2 3)) 4)
xxxxxxxxxx
true
3. Intro to Strings
xxxxxxxxxx
(= "HELLO WORLD" (.toUpperCase "hello world"))
xxxxxxxxxx
true
4. Intro to Lists
xxxxxxxxxx
(= (list :a :b :c) (:a :b :c))
xxxxxxxxxx
true
5. Lists: conj
xxxxxxxxxx
(= (1 2 3 4) (conj (2 3 4) 1))
xxxxxxxxxx
true
xxxxxxxxxx
(= (1 2 3 4) (conj (3 4) 2 1))
xxxxxxxxxx
true
6. Intro to Vectors
xxxxxxxxxx
(= [:a :b :c] (list :a :b :c) (vec (:a :b :c)) (vector :a :b :c))
xxxxxxxxxx
true
7. Vectors: conj
xxxxxxxxxx
(= [1 2 3 4] (conj [1 2 3] 4))
xxxxxxxxxx
true
xxxxxxxxxx
(= [1 2 3 4] (conj [1 2] 3 4))
xxxxxxxxxx
true
8. Intro to Sets
xxxxxxxxxx
(= {:a :b :c :d} (set (:a :a :b :c :c :c :c :d :d)))
xxxxxxxxxx
true
xxxxxxxxxx
(= {:a :b :c :d} (clojure.set/union {:a :b :c} {:b :c :d}))
xxxxxxxxxx
true
9. Sets: conj
xxxxxxxxxx
(= {1 2 3 4} (conj {1 4 3} 2))
xxxxxxxxxx
true
10. Intro to Maps
xxxxxxxxxx
(= 20 ((hash-map :a 10, :b 20, :c 30) :b))
xxxxxxxxxx
true
xxxxxxxxxx
(= 20 (:b {:a 10, :b 20, :c 30}))
xxxxxxxxxx
true
11. Maps: conj
xxxxxxxxxx
(= {:a 1, :b 2, :c 3} (conj {:a 1} [:b 2] [:c 3]))
xxxxxxxxxx
true
12. Intro to Sequences
xxxxxxxxxx
(= 3 (first (3 2 1)))
xxxxxxxxxx
true
xxxxxxxxxx
(= 3 (second [2 3 4]))
xxxxxxxxxx
true
xxxxxxxxxx
(= 3 (last (list 1 2 3)))
xxxxxxxxxx
true
13. Sequences: rest
xxxxxxxxxx
(= [20 30 40] (rest [10 20 30 40]))
xxxxxxxxxx
true
14. Intro to Functions
xxxxxxxxxx
(= 8 ((fn add-five [x] (+ x 5)) 3))
xxxxxxxxxx
true
xxxxxxxxxx
(= 8 ((fn [x] (+ x 5)) 3))
xxxxxxxxxx
true
xxxxxxxxxx
(= 8 ((+ % 5) 3))
xxxxxxxxxx
true
xxxxxxxxxx
(= 8 ((partial + 5) 3))
xxxxxxxxxx
true
15. Double Down
xxxxxxxxxx
(defn double-down [x]
(* x 2))
(= (double-down 3) 6)
xxxxxxxxxx
true
16. Hello World
xxxxxxxxxx
(= ((str "Hello, " % "!") "Dave")
"Hello, Dave!")
xxxxxxxxxx
true
17. Sequences: map
xxxxxxxxxx
(= (6 7 8) (map (+ % 5) (1 2 3)))
xxxxxxxxxx
true
18. Sequences: filter
xxxxxxxxxx
(= (6 7) (filter (> % 5) (3 4 5 6 7)))
xxxxxxxxxx
true
19. Last Element
xxxxxxxxxx
(= (-> [1 2 3 4 5]
reverse
first) 5)
xxxxxxxxxx
true
xxxxxxxxxx
(defn last-elem [[head & tail]]
(if (seq tail)
(recur tail)
head))
(= (last-elem [1 2 3 4 5]) 5)
xxxxxxxxxx
true
20. Penultimate Element
xxxxxxxxxx
(= (-> (1 2 3 4 5)
reverse
rest
first) 4)
xxxxxxxxxx
true
xxxxxxxxxx
(defmulti penultimate
(fn [input]
(= (count input) 1)))
(defmethod penultimate true [input] nil)
(defmethod penultimate false [[head after_head & tail :as input]]
(if (seq tail)
(recur (rest input))
head))
(println (= (penultimate (1 2 3 4 5)) 4))
(println (= (penultimate (1)) nil))
(println (= (penultimate ()) nil))
xxxxxxxxxx
true
nil
true
21. Nth Element
xxxxxxxxxx
(defn my-nth [input_seq n]
{:pre [(>= n 0)]}
(loop [counter 0
input input_seq]
(if (= counter n)
(first input)
(recur (inc counter) (rest input)))))
(println (= (my-nth (4 5 6 7) 2) 6))
(my-nth (4 5 6 7) -1)
xxxxxxxxxx
true
true
Execution error.
ERROR: Error: Assert failed: (>= n 0)
xxxxxxxxxx
(= ((->> %1
(drop %2)
first) (4 5 6 7) 2) 6)
xxxxxxxxxx
true
true
true
true
true
22. Count a Sequence
xxxxxxxxxx
(defn my-count [input_seq]
(apply +
(for [elem input_seq] 1)))
(println (= (my-count (1 2 3 3 1)) 5))
(println (= (my-count "Hello World") 11))
xxxxxxxxxx
nil
23. Reverse a Sequence
xxxxxxxxxx
(= ((into () %) [1 2 3 4 5])
[5 4 3 2 1])
xxxxxxxxxx
true
24. Sum It All Up
xxxxxxxxxx
(= ((apply + %) [1 2 3]) 6)
xxxxxxxxxx
true
25. Find the odd numbers
xxxxxxxxxx
(= ((filter odd? %) [4 2 1 6]) (1))
xxxxxxxxxx
true
26. Fibonacci Sequence
xxxxxxxxxx
(defn fib [n]
(->> (iterate (fn [[a b]] [b (+ a b)]) [1 1])
(map first)
(take n)))
(= (fib 8) (1 1 2 3 5 8 13 21))
xxxxxxxxxx
true
27. Palindrome Detector
xxxxxxxxxx
(defn palindrome? [[head & tail :as input_seq]]
(cond
(= (count input_seq) 1) true
(empty? input_seq) true
:else (and (= head (last tail)) (recur (butlast tail)))))
(true? (palindrome? "racecar"))
xxxxxxxxxx
true
28. Flatten a Sequence
xxxxxxxxxx
(defn- my-flatten-helper [[head & tail :as input_seq] result]
(cond
(empty? input_seq) result
(sequential? head) (recur tail (concat (my-flatten-helper head ()) result))
:else (recur tail (conj result head))))
(defn my-flatten [input-seq]
(reverse (my-flatten-helper input-seq ())))
(println (= (my-flatten ((1 2) 3 [4 [5 6]])) (1 2 3 4 5 6)))
(println (= (my-flatten ["a" ["b"] "c"]) ("a" "b" "c")))
(println (= (my-flatten ((((:a))))) (:a)))
xxxxxxxxxx
true
nil
29. Get the Caps
xxxxxxxxxx
(defn is-upper? [input_char]
(contains?
(set "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
input_char))
(defn get-caps [input_string]
(apply str (filter is-upper? input_string)))
(println (= (get-caps "HeLlO, WoRlD!") "HLOWRD"))
(println (empty? (get-caps "nothing")))
(println (= (get-caps "$#A(*&987Zf") "AZ"))
xxxxxxxxxx
true
true
nil
30. Compress a Sequence
xxxxxxxxxx
(defn compress [input_seq]
(loop [[head & tail :as input] input_seq
result ()]
(cond
(empty? input) (reverse result)
(= head (first result)) (recur tail result)
:else (recur tail (cons head result)))))
(= (apply str (compress "Leeeeeerrroyyy")) "Leroy")
xxxxxxxxxx
true
true
31. Pack a Sequence
xxxxxxxxxx
(defn pack [input_seq]
(loop [[head & tail :as input] input_seq
[result_head & result_tail :as result] ()]
(cond
(empty? input) (reverse result)
(= head (first result_head)) (recur tail (cons (cons head result_head) result_tail))
:else (recur tail (cons (list head) result)))))
(= (pack [1 1 2 1 1 1 3 3]) ((1 1) (2) (1 1 1) (3 3)))
xxxxxxxxxx
true
32. Duplicate a Sequence
xxxxxxxxxx
(defn duplicate [input_seq]
(loop [[head & tail :as input] input_seq
result ()]
(if (empty? input)
(reverse result)
(recur tail (conj result head head)))))
(= (duplicate [1 2 3]) (1 1 2 2 3 3))
xxxxxxxxxx
true
33. Replicate a Sequence
xxxxxxxxxx
(defn replicate [input_seq n]
(loop [[head & tail :as input] input_seq
result ()]
(if (empty? input)
(reverse result)
(recur tail (concat (repeat n head) result)))))
(= (replicate [1 2 3] 2) (1 1 2 2 3 3))
xxxxxxxxxx
WARNING: replicate already refers to: cljs.core/replicate being replaced by: cljs.user/replicate at line 1
true
34. Implement range
xxxxxxxxxx
(defn my-range [start end]
(loop [counter start
result ()]
(if (= counter end)
(reverse result)
(recur (inc counter) (conj result counter)))))
(= (my-range 1 4) (1 2 3))
xxxxxxxxxx
true
35. Local bindings
xxxxxxxxxx
(= 7 (let [x 5] (+ 2 x)))
(= 7 (let [x 3, y 10] (- y x)))
(= 7 (let [x 21] (let [y 3] (/ x y))))
xxxxxxxxxx
true
36. Let it Be
xxxxxxxxxx
(println (= 10 (let [x 7 y 3 z 1] (+ x y))))
(println (= 4 (let [x 7 y 3 z 1] (+ y z))))
(println (= 1 (let [x 7 y 3 z 1] z)))
xxxxxxxxxx
true
nil
37. Regular Expressions
xxxxxxxxxx
(= "ABC" (apply str (re-seq "[A-Z]+" "bA1B3Ce ")))
xxxxxxxxxx
true
true
38. Maximum value
xxxxxxxxxx
(defn my-max [& args]
(loop [[head & tail :as input] args
result head]
(cond
(empty? input) result
(> head result) (recur tail head)
:else (recur tail result))))
(= (my-max 45 67 11) 67)
xxxxxxxxxx
true
true
true
true
true
true
39. Interleave Two Seqs
xxxxxxxxxx
(defn my-interleave [seq1 seq2]
(loop [[head1 & tail1 :as s1] seq1
[head2 & tail2 :as s2] seq2
result ()]
(cond
(or (empty? s1) (empty? s2)) (reverse result)
:else (recur tail1 tail2 (conj result head1 head2)))))
(= (my-interleave [1 2 3] [:a :b :c]) (1 :a 2 :b 3 :c))
xxxxxxxxxx
true
40. Interpose a Seq
xxxxxxxxxx
(defn my-interpose [elem input_seq]
(loop [[head & tail :as input] input_seq
result ()]
(if (empty? input)
(butlast (reverse result))
(recur tail (conj result head elem)))))
(= (my-interpose :z [:a :b :c :d]) [:a :z :b :z :c :z :d])
xxxxxxxxxx
true
41. Drop Every Nth Item
xxxxxxxxxx
(defn drop-nth [input_seq n]
(loop [[head & tail :as input] input_seq
counter 1
result ()]
(cond
(empty? input) (reverse result)
(= counter n) (recur tail 1 result)
:else (recur tail (inc counter) (conj result head)))))
(= (drop-nth [1 2 3 4 5 6] 4) [1 2 3 5 6])
xxxxxxxxxx
true
42. Factorial Fun
xxxxxxxxxx
(def factorial (memoize (fn [n]
{:pre [(>= n 0)]}
(condp = n
0 1
(* n (factorial (dec n)))))))
(= (factorial 8) 40320)
xxxxxxxxxx
true
43. Reverse Interleave
(defn reverse-interleave [input_seq n]
(loop [input input_seq
result (vec (repeat n []))
counter 0]
(match [input counter]
[([] :seq) _] result
[_ n] (recur input result 0)
[([head & tail] :seq) counter] (recur tail
(update-in result [counter] #(conj % head))
(inc counter)))))
(println (= (reverse-interleave [1 2 3 4 5 6] 2) '((1 3 5) (2 4 6))))
(println (= (reverse-interleave (range 9) 3) '((0 3 6) (1 4 7) (2 5 8))))
(println (= (reverse-interleave (range 10) 5) '((0 5) (1 6) (2 7) (3 8) (4 9))))
44. Rotate Sequence
xxxxxxxxxx
(defn- rotate-left [n input_seq]
{:pre [(>= n 0)]}
(loop [result input_seq
counter (mod n (count input_seq))]
(let [head (first result)
tail (vec (rest result))]
(if (= counter 0)
result
(recur (conj tail head) (dec counter))))))
(defn- rotate-right [n input_seq]
{:pre [(>= n 0)]}
(->> input_seq
(reverse)
(rotate-left n)
(reverse)))
(defn rotate [n input_seq]
(cond
(= n 0) input_seq
(> n 0) (rotate-left n input_seq)
(< n 0) (rotate-right (* -1 n) input_seq)))
(println (= (rotate 2 [1 2 3 4 5]) (3 4 5 1 2)))
(println (= (rotate -2 [1 2 3 4 5]) (4 5 1 2 3)))
(println (= (rotate 6 [1 2 3 4 5]) (2 3 4 5 1)))
(println (= (rotate 1 (:a :b :c)) (:b :c :a)))
(println (= (rotate -4 (:a :b :c)) (:c :a :b)))
xxxxxxxxxx
true
true
nil
true
45. Intro to Iterate
xxxxxxxxxx
(= [1 4 7 10 13] (take 5 (iterate (+ 3 %) 1)))
xxxxxxxxxx
true
true
46. Flipping out
xxxxxxxxxx
(defn flip [f]
(fn [& args]
(apply f (reverse args))))
(= [1 2 3] ((flip take) [1 2 3 4 5] 3))
xxxxxxxxxx
true
true
true
47. Contain Yourself
xxxxxxxxxx
(println (contains? {4 5 6} 4))
(println (contains? [1 1 1 1 1] 4))
(println (contains? {4 :a 2 :b} 4))
(println (not (contains? [1 2 4] 4)))
xxxxxxxxxx
true
nil
48. Intro to some
xxxxxxxxxx
(println (= 6 (some {2 7 6} [5 6 7 8])))
(println (= 6 (some (when (even? %) %) [5 6 7 8])))
xxxxxxxxxx
true
true
true
nil
49. Split a sequence
xxxxxxxxxx
(defn my-split [n input_seq]
[(subvec input_seq 0 n) (subvec input_seq n)])
(println (= (my-split 3 [1 2 3 4 5 6]) [[1 2 3] [4 5 6]]))
(println (= (my-split 1 [:a :b :c :d]) [[:a] [:b :c :d]]))
(println (= (my-split 2 [[1 2] [3 4] [5 6]]) [[[1 2] [3 4]] [[5 6]]]))
xxxxxxxxxx
nil
50. Split by Type
xxxxxxxxxx
(defn- split-by-type-helper [t input_seq]
(filterv (instance? t %) input_seq))
(defn split-by-type [input_seq]
(let [types (set (map type input_seq))]
(set (for [t types]
(split-by-type-helper t input_seq)))))
xxxxxxxxxx
cljs.user/split-by-type