Created
May 5, 2020 07:17
-
-
Save harmenjanssen/3fbdeff221777f43697c389b0f1e4d21 to your computer and use it in GitHub Desktop.
nearest-vowels Clojure kata
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
(def vowels "aeiou") | |
(defn is-vowel [ch] (not (nil? (some #{ch} vowels)))) | |
(defn abs [n] (max n (- n))) | |
(defn distance | |
"Returns the distance from the number x to the closest number in xs" | |
[xs x] | |
(apply min (map (fn [y] (abs (- y x))) xs))) | |
(defn nearest-vowels [string] | |
(let [vowel-indices (->> string | |
(map-indexed vector) | |
(filter #(is-vowel (second %))) | |
(map first))] | |
(if (not-empty vowel-indices) | |
(map-indexed (fn [index ch] (distance vowel-indices index)) string) | |
(map #{nil} string)))) | |
(nearest-vowels "babbba") ;; (1 0 1 2 1 0) | |
(nearest-vowels "hjkl") ;; (nil nil nil nil) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment