Skip to content

Instantly share code, notes, and snippets.

;; ariarule's solution to Sequence of pronunciations
;; https://4clojure.com/problem/110
(fn pro-seq [start]
(letfn [(pronounce [vn]
(let [z (partition-by identity vn)]
(interleave (map count z) (map first z))))]
(iterate pronounce (pronounce start))))
;; ariarule's solution to Partially Flatten a Sequence
;; https://4clojure.com/problem/93
(fn part-flatten [s]
(cond
(not (coll? s)) s
(some coll? s) (mapcat part-flatten s)
:else [s]))
;; ariarule's solution to The Balance of N
;; https://4clojure.com/problem/115
(fn balanced? [n]
(let [n-str (str n)
n-half-count (int (/ (count n-str) 2))]
(apply = (map (fn [l] (reduce #(+ %1 (Integer/parseInt (str %2))) 0 l))
(list (take n-half-count n-str)
(take-last n-half-count n-str))))))
;; ariarule's solution to Equivalence Classes
;; https://4clojure.com/problem/98
(fn equiv [f s]
(set (map set (partition-by f (sort-by f s)))))
;; ariarule's solution to Power Set
;; https://4clojure.com/problem/85
(fn power-set
([s]
(let [units (set (map set (partition-all 1 s)))]
(power-set (map first units) units 2 (count s))))
([units prev-subsets current-size target-size]
(if (> current-size target-size)
(conj prev-subsets #{})
;; ariarule's solution to Power Set
;; https://4clojure.com/problem/85
(fn power-set
([s]
(let [units (set (map set (partition-all 1 s)))]
(power-set (map first units) units 2 (count s))))
([units prev-subsets current-size target-size]
(if (> current-size target-size)
(conj prev-subsets #{})
;; ariarule's solution to Identify keys and values
;; https://4clojure.com/problem/105
(fn keys-and-values [x]
(let [cx (mapcat
#(if (keyword? (first %))
(interpose
'()
(partition-all 1 %))
(list %))
;; ariarule's solution to Euler's Totient Function
;; https://4clojure.com/problem/75
(fn totient [n]
(if (= n 1)
1
(count
(filter
#(= (denominator %) n)
(map #(/ % n) (range 1 n))))))
;; ariarule's solution to Sequence Reductions
;; https://4clojure.com/problem/60
(fn my-reductions
([f c]
(my-reductions f (first c) (drop 1 c)))
([f i c]
(lazy-seq
(cons
i
;; ariarule's solution to Happy numbers
;; https://4clojure.com/problem/86
(fn [prev-nums n]
(let [next-n (apply + (map #(int (Math/pow (Integer/parseInt (str %)) 2)) (vec (str n))))]
(if (some #(= next-n %) prev-nums)
false
(if (= 1 next-n)
true
(recur (conj prev-nums n) next-n))))) #{}