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
;; chrisvest's solution to http://4clojure.com/problem/80 | |
#(let [ntz (Integer/numberOfTrailingZeros %1) | |
nlz (Integer/numberOfLeadingZeros %1)] | |
(= ntz (- 31 ntz nlz))) |
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
;; chrisvest's solution to http://4clojure.com/problem/77 | |
#(->> % (group-by sort) vals (map set) (filter second) set) |
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
;; chrisvest's solution to http://4clojure.com/problem/78 | |
(fn [f & args] | |
(loop [f? (apply f args)] | |
(if (fn? f?) (recur (f?)) f?))) |
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
;; chrisvest's solution to http://4clojure.com/problem/73 | |
(fn [m] | |
(let [transpose (partial apply map list) | |
star (fn [[[a b c] [d e f] [g h i]]] [[a e i] [c e g]]) | |
permute (juxt identity transpose star) | |
fitness #(->> % (filter (fn [x] (apply = x))) first)] | |
(->> (permute m) (map fitness) (some first) #{:x :o}))) |
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
;; chrisvest's solution to http://4clojure.com/problem/65 | |
(fn [coll] | |
(let [x [:a :b] | |
y [:c :d]] | |
(if (= (count (conj coll x x)) (count (conj coll x))) | |
(if (= ((conj coll x) :a) :b) :map :set) | |
(if (= (first (conj coll x y)) y) :list :vector)))) |
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
;; chrisvest's solution to http://4clojure.com/problem/75 | |
(fn [x] (if (= x 1) 1 | |
(let [coprime (fn [y] (= 1 | |
(loop [dividend (max x y) ;; Euclid's GCD algorithm | |
divisor (min x y) | |
quotient (quot dividend divisor) | |
remainder (rem dividend divisor)] | |
(if (zero? remainder) | |
divisor |
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
;; chrisvest's solution to http://4clojure.com/problem/60 | |
(fn rdx | |
([f [init & coll]] | |
(rdx f init coll)) | |
([f init coll] | |
(lazy-seq (if (empty? coll) (cons init nil) | |
(cons init (rdx f (f init (first coll)) (rest coll))))))) |
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
;; chrisvest's solution to http://4clojure.com/problem/63 | |
(fn [f coll] | |
(reduce (fn [m x] | |
(let [k (f x)] | |
(update-in m [k] #(conj (or % []) x)))) | |
{} coll)) |
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
;; chrisvest's solution to http://4clojure.com/problem/66 | |
;; Using Euclid's algorithm | |
(fn gcd [a b] | |
(loop [dividend (max a b) | |
divisor (min a b) | |
quotient (quot dividend divisor) | |
remainder (rem dividend divisor)] | |
(if (zero? remainder) | |
divisor |
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
;; chrisvest's solution to http://4clojure.com/problem/58 | |
#(reduce | |
(fn [f g] | |
(fn [& a] (f (apply g a)))) | |
%&) |