Last active
October 24, 2021 08:43
-
-
Save seltzer1717/577b145548ec8160ac97e5964fec58d3 to your computer and use it in GitHub Desktop.
Solution to problem - https://www.youtube.com/watch?v=6NvNtYChIo8
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 shift | |
"Takes digit char, returns int offset from zero." | |
[nbr] | |
(- (int nbr) (int \0))) | |
(defn next-pair | |
"Reducing fn, takes acc, the destructured pair [chr nbr] | |
and returns next acc." | |
[acc [chr nbr]] | |
(conj acc chr (if nbr (char (+ (int chr) (shift nbr)))))) | |
(defn replace-digits | |
"Takes alpha-digit string, returns digit replaced string." | |
[s] | |
(apply str (reduce next-pair [] (partition-all 2 s)))) | |
;; For those who like brevity | |
(defn replace-digits [s] | |
(apply str | |
(reduce (fn [acc [chr nbr]] | |
(conj acc chr | |
(if nbr | |
(char (+ (int chr) | |
(- (int nbr) | |
(int \0))))))) | |
[] | |
(partition-all 2 s)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment