Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/Soundex/Clojure/soundex-1.clj
Normal file
20
Task/Soundex/Clojure/soundex-1.clj
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
(defn get-code [c]
|
||||
(case c
|
||||
(\B \F \P \V) 1
|
||||
(\C \G \J \K
|
||||
\Q \S \X \Z) 2
|
||||
(\D \T) 3
|
||||
\L 4
|
||||
(\M \N) 5
|
||||
\R 6
|
||||
nil)) ;(\A \E \I \O \U \H \W \Y)
|
||||
|
||||
(defn soundex [s]
|
||||
(let [[f & s] (.toUpperCase s)]
|
||||
(-> (map get-code s)
|
||||
distinct
|
||||
(concat , "0000")
|
||||
(->> (cons f ,)
|
||||
(remove nil? ,)
|
||||
(take 4 ,)
|
||||
(apply str ,)))))
|
||||
27
Task/Soundex/Clojure/soundex-2.clj
Normal file
27
Task/Soundex/Clojure/soundex-2.clj
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
;;; With proper consecutive duplicates elimination
|
||||
|
||||
(defn get-code [c]
|
||||
(case c
|
||||
(\B \F \P \V) 1
|
||||
(\C \G \J \K
|
||||
\Q \S \X \Z) 2
|
||||
(\D \T) 3
|
||||
\L 4
|
||||
(\M \N) 5
|
||||
\R 6
|
||||
nil)) ;(\A \E \I \O \U \H \W \Y)
|
||||
|
||||
(defn reduce-fn [acc nxt]
|
||||
(let [next-code (get-code nxt)]
|
||||
(if (and (not= next-code (last acc))
|
||||
(not (nil? next-code)))
|
||||
(conj acc next-code)
|
||||
acc)))
|
||||
|
||||
(defn soundex [the-word]
|
||||
(let [[first-char & the-rest] (.toUpperCase the-word)
|
||||
next-code (get-code (first the-rest))]
|
||||
(if (nil? next-code)
|
||||
(recur (apply str first-char (rest the-rest)))
|
||||
(let [soundex-nums (reduce reduce-fn [] the-rest)]
|
||||
(apply str first-char (take 3 (conj soundex-nums 0 0 0)))))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue