Last active
November 14, 2016 14:20
-
-
Save speedcell4/ab3cfbf71aabe5b0a43a to your computer and use it in GitHub Desktop.
4clojure problem solutions
This file contains 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
; 1, Nothing but the Truth | |
true | |
; 2, Simple Math | |
4 | |
; 3, Intro to Strings | |
"HELLO WORLD" | |
; 4, Intro to Lists | |
:a :b :c | |
; 5, Lists: conj | |
'(1 2 3 4) | |
; 6, Intro to Vectors | |
:a :b :c | |
; 7, Vectors: conj | |
[1 2 3 4] | |
; 8, Intro to Sets | |
#{:a :b :c :d} | |
; 9, Sets: conj | |
2 | |
; 10, Intro to Maps | |
20 | |
; 11, Maps: conj | |
[:b 2] | |
; 12, Intro to Sequences | |
3 | |
; 13, Sequences: rest | |
[20 30 40] | |
; 14, Intro to Functions | |
8 | |
; 15, Double Down | |
* 2 | |
; 16, Hello World | |
format "Hello, %s!" | |
; 17, Sequences: map | |
'(6 7 8) | |
; 18, Sequences: filter | |
'(6 7) | |
; 19, Last Element | |
(comp first reverse) | |
; 20, Penultimate Element | |
(comp first rest reverse) | |
; 21, Nth Element | |
#(first (drop %2 %1)) | |
; 22, Count a Sequence | |
reduce (fn [z a] (inc z)) 0 | |
; 23, Reverse a Sequence | |
apply conj '() | |
; 24, Sum It All Up | |
apply + | |
; 25, Find the odd numbers | |
filter odd? | |
; 26, Fibonacci Sequence | |
#(map second (take %1 (iterate (fn [[a b]] [b (+ a b)]) [0 1]))) | |
; 27, Palindrome Detector | |
#(= (seq %1) (reverse %1)) | |
; 28, Flatten a Sequence | |
(fn flat [c] (if (coll? c) (mapcat flat c) [c])) | |
; 29, Get the Caps | |
#(reduce str (re-seq #"[A-Z]" %1)) | |
; 30, Compress a Sequence | |
#(map first (partition-by identity %1)) | |
; 31, Pack a Sequence | |
partition-by identity | |
; 32, Duplicate a Sequence | |
mapcat (fn [a] [a a]) | |
; 33, Replicate a Sequence | |
(fn [coll n] (mapcat #(repeat n %) coll)) | |
; 34, Implement range | |
#(take (- %2 %1) (iterate inc %1)) | |
; 35, Local bindings | |
7 | |
; 36, Let it Be | |
[x 7 y 3 z 1] | |
; 37, Regular Expressions | |
"ABC" | |
; 38, Maximum value | |
(fn [& more] (reduce #(if (> %1 %2) %1 %2) more)) | |
; 39, Interleave Two Seqs | |
mapcat (fn [x y] [x y]) | |
; 40, Interpose a Seq | |
#(rest (mapcat (fn [x] [%1 x]) %2)) | |
; 41, Drop Every Nth Item | |
#(mapcat (fn [c i] (if (zero? (rem i %2)) [] [c])) %1 (drop 1 (range))) | |
; 42, Factorial Fun | |
#(apply *' (range 1 (inc %1))) | |
; 43, Reverse Interleave | |
#(apply map vector (partition %2 %1)) | |
; 44, Rotate Sequence | |
#(let [n (mod %1 (count %2))] (concat (drop n %2) (take n %2))) | |
; 45, Intro to Iterate | |
'(1 4 7 10 13) | |
; 46, Flipping out | |
(fn [f] #(f %2 %1)) | |
; 47, Contain Yourself | |
4 | |
; 48, Intro to some | |
6 | |
; 49, Split a sequence | |
(fn [n c] [(subvec c 0 n) (subvec c n)]) | |
; 50, Split by Type | |
#(vals (group-by class %)) | |
; 51, Advanced Destructuring | |
[1 2 3 4 5] | |
; 52, Intro to Destructuring | |
[c e] | |
; 53, Longest Increasing Sub-Seq | |
(fn [[x & xs]] | |
(let [ans (reverse (reduce (partial max-key count) | |
(reduce (fn [[[y & ys] & zs] x] | |
(if (< y x) | |
(conj zs (conj ys y x)) | |
(conj zs (conj ys y) [x]))) [[x]] xs)))] | |
(if (> (count ans) 1) ans []))) | |
; 54, Partition a Sequence | |
(fn f [n coll] (if (>= (count coll) n) (cons (take n coll) (f n (drop n coll))))) | |
; 55, Count Occurrences | |
#(reduce (fn [r [k v]] (conj r [k (count v)])) {} (group-by identity %)) | |
; 56, Find Distinct Items | |
reduce (fn [s e] (if (some #(= % e) s) s (conj s e))) [] | |
; 57, Simple Recursion | |
'(5 4 3 2 1) | |
; 58, Function Composition | |
(fn [& fs] | |
(fn [& as] | |
(first (reduce #(vector (apply %2 %1)) as (reverse fs))))) | |
; 59, Juxtaposition | |
(fn [& fs] | |
(fn [& x] | |
(map #(apply % x) fs))) | |
; 60, Sequence Reductions | |
(fn r | |
([f [a & as]] (r f a as)) | |
([f z [a & as]] (lazy-cat [z] (if (empty? as) [(f z a)] (r f (f z a) as))))) | |
; 61, Map Construction | |
#(into {} (map vector %1 %2)) | |
; 62, Re-implement Iterate | |
(fn r [f x] (lazy-cat [x] (r f (f x)))) | |
; 63, Group a Sequence | |
(fn [f s] (apply merge-with concat (map #(hash-map (f %) [%]) s))) | |
; 64, Intro to Reduce | |
+ | |
; 65, Black Box Testing | |
#(if (reversible? %) :vector ({{} :map, #{} :set, '() :list} (empty %))) | |
; 66, Greatest Common Divisor | |
(fn [a b] (if (zero? b) a (recur b (rem a b)))) | |
; 67, Prime Numbers | |
(fn [n] | |
(take n ((fn p [[a & as]] | |
(lazy-cat [a] (p (filter #(pos? (rem % a)) as)))) (iterate inc 2)))) | |
; 68, Recurring Theme | |
[7 6 5 4 3] | |
; 69, Merge with a Function | |
(fn [f & ms] | |
(apply hash-map (mapcat (fn [[k v]] [k (reduce f (vals v))]) (group-by key (apply concat ms))))) | |
; 70, Word Sorting | |
#(sort-by clojure.string/lower-case (re-seq #"\w+" %)) | |
; 71, Rearranging Code: -> | |
last | |
; 72, Rearranging Code: ->> | |
reduce + | |
; 73, Analyze a Tic-Tac-Toe Board | |
(fn [b] | |
(letfn [(w [[[a b c] | |
[d e f] | |
[g h i]] p] (or (= p a b c) | |
(= p d e f) | |
(= p g h i) | |
(= p a d g) | |
(= p b e h) | |
(= p c f i) | |
(= p a e i) | |
(= p c e g)))] | |
(cond (w b :x) :x | |
(w b :o) :o))) | |
; 74, Filter Perfect Squares | |
(fn [s] | |
(clojure.string/join "," (filter (set (for [x (range 1 10)] (* x x))) (map #(Integer/valueOf %) (re-seq #"\d+" s))))) | |
; 75, Euler's Totient Function | |
(fn [n] (count (filter #(= 1 (.gcd (biginteger %) (biginteger n))) (range 1 (inc n))))) | |
; 76, Intro to Trampoline | |
(range 1 12 2) | |
; 77, Anagram Finder | |
#(set (filter (fn [s] (> (count s) 1)) (map set (vals (group-by sort %))))) | |
; 78, Reimplement Trampoline | |
(fn [f v] | |
(loop [r (f v)] | |
(if (fn? r) (recur (r)) r))) | |
; 79, Triangle Minimal Path | |
(fn m [[[r] & t]] | |
(if (nil? r) 0 | |
(+ r (min | |
(m (map rest t)) | |
(m (map butlast t)))))) | |
; 80, Perfect Numbers | |
#(= % (apply + (filter (fn [i] (zero? (rem % i))) (range 1 %)))) | |
; 81, Set Intersection | |
(comp set filter) | |
; 82, Word Chains | |
; 83, A Half-Truth | |
not= | |
; 147, Pascal's Trapezoid | |
iterate #(map +' (concat [0] %) (concat % [0])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment