Skip to content

Instantly share code, notes, and snippets.

View chrisvest's full-sized avatar
🍉

Chris Vest chrisvest

🍉
View GitHub Profile
;; chrisvest's solution to http://4clojure.com/problem/80
#(let [ntz (Integer/numberOfTrailingZeros %1)
nlz (Integer/numberOfLeadingZeros %1)]
(= ntz (- 31 ntz nlz)))
;; chrisvest's solution to http://4clojure.com/problem/77
#(->> % (group-by sort) vals (map set) (filter second) set)
;; chrisvest's solution to http://4clojure.com/problem/78
(fn [f & args]
(loop [f? (apply f args)]
(if (fn? f?) (recur (f?)) f?)))
;; 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})))
;; 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))))
;; 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
;; 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)))))))
;; 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))
;; 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
;; chrisvest's solution to http://4clojure.com/problem/58
#(reduce
(fn [f g]
(fn [& a] (f (apply g a))))
%&)