Last active
March 17, 2022 15:22
-
-
Save brettrowberry/1813d497a833b09b4e6b8f0959b8e5b1 to your computer and use it in GitHub Desktop.
Function to detect isograms.
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 isogram? | |
"An isogram is a word in which no letters occur more than once. | |
Empty string is considered an isogram in this implementation." | |
[^String s] | |
(boolean | |
(when (string? s) | |
(loop [letters (sort (seq s))] | |
(let [[current next & _] letters] | |
(cond | |
(nil? next) true | |
(= current next) false | |
:else (recur (rest letters)))))))) | |
(assert | |
(every? isogram? ["" | |
" " | |
"c" | |
"ca" | |
"cat" | |
"cats"])) | |
(assert | |
(not-any? isogram? [nil | |
"cc" | |
"cct" | |
"caa" | |
"catt"])) | |
;; TODO solve in another way or two | |
;; Can isograms have only 1, 2 or more? | |
```clojure | |
#(= (count (set (vals (frequencies %)))) 1 ) | |
#(= (count %) (count (into #{} %)) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment