Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
15
Task/Roman-numerals-Decode/Clojure/roman-numerals-decode.clj
Normal file
15
Task/Roman-numerals-Decode/Clojure/roman-numerals-decode.clj
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
;; Incorporated some improvements from the alternative implementation below
|
||||
(defn ro2ar [r]
|
||||
(->> (reverse (.toUpperCase r))
|
||||
(map {\M 1000 \D 500 \C 100 \L 50 \X 10 \V 5 \I 1})
|
||||
(partition-by identity)
|
||||
(map (partial apply +))
|
||||
(reduce #(if (< %1 %2) (+ %1 %2) (- %1 %2)))))
|
||||
|
||||
;; alternative
|
||||
(def numerals { \I 1, \V 5, \X 10, \L 50, \C 100, \D 500, \M 1000})
|
||||
(defn from-roman [s]
|
||||
(->> s .toUpperCase
|
||||
(map numerals)
|
||||
(reduce (fn [[sum lastv] curr] [(+ sum curr (if (< lastv curr) (* -2 lastv) 0)) curr]) [0,0])
|
||||
first))
|
||||
Loading…
Add table
Add a link
Reference in a new issue