Next round of 4clojure.com solutions

After the seventh round, five more 4clojure.com solutions:

;;; Problem 60 - Sequence Reduction
(deftest test-problem-60
  (let [v1 (fn fred
             ([f [p1 & ps]] (fred f p1 ps))
             ([f p1 [p2 & ps]]
              (cons p1
                    (when p2
                      (lazy-seq (fred f (f p1 p2) ps))))))
        __ v1]
    (is (= (take 5 (__ + (range))) [0 1 3 6 10]))
    (is (= (__ conj [1] [2 3 4]) [[1] [1 2] [1 2 3] [1 2 3 4]]))
    (is (= (last (__ * 2 [3 4 5])) (reduce * 2 [3 4 5]) 120))))

;;; Problem 59 - Juxtaposition
(deftest test-problem-59
  (let [v1 (fn [& fns]
             (fn [& xs]
               (map #(apply % xs) fns)))
        __ v1]
    (is (= [21 6 1] ((__ + max min) 2 3 5 1 6 4)))
    (is (= ["HELLO" 5] ((__ #(.toUpperCase %) count) "hello")))
    (is (= [2 6 4] ((__ :a :c :b) {:a 2, :b 4, :c 6, :d 8 :e 10})))))

;;; Problem 58 - Function composition
(deftest test-problem-58
  (let [v1 (fn [& fns]
             (fn [& xs]
               (let [[f & rfns] (reverse fns)]
                 (reduce #(%2 %)
                         (apply f xs)
                         rfns))))
        v2 (fn [& fns]
             (reduce (fn [f g]
                       (fn [& xs]
                         (f (apply g xs))))
                     fns))
        __ v2]
    (is (= [3 2 1] ((__ rest reverse) [1 2 3 4])))
    (is (= 5 ((__ (partial + 3) second) [1 2 3 4])))
    (is (= true ((__ zero? #(mod % 8) +) 3 5 7 9)))
    (is (= "HELLO" ((__ #(.toUpperCase %) #(apply str %) take) 5 "hello world")))))

;;; Problem 57 - Simple Recursion
(deftest test-problem-57
  (let [v1 [5 4 3 2 1] 
        __ v1]
    (is (= __ ((fn foo [x] (when (> x 0) (conj (foo (dec x)) x))) 5)))))

;;; Problem 56 - Find Distinct Items
(deftest test-problem-56
  (let [v1 (fn [xs]
             (loop [[e & c] xs a [] d #{}]
               (if e
                 (recur c
                        (if (d e) a (conj a e))
                        (conj d e))
                 a)))
        __ v1]
    (is (= (__ [1 2 1 3 1 2 4]) [1 2 3 4]))
    (is (= (__ [:a :a :b :b :c :c]) [:a :b :c]))
    (is (= (__ '([2 4] [1 2] [1 3] [1 3])) '([2 4] [1 2] [1 3])))
    (is (= (__ (range 50)) (range 50)))))

(run-tests)

Next round of 4clojure.com solutions

After the sixth round, five more 4clojure.com solutions:

;;; Problem 55 - Count Occurrences
(deftest test-problem-55
  (let [v1 (fn [xs]
             (reduce (fn [a v] (assoc a v (inc (get a v 0)))) {} (vec xs)))
        __ v1]
    (is (= (__ [1 1 2 3 2 1 1]) {1 4, 2 2, 3 1}))
    (is (= (__ [:b :a :b :a :b]) {:a 2, :b 3}))
    (is (= (__ '([1 2] [1 3] [1 3])) {[1 2] 1, [1 3] 2}))))

 ;;; Problem 54 - Partition a Sequence
(deftest test-problem-54
  (let [v1 (fn [n xs]
             (loop [a [] r xs]
               (if (< (count r) n)
                 a
                 (recur (conj a (take n r)) (drop n r)))))
        __ v1]
    (is (= (__ 3 (range 9)) '((0 1 2) (3 4 5) (6 7 8))))
    (is (= (__ 2 (range 8)) '((0 1) (2 3) (4 5) (6 7))))
    (is (= (__ 3 (range 8)) '((0 1 2) (3 4 5))))))

 ;;; Problem 53 - Longest Increasing Sub-Seq
(deftest test-problem-53
  (let [subseqs #(take-while not-empty (iterate (partial rest) %))
        filter2 (fn [f xs]
                  (loop [ll (first xs) l [ll] [fr :as r] (rest xs)]
                    (if (or (nil? ll)
                            (nil? fr)
                            (not (f ll fr)))
                      l
              (recur fr (conj l fr) (rest r)))))
        do-run #(map (partial filter2 <) %)
        do-long (partial filter #(> (count %) 1))
        do-sort (partial sort-by #(-' (count %)))
        v1 #(-> % subseqs do-run do-long (conj []) do-sort first)
        __ v1]
    (is (= (__ [1 0 1 2 3 0 4 5]) [0 1 2 3]))
    (is (= (__ [5 6 1 3 2 7]) [5 6]))
    (is (= (__ [2 3 3 4 5]) [3 4 5]))
    (is (= (__ [7 6 5 4]) []))))

 ;;; Problem 52 - Intro to Destructuring
(deftest test-problem-52
  (is (= [2 4] (let [[a b c d e f g] (range)] ))))

 ;;; Problem 51 - Advanced Destructuring
(deftest test-problem-51
  (let [v1 [1 2 3 4 5]
        __ v1]
    (is (= [1 2 [3 4 5] [1 2 3 4 5]] (let [[a b & c :as d] __] [a b c d])))))

(run-tests)

Next round of 4clojure.com solutions

After the fifth round, five more 4clojure.com solutions:

;;; Problem 50 - Split by Type
(deftest test-problem-50
  (let [v1 (fn [xs] (for [t (map type xs)] (filter #(= (type %) t) xs))) 
        v2 (fn [xs] (vals (group-by type xs)))
        __ v2]
    (is (= (set (__ [1 :a 2 :b 3 :c])) #{[1 2 3] [:a :b :c]}))
    (is (= (set (__ [:a "foo"  "bar" :b])) #{[:a :b] ["foo" "bar"]}))
    (is (= (set (__ [[1 2] :a [3 4] 5 6 :b])) #{[[1 2] [3 4]] [:a :b] [5 6]}))))
 
;;; Problem 49 - Split a sequence
(deftest test-problem-49
  (let [v1 (fn [n xs] [(take n xs) (drop n xs)]) 
        __ v1]
    (is (= (__ 3 [1 2 3 4 5 6]) [[1 2 3] [4 5 6]]))
    (is (= (__ 1 [:a :b :c :d]) [[:a] [:b :c :d]]))
    (is (= (__ 2 [[1 2] [3 4] [5 6]]) [[[1 2] [3 4]] [[5 6]]]))))
 
;;; Problem 48 - Intro to some
(deftest test-problem-48
  (let [v1 6 
        __ v1]
    (is (= __ (some #{2 7 6} [5 6 7 8])))
    (is (= __ (some #(when (even? %) %) [5 6 7 8])))))

;;; Problem 47 - Contain Yourself
(deftest test-problem-47
  (let [v1 4 
        __ v1]
    (is (contains? #{4 5 6} __))
    (is (contains? [1 1 1 1 1] __))
    (is (contains? {4 :a 2 :b} __))
    ; No longer works in CLojure 1.5+: http://clojuredocs.org/clojure_core/clojure.core/contains_q
    ; (is (not (contains? '(1 2 4) __))) 
    ))

;;; Problem 46 - Flipping out
(deftest test-problem-46
  (let [v1 (fn [f] #(f %2 %)) 
        __ v1]
    (is (= 3 ((__ nth) 2 [1 2 3 4 5])))
    (is (= true ((__ >) 7 8)))
    (is (= 4 ((__ quot) 2 8)))
    (is (= [1 2 3] ((__ take) [1 2 3 4 5] 3)))))

(run-tests)

Next round of 4clojure.com solutions

After the fourth round, five more 4clojure.com solutions:

;;; Problem 45 - Intro to Iterate
(deftest test-problem-45
  (let [v1 [1 4 7 10 13] 
        __ v1]
    (is (= __ (take 5 (iterate #(+ 3 %) 1))))))
 
;;; Problem 44 - Rotate Sequence
(deftest test-problem-44
  (let [v1 (fn [n xs] (let [k (mod n (count xs))] (concat (drop k xs) (take k xs))))
        __ v1]
    (is (= (__ 2 [1 2 3 4 5]) '(3 4 5 1 2)))
    (is (= (__ -2 [1 2 3 4 5]) '(4 5 1 2 3)))
    (is (= (__ 6 [1 2 3 4 5]) '(2 3 4 5 1)))
    (is (= (__ 1 '(:a :b :c)) '(:b :c :a)))
    (is (= (__ -4 '(:a :b :c)) '(:c :a :b)))))
 
;;; Problem 43 - Reverse Interleave
(deftest test-problem-43
  (let [v1 (fn [xs n] (partition (/ (count xs) n) (apply interleave (partition n xs)))) 
        __ v1]
    (is (= (__ [1 2 3 4 5 6] 2) '((1 3 5) (2 4 6))))
    (is (= (__ (range 9) 3) '((0 3 6) (1 4 7) (2 5 8))))
    (is (= (__ (range 10) 5) '((0 5) (1 6) (2 7) (3 8) (4 9))))))
 
;;; Problem 42 - Factorial Fun 
(deftest test-problem-42
  (let [v1 #(apply * (range 1 (inc %)))
        __ v1]
    (is (= (__ 1) 1))
    (is (= (__ 3) 6))
    (is (= (__ 5) 120))
    (is (= (__ 8) 40320))))
 
;;; Problem 41 - Drop Every Nth Item
(deftest test-problem-41
  (let [v1 (fn [xs n] (apply concat (map #(take (dec n) %) (partition-all n xs))))
        __ v1] 
    (is (= (__ [1 2 3 4 5 6 7 8] 3) [1 2 4 5 7 8]))
    (is (= (__ [:a :b :c :d :e :f] 2) [:a :c :e]))
    (is (= (__ [1 2 3 4 5 6] 4) [1 2 3 5 6]))))
 
(run-tests)