Markdown 4clojure solutions generated with 4clojure-to-md
#1 Nothing but the Truth [Elementary]
true
4
#3 Intro to Strings [Elementary]
"HELLO WORLD"
#4 Intro to Lists [Elementary]
:a :b :c
'(1 2 3 4)
#6 Intro to Vectors [Elementary]
:a :b :c
[1 2 3 4]
#{:a :b :c :d}
2
#10 Intro to Maps [Elementary]
20
{:b 2}
#12 Intro to Sequences [Elementary]
3
#13 Sequences: rest [Elementary]
[20 30 40]
#14 Intro to Functions [Elementary]
8
(partial * 2)
#(str "Hello, " % "!")
#17 Sequences: map [Elementary]
'(6 7 8)
#18 Sequences: filter [Elementary]
'(6 7)
(fn my-last [coll]
(if (empty? (rest coll))
(first coll)
(my-last (rest coll))))
#20 Penultimate Element [Easy]
(fn my-last [coll]
(if (empty? (rest (rest coll)))
(first coll)
(my-last (rest coll))))
(fn my-nth [coll n]
(if (= n 0)
(first coll)
(my-nth (rest coll) (dec n))))
(fn my-count [coll]
(if (empty? coll) 0
(inc (my-count (rest coll)))))
(fn my-reverse [coll]
(if (empty? coll) nil
(concat (my-reverse (rest coll)) (list (first coll)))))
(fn sum [coll]
(if (empty? coll) 0
(+ (first coll) (sum (rest coll)))))
#25 Find the odd numbers [Easy]
(fn filter-odd [coll]
(if (empty? coll) '()
(let [elem (first coll)]
(if (= (mod elem 2) 1)
(conj (filter-odd (rest coll)) elem)
(filter-odd (rest coll))))))
#(take % ((fn fibgen [n1 n2] (lazy-seq (cons n1 (fibgen n2 (+ n1 n2))))) 1 1))
#27 Palindrome Detector [Easy]
#(= (seq %) (reverse (seq %)))
(fn flat [coll]
(when-let [s (seq coll)]
(if (coll? (first s))
(concat (flat (first s)) (flat (rest s)))
(cons (first s) (flat (rest s))))))
(fn [s] (apply str (filter #(not= (str %) (clojure.string/lower-case %)) s)))
#30 Compress a Sequence [Easy]
#(map first (partition-by identity %))
#(partition-by identity %)
#32 Duplicate a Sequence [Easy]
mapcat #(repeat 2 %)
#33 Replicate a Sequence [Easy]
(fn [coll times] (mapcat #(repeat times %) coll))
#(take (- %2 %1) (iterate inc %1))
#35 Local bindings [Elementary]
7
[x 7 y 3 z 1]
#37 Regular Expressions [Elementary]
"ABC"
#((fn max-iterative [coll current-max]
(if (empty? coll) current-max
(let [possible-new-max (first coll)]
(if (> possible-new-max current-max) (max-iterative (rest coll) possible-new-max)
(max-iterative (rest coll) current-max))))) %& 0)
#39 Interleave Two Seqs [Easy]
#(mapcat vector %1 %2)
(fn [separator coll] (drop-last (mapcat #(vector % separator) coll)))
#41 Drop Every Nth Item [Easy]
(fn [coll size] (mapcat #(take (dec size) %) (partition-all size coll)))
#(reduce * (range 1 (inc %)))
#43 Reverse Interleave [Medium]
#(apply map list (partition %2 %1))
(fn go [amount coll]
(if (< amount 0) (go (+ (count coll) amount) coll)
(if (= amount 0) coll
(go (dec amount) (reverse (into '() (conj (vec (drop 1 coll)) (first coll))))))))
'(1 4 7 10 13)
(fn [op] (fn [a b] (op b a)))
4
6
(fn [n coll] (list (vec (take n coll)) (vec (nthrest coll n))))
(fn [coll]
(vals (group-by type coll)))
#51 Advanced Destructuring [Easy]
[1 2 3 4 5]
#52 Intro to Destructuring [Elementary]
[c e]
#53 Longest Increasing Sub-Seq [Hard]
(fn [coll]
(letfn [(is-inc? [a e]
(or (empty? a)
(= (inc (last a)) e)))
(inner-loop [c acc max-acc]
(if (empty? c) (if (= (count max-acc) 1) [] ; because of [(first c)]
(if (= (last (drop-last 1 coll)) (last max-acc)) ; we miss the last one in the latest if (this is a hack)...
(conj max-acc (last coll))
max-acc))
(inner-loop (rest c)
(if (is-inc? acc (first c))
(conj acc (first c))
[(first c)])
(if (>= (count acc) (count max-acc))
acc
max-acc))))]
(inner-loop coll [] [])))
#54 Partition a Sequence [Medium]
(fn [x coll]
(loop [c coll
acc []]
(if (or (empty? c)
(< (count c) x))
acc
(recur (drop x c)
(conj acc (take x c))))))
#55 Count Occurrences [Medium]
(fn [coll]
(into {} (map (fn [[k v]] [k (count v)]) (group-by identity coll))))
#56 Find Distinct Items [Medium]
(fn [coll]
(let [step (fn step [xs seen]
(lazy-seq
((fn [[f :as xs] seen]
(when-let [s (seq xs)]
(if (contains? seen f)
(recur (rest s) seen)
(cons f (step (rest s) (conj seen f))))))
xs seen)))]
(step coll #{})))
#57 Simple Recursion [Elementary]
[5 4 3 2 1]
#58 Function Composition [Medium]
(fn [& fs]
(fn [& args]
(let [sf (reverse fs)]
(if (nth sf 2 nil)
((nth sf 2) ((second sf) (apply (first sf) args)))
((second sf) (apply (first sf) args))))))
(fn [& fs]
(fn [& args]
(mapv (fn [f] (apply f args)) fs)))
#60 Sequence Reductions [Medium]
(fn step
([f coll]
(step f (f (first coll)) (next coll)))
([f e coll]
(if (not-empty coll)
(lazy-seq (cons e
(step f
(f e (first coll))
(next coll))))
(list e))))
(fn [ks vs]
(apply hash-map (interleave ks vs)))
#62 Re-implement Iterate [Easy]
(fn step [f val]
(lazy-seq (cons val (step f (f val)))))
(fn step
([f coll] (step f coll {}))
([f coll m]
(if coll
(let [k (f (first coll))]
(step
f
(next coll)
(assoc m k (conj (get m k []) (first coll)))))
m)))
#64 Intro to Reduce [Elementary]
+
#65 Black Box Testing [Medium]
(fn [data]
(cond (or (= data {}) (:a data))
:map
(= (inc (count data)) (count (conj data :data :data :data)))
:set
(= (first (conj data 20 21)) 21)
:list
(= (last (conj data 20 21)) 21)
:vector))
#66 Greatest Common Divisor [Easy]
(fn gcd [a b]
(if (zero? b)
a
(gcd b (mod a b))))
(fn [n]
(take n
((fn primes [candidate]
(lazy-seq
(if (some #(zero? (mod candidate %)) (range 2 candidate))
(primes (inc candidate))
(cons candidate (primes (inc candidate))))))
2)))
#68 Recurring Theme [Elementary]
[7 6 5 4 3]
#69 Merge with a Function [Medium]
(fn [f & ms]
(let [keyvals (mapcat seq ms)]
(loop [m {}
coll (next keyvals)
[k v] (first keyvals)]
(if v
(recur (assoc m k (if-let [v2 (get m k)]
(f v2 v)
v))
(next coll)
(first coll))
m))))
(fn [sentence]
(sort-by #(clojure.string/lower-case %)
(-> (clojure.string/replace sentence #"[!.]" "")
(clojure.string/split #" "))))
#71 Rearranging Code: -> [Elementary]
last
#72 Rearranging Code: ->> [Elementary]
apply +
#73 Analyze a Tic-Tac-Toe Board [Hard]
(letfn [(transpose [m] (apply mapv vector m))]
(fn [board]
(cond (some #(= % [:x :x :x]) (concat board (transpose board)))
:x
(some #(= % [:o :o :o]) (concat board (transpose board)))
:o
(or (and (= (ffirst board) :x)
(= (second (second board)) :x)
(= (-> board (nth 2) (nth 2)) :x))
(and (= (first (last board)) :x)
(= (second (second board)) :x)
(= (last (first board)) :x)))
:x
(or (and (= (ffirst board) :o)
(= (second (second board)) :o)
(= (-> board (nth 2) (nth 2)) :o))
(and (= (first (last board)) :o)
(= (second (second board)) :o)
(= (last (first board)) :o)))
:o)))
#74 Filter Perfect Squares [Medium]
(fn [s]
(letfn [(perfect-square [n]
(let [root (Math/sqrt n)]
(= (Math/floor root) root)))]
(->> (clojure.string/split s #",")
(map #(Integer/valueOf %))
(filter perfect-square)
(interpose ",")
(apply str))))
#75 Euler's Totient Function [Medium]
(letfn [(gcd [a b]
(if (zero? b)
a
(gcd b (mod a b))))]
(fn [n]
(if (= n 1) 1
(->> (range 1 n)
(filter #(= (gcd n %) 1))
(count)))))
#76 Intro to Trampoline [Medium]
[1 3 5 7 9 11]
(fn [words]
(->> (group-by sort words)
(vals)
(filter #(> (count %) 1))
(map set)
(set)))
#78 Reimplement Trampoline [Medium]
(fn [f & args]
(loop [res (apply f args)]
(if (fn? res)
(recur (res))
res)))
#79 Triangle Minimal Path [Hard]
(fn [triangle]
(let [row (last triangle)]
(if (= (count row) 1)
(first row)
(recur (concat (drop-last 2 triangle)
[(mapv (fn [a [b c]] (min (+ a b) (+ a c)))
(last (drop-last triangle))
(partition 2 1 (last triangle)))])))))
(fn [x]
(letfn [(divisors [n] (filter #(integer? (/ n %)) (range 1 n)))
(perfect? [n] (= n (apply + (divisors n))))]
(perfect? x)))
(fn [s1 s2]
(letfn [(dups [c] (for [[n freq] (frequencies c)
:when (> freq 1)]
n))]
(set (dups (concat s1 s2)))))
TODO
TODO
TODO
TODO
TODO
TODO
#88 Symmetric Difference [Easy]
TODO
TODO
TODO
TODO
#92 Read Roman numerals [Hard]
TODO
#93 Partially Flatten a Sequence [Medium]
TODO
TODO
#95 To Tree, or not to Tree [Easy]
TODO
TODO
TODO
#98 Equivalence Classes [Medium]
TODO
TODO
#100 Least Common Multiple [Easy]
TODO
#101 Levenshtein Distance [Hard]
TODO
TODO
#103 Generating k-combinations [Medium]
TODO
#104 Write Roman Numerals [Medium]
TODO
#105 Identify keys and values [Medium]
TODO
TODO
TODO
TODO
TODO
#110 Sequence of pronunciations [Medium]
TODO
TODO
#112 Sequs Horribilis [Medium]
TODO
TODO
#114 Global take-while [Medium]
TODO
#115 The Balance of N [Medium]
TODO
TODO
TODO
TODO
#119 Win at Tic-Tac-Toe [Hard]
TODO
#120 Sum of square of digits [Easy]
TODO
#121 Universal Computation Engine [Medium]
TODO
#122 Read a binary number [Easy]
TODO
TODO
TODO
TODO
#126 Through the Looking Class [Easy]
TODO
TODO
#128 Recognize Playing Cards [Easy]
TODO
TODO
TODO
#131 Sum Some Set Subsets [Medium]
TODO
#132 Insert between two items [Medium]
TODO
TODO
#(and (contains? %2 %1) (nil? (%1 %2)))
TODO
TODO
#137 Digits and bases [Medium]
TODO
TODO
TODO
TODO
#141 Tricky card games [Medium]
TODO
TODO
TODO
(fn [val & fns]
(let [fns (cycle fns)]
(reductions (fn [acc f] (f acc)) val fns)))
TODO
TODO
#147 Pascal's Trapezoid [Easy]
TODO
TODO
TODO
#150 Palindromic Numbers [Medium]
TODO
TODO
#152 Latin Square Slicing [Hard]
TODO
#153 Pairwise Disjoint Sets [Easy]
TODO
TODO
TODO
#156 Map Defaults [Elementary]
TODO
#157 Indexing Sequences [Easy]
TODO
TODO
TODO
TODO