Skip to content

Instantly share code, notes, and snippets.

@dandorman
Created August 18, 2017 19:01
Show Gist options
  • Save dandorman/d0516cd4f73b76231408e3132627f4cf to your computer and use it in GitHub Desktop.
Save dandorman/d0516cd4f73b76231408e3132627f4cf to your computer and use it in GitHub Desktop.
A little Clojure(Script) solution for a weird little problem.
#!/usr/bin/env planck
(def vowel? #{\A \a \E \e \I \i \O \o \U \u})
(defn reverse-vowels
"Takes in a string and reverses all the vowels in the string."
[in]
(let [idx->letter (zipmap (range) in)
idx->vowel (filter (fn [[_ letter]] (vowel? letter)) idx->letter)
[idxes vowels] ((juxt keys vals) idx->vowel)
ridx->vowel (zipmap (reverse idxes) vowels)]
(apply
str
(reduce (fn [out [idx letter]] (assoc out idx letter))
(into [] (range (count in)))
(merge idx->letter ridx->vowel)))))
(println (reverse-vowels "I love burritos"))
;; => o livu berrotIs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment