Last active
October 31, 2020 05:23
-
-
Save germ13/bb52e49e44463a3b842b9f1f1644d461 to your computer and use it in GitHub Desktop.
Purely Functional Solutions
This file contains hidden or 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
;; https://gist.github.com/ericnormand/b93b4f9cc9ab0bd5d396c9dac8bcfd7d | |
(defn digitize [n] | |
(map #(- (int %) 48) | |
(seq (str n)))) | |
(defn digit-search [digits] | |
(loop [coll digits | |
accumulator #{}] | |
(if (empty? coll) | |
nil | |
(if | |
(clojure.set/subset? #{0 1 2 3 4 5 6 7 8 9} | |
(clojure.set/union (set (digitize (first coll))) | |
accumulator)) | |
(first coll) | |
(recur (rest coll) | |
(clojure.set/union | |
(set (digitize (first coll))) |
This file contains hidden or 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
(defn rgbs [rgb] | |
(let [a (subs rgb 1 3) | |
b (subs rgb 3 5) | |
c (subs rgb 5)] | |
(map #(read-string (str "0x" %)) [a b c]))) | |
(defn mix[colors] | |
(let [[& color] (map rgbs colors)] | |
(str "#" (clojure.string/join "" | |
(map #(format "%X" %) | |
(map byte (map #(/ % (count colors)) | |
(apply map + color)))))))) |
This file contains hidden or 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
;; create a (count coll) x (count coll) matrix with all possible pair sums from collection | |
;; restrict upper right matrix, this will omit commutative pair sums | |
;; omit diagonal to omit reflexive sums. | |
;; filter only pairs that sum to target and sort | |
(defn sum-of-pairs [coll target] | |
(for [x (map-indexed vector coll) | |
y (map-indexed vector coll) | |
:when (and (< (first x) (first y)) | |
(= (+ (second x) (second y)) target))] | |
(vec (sort [(second x) (second y)])))) |
This file contains hidden or 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
;; https://www.codewars.com/kata/52f78966747862fc9a0009ae/train/clojure | |
(let [expr "5 1 2 + 4 * + 3 -"] | |
(let [k (clojure.string/split expr #" ")] | |
(map #(if (re-matches #"[\*\+\-/]" %) | |
(symbol %) | |
(Float/parseFloat %) | |
) k))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment