My solutions for 4clojure exercises.
List is not complete yet, because I'm still working on it.
Nothing but the Truth
(= 1 1)
Simple Math
4
Intro to Strings
"HELLO WORLD"
Intro to Lists
:a :b :c
Lists: conj
'(1 2 3 4)
Intro to Vectors
:a :b :c
Vectors: conj
[1 2 3 4]
Intro to Sets
#{:a :b :c :d}
Sets: conj
2
Intro to Maps
20
Maps: conj
[:b 2]
Intro to Sequences
3
Sequences: rest
'(20 30 40)
Intro to Functions
8
Double Down
#(* 2 %)
Hello World
#(str "Hello, " % "!")
Sequences: map
'(6 7 8)
Sequences: filter
'(6 7)
Last Element
#(first (reverse %))
Penultimate Element
#(second (reverse %))
Nth Element
#(get (vec %1) %2)
Count a Sequence
#(reduce (fn [acc x]
(inc acc))
0 %)
Reverse a Sequence
#(reduce (fn [acc x]
(cons x acc))
(empty %)
%)
Sum It All Up
#(apply + %)
Find the odd numbers
#(reverse (reduce (fn [acc x]
(if (odd? x)
(conj acc x)
acc))
'() %))
Fibonacci Sequence
#(loop [x [1 1]]
(if (= (count x) %)
x
(recur (conj x (apply + (take 2 (reverse x)))))))
Palindrome Detector
(if (string? %)
(= (clojure.string/reverse %) %)
(= (reverse %) %))
Flatten a Sequence
#(reverse (reduce
(fn rec-flatten [acc item]
(if (coll? item) (reduce rec-flatten acc item)
(conj acc item)))
'()
%))
Get the Caps
#(apply str(filter (set (map char (range 65 91))) %))
Compress a Sequence
#(map first (partition-by identity %))
Pack a Sequence
#(partition-by identity %)
Duplicate a Sequence
#(seq (reduce (fn [acc item]
(-> acc
(conj item)
(conj item)))
[] %))
Replicate a Sequence
#(mapcat (fn [item]
(take %2 (repeat item)))
%1)
Implement range
#(take (- %2 %1) (iterate inc %1))
Local bindings
7
Let it Be
[x 7 y 3 z 1]
Regular Expressions
"ABC"
Maximum value
(fn [& args]
(last (sort args)))
Interleave Two Seqs
#(flatten (map (fn [f s]
(conj '() s f))
%1 %2))
Interpose a Seq
#(take (- (* 2 (count %2)) 1)
(interleave %2 (repeat %1)))
Drop Every Nth Item
#(mapcat (fn [a]
(take (- %2 1) a))
(partition-all %2 %1))
Factorial Fun
#(apply * (range 1 (+ 1 %)))
Reverse Interleave
#(apply map (fn [& args]
args)
(partition %2 %1))
Rotate Sequence
#(let [new-coll (if (pos? %1)
%2
(reverse %2))
new-position (if (pos? %1)
%1
(+ %1 (* -2 %1)))
real-position (mod new-position (count %2))
rotated (flatten
(reduce
(fn [[acc-x acc-y] [x y]]
(if (< x real-position)
[acc-x (conj acc-y y)]
[(conj acc-x y) acc-y]))
[[] []]
(map-indexed vector new-coll)))]
(if (pos? %1)
rotated
(reverse rotated)))
Intro to Iterate
'(1 4 7 10 13)
Flipping out
#(fn [x y]
(% y x))
Contain Yourself
4
Intro to some
6
Split a sequence
#(vector (take %1 %2) (drop %1 %2))
Split by Type
#(vals (group-by type %))
Advanced Destructuring
[1 2 3 4 5]
Intro to some
[c e]
Longest Increasing Sub-Seq
(fn [coll]
(let [longest-seq (->> (reduce (fn [acc item]
(cond
(= (ffirst acc) nil) (conj (rest acc) (conj (first acc) item))
(= (inc (ffirst acc)) item) (conj (rest acc) (conj (first acc) item))
:else (conj acc (list item))))
'(())
coll)
(sort-by count)
last
reverse
vec)]
(if (> (count longest-seq) 1)
longest-seq
[])))
Partition a Sequence
(fn [par-num coll]
(->> (reduce (fn [acc item]
(if (= (count (first acc)) par-num)
(conj acc (list item))
(conj (rest acc) (conj (first acc) item))))
'(())
coll)
(filter #(= par-num (count %)))
(map reverse)
reverse))
Count Occurrences
#(into (sorted-map)
(map (fn [[k v]]
{k (count v)})
(group-by identity %)))
Find Distinct Items
(fn [coll]
(reduce (fn [acc item]
(if ((set acc) item)
acc
(conj acc item)))
[]
coll))
Simple Recursion
'(5 4 3 2 1)
Function Composition
(fn my-comp [& fns]
(fn [& args]
(if (= (count fns) 1)
(apply (first fns) args)
((apply my-comp (butlast fns))
(apply (last fns) args)))))
Juxtaposition
(fn [& fns]
(fn [& args]
(reduce #(conj %1 (apply %2 args)) [] fns)))
(fn problem60
([f coll]
(problem60 f (first coll) (rest coll)))
([f init coll]
(if (seq coll)
(cons init (lazy-seq (problem60 f (f init (first coll)) (rest coll))))
(list init))))
Map Construction
#(apply merge (map (fn [k v]
{k v})
%1
%2))
Re-implement Iterate
(fn my-iterate [fnc x]
(cons x (lazy-seq (my-iterate fnc (fnc x)))))
Group a Sequence
#(apply merge-with concat
(map (fn [item]
(hash-map (%1 item) [item]))
%2))
Intro to reduce
+
Black Box Testing
#(let [empty-coll (empty %)
coll-with-items (conj empty-coll [1 2] [1 2] [3 4])]
(if (= (count coll-with-items) 3)
(if (= (first (conj coll-with-items [5 6])) [5 6])
:list
:vector)
(if (contains? coll-with-items 1)
:map
:set))))
Greatest Common Divisor
#(loop [a (max %1 %2)
b (min %1 %2)
r (rem a b)]
(if (= r 0)
b
(recur b r (rem b r))))
(fn problem67
([count-nr] (problem67 count-nr 3 [2]))
([count-nr test-nr coll]
(if (= (count coll) count-nr)
coll
(if (some #(integer? (/ test-nr %)) coll)
(recur count-nr (inc test-nr) coll)
(recur count-nr (inc test-nr) (conj coll test-nr))))))
Recurring Theme
[7 6 5 4 3]
Merge with a function
(fn [f & args]
(into {} (map (fn [[k v]]
{k (if (> (count (vals v)) 1)
(apply f (vals v))
(first (vals v)))})
(->> args
(apply concat)
(group-by first)))))
Word Sortinf
(sort-by clojure.string/lower-case (-> s
(clojure.string/replace #"[.!?]" "")
(clojure.string/split #" "))))
Rearranging Code: ->
last
Rearranging Code: ->>
reduce +
Filter Perfect Squares
(fn problem74 [string-of-integers]
(let [lazy-prime (filter (fn [nr]
(not-any? #(zero? (mod nr %))
(range 2 (dec nr)))) (iterate inc 2))]
(letfn [(prime-factorization
([nr] (prime-factorization nr (take-while #(>= nr %) lazy-prime) []))
([nr primes prime-factors]
(let [prime-number (first primes)
quotient (/ nr prime-number)]
(if (= quotient 1)
(conj prime-factors prime-number)
(if (integer? quotient)
(recur quotient primes (conj prime-factors prime-number))
(recur nr (rest primes) prime-factors))))))
(is-perfect-square? [nr]
(not-any? (fn [[k v]]
(odd? v))
(frequencies (prime-factorization nr))))]
(clojure.string/join ","
(filter #(->> %
read-string
is-perfect-square?)
(clojure.string/split string-of-integers #","))))))
Set Intersection
#(apply sorted-set
(filter
(fn [x]
(contains? %1 x))
%2))
A Half-Truth
(fn [& args]
(and (contains? (set args) false)
(contains? (set args) true)))
Symmetric Difference
#(set (concat (apply disj %1 %2) (apply disj %2 %1)))
Cartesian Product
#(set
(for [x %1
y %2]
[x y]))
To Tree, or not to Tree
(fn tree [coll]
(let [inner-coll (first (filter coll? coll))
is-binary (odd? (count (filter #(and (not= % true) (not= % false)) (flatten coll))))]
(if (and is-binary (= (count inner-coll) 3))
(tree inner-coll)
is-binary)))
Pascal's Triangle
(fn pascal [row]
(map (comp last take)
(reverse (range 1 (+ 1 row)))
(take row (iterate (partial reductions +) (take row (repeat 1))))))
Product Digits
#(->> (* %1 %2)
str
seq
(map (comp read-string str)))
Least Common Multiple
(fn [& args]
(letfn [(gcd [x y]
(if (= x y)
x
(if (< x y)
(recur x (- y x))
(recur y (- x y)))))
(lcm [x y]
(/ (* x y) (gcd x y)))]
(reduce lcm args)))
Simple closures
#(fn [y]
(apply * (repeat % y)))
Re-implement Map
(fn my-map [f s]
(when (seq s)
(cons (f (first s))
(lazy-seq (my-map f (rest s))))))
Sum of square of digits
(fn [seq]
(-> (fn [item]
(if (> 10 item)
(< item (* item item))
(< item (apply + (map (comp #(* % %) read-string str first)
(split-at 1 (str item)))))))
(filter seq)
count))
Read a binary number
(fn [s]
(apply + (map (fn [x]
(apply * (repeat x 2)))
(keep-indexed #(if (= 1 %2) %1)
(mapv (comp read-string str) (reverse s))))))
Through the Looking Class
java.lang.Class
Recognize Playing Cards
#(let [cards-suits {"S" :spade
"H" :heart
"D" :diamond
"C" :club}
cards-rank (zipmap
(conj (->> (range 2 10)
(map str)
(vec))
"T" "J" "Q" "K" "A")
(range 13))
card-data (map str (seq %))
suit (cards-suits (first card-data))
rank (cards-rank (last card-data))]
{:suit suit :rank rank})
A nil key
(fn [k m]
(and (contains? m k)
(nil? (m k))))
Infix Calculator
(fn infix-calculator [& args]
(let [[first-operand operator second-operand] (take 3 args)
result (operator first-operand second-operand)]
(if (= (count args) 3)
result
(apply infix-calculator (cons result (drop 3 args))))))
dot product
#(apply + (map * %1 %2))
For the win
'(1 5 9 13 17 21 25 29 33 37)
Map Defaults
#(zipmap %2 (repeat %1))
Indexing Sequences
#(map-indexed (fn [itm idx]
[idx itm])%)
Subset and Superset
#{1 2}
Logical falsity and truth
1
Comparisons
#(cond
(%1 %2 %3) :lt
(%1 %3 %2) :gt
:else :eq)