Last active
January 10, 2017 22:36
-
-
Save thieux/06452a5d00570ac8d24f882d9826970a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns fourclojure.problem) | |
(comment Maximum Value 38) | |
(defn max-of [& positive-numbers] | |
(let [max-acc | |
(fn [ns acc] | |
(if (empty? ns) | |
acc | |
(recur (rest ns) (if (> (first ns) acc) (first ns) acc))))] | |
(max-acc positive-numbers -1))) | |
(= (max-of 1 8 3 4) 8) | |
(= (max-of 30 20) 30) | |
(= (max-of 45 67 11) 67) | |
(comment Get The Caps 39) | |
(defn get-caps [text] | |
(clojure.string/join (re-seq #"[A-Z]+" text))) | |
(= (get-caps "HeLlO, WoRlD!") "HLOWRD") | |
(comment Duplicate Sequence 32) | |
(defn duplicate-seq | |
([sequence] | |
(duplicate-seq sequence [])) | |
([sequence acc] | |
(if (empty? sequence) | |
acc | |
(let [[head & tail] sequence] | |
(recur tail (conj acc head head)))))) | |
(= (duplicate-seq [1 2 3]) '(1 1 2 2 3 3)) | |
(= (duplicate-seq [:a :a :b :b]) '(:a :a :a :a :b :b :b :b)) | |
(= (duplicate-seq [[1 2] [3 4]]) '([1 2] [1 2] [3 4] [3 4])) | |
(comment Implement Range 34) | |
(defn range-in | |
([start end] | |
(range-in start end [])) | |
([start end acc] | |
(if (>= start end) | |
acc | |
(recur (inc start) end (conj acc start))))) | |
(= (range-in 1 4) '(1 2 3)) | |
(= (range-in -2 2) '(-2 -1 0 1)) | |
(= (range-in 5 8) '(5 6 7)) | |
(comment Compress Sequence 30) | |
(defn compress-seq | |
([sequence] | |
(compress-seq sequence nil [])) | |
([sequence previous acc] | |
(if (empty? sequence) | |
acc | |
(recur (rest sequence) | |
(first sequence) | |
(if (or (nil? previous) (not= previous (first sequence))) | |
(conj acc (first sequence)) | |
acc))))) | |
(= (apply str (compress-seq "Leeeeeerrroyyy")) "Leroy") | |
(= (compress-seq [1 1 2 3 3 2 2 3]) '(1 2 3 2 3)) | |
(= (compress-seq [[1 2] [1 2] [3 4] [1 2]]) '([1 2] [3 4] [1 2])) | |
(comment Factorial Fun 42) | |
(defn factorial | |
([n] | |
(factorial n 1)) | |
([n result] | |
(if (zero? n) result (recur (dec n) (* n result))))) | |
(comment Interleave Two Seqs 39) | |
(defn interleave-two-seqs | |
([l r] | |
(interleave-two-seqs l r [])) | |
([l r acc] | |
(if (or (empty? l) (empty? r)) acc | |
(let [[lh & lt] l | |
[rh & rt] r] | |
(let [interleaved (conj acc lh rh)] | |
(if (or (empty? rt) (empty? lt)) | |
interleaved | |
(recur lt rt interleaved))))))) | |
(= (interleave-two-seqs [1 2 3] [:a :b :c]) '(1 :a 2 :b 3 :c)) | |
(= (interleave-two-seqs [1 2] [3 4 5 6]) '(1 3 2 4)) | |
(= (interleave-two-seqs [1 2 3 4] [5]) [1 5]) | |
(comment Flatten a Sequence 28) | |
(fn flatten-seq [sequence] | |
(loop [s sequence | |
acc []] | |
(if (empty? s) | |
acc | |
(let [[head & tail] s] | |
(recur tail | |
(into acc | |
(if (coll? head) (flatten-seq head) [head]))))))) | |
(= (flatten-seq '((1 2) 3 [4 [5 6]])) '(1 2 3 4 5 6)) | |
(= (flatten-seq ["a" ["b"] "c"]) '("a" "b" "c")) | |
(= (flatten-seq '((((:a))))) '(:a)) | |
(comment Replicate a Sequence 33) | |
(defn replicate-seq [seq factor] | |
(let [repetitions | |
(map (fn [e] (repeat factor e)) seq)] | |
(reduce into (conj repetitions [])))) | |
(= (replicate-seq [1 2 3] 2) '(1 1 2 2 3 3)) | |
(= (replicate-seq [:a :b] 4) '(:a :a :a :a :b :b :b :b)) | |
(= (replicate-seq [4 5 6] 1) '(4 5 6)) | |
(= (replicate-seq [[1 2] [3 4]] 2) '([1 2] [1 2] [3 4] [3 4])) | |
(= (replicate-seq [44 33] 2) [44 44 33 33]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment