Created
April 29, 2020 22:23
-
-
Save jackcallister/08c813ae51a4b5b6f68445ffe085a992 to your computer and use it in GitHub Desktop.
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 map-every-nth [f coll n] | |
(map-indexed #(if (zero? (mod (inc %1) n)) (f %2) %2) coll)) | |
(defn convert-to-number-seq [num-string] | |
(map #(Integer/parseInt %) (re-seq #"\d" num-string))) | |
(defn double-every-second-digit [coll] | |
(map-every-nth #(* 2 %) (reverse coll) 2)) | |
(defn subtract-high-digits [coll] | |
(map-every-nth #(if (> % 9) (- % 9) %) coll 2)) | |
(defn sum [coll] | |
(reduce + coll)) | |
(defn divisible-by-10? [n] | |
(= (mod n 10) 0)) | |
(defn invalid-chars? [num-string] | |
(not (empty? (re-find #"[^\d.\s]" num-string)))) | |
(defn invalid-count? [num-string] | |
(<= (count (convert-to-number-seq num-string)) 1)) | |
(defn invalid? [num-string] | |
(or (invalid-chars? num-string) | |
(invalid-count? num-string))) | |
(defn valid? [num-string] | |
(if (invalid? num-string) false | |
(-> num-string | |
convert-to-number-seq | |
double-every-second-digit | |
subtract-high-digits | |
sum | |
divisible-by-10?))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment