feat(haskell): add roman numerals decoder kata
This commit is contained in:
parent
2a6c53e075
commit
8fa41c4e4c
2 changed files with 24 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
|||
module Codewars.Kata.SplitStrings where
|
||||
module Codewars.Kata.SplitStrings (solution) where
|
||||
|
||||
import qualified Data.Char as Char
|
||||
|
||||
|
|
23
haskell/11_roman_numerals_decoder.hs
Normal file
23
haskell/11_roman_numerals_decoder.hs
Normal file
|
@ -0,0 +1,23 @@
|
|||
{-# LANGUAGE NumericUnderscores #-}
|
||||
module Roman (solution) where
|
||||
|
||||
solution :: String -> Int
|
||||
solution = sum . mergeParts . map fromRoman
|
||||
|
||||
mergeParts :: [Int] -> [Int]
|
||||
mergeParts [] = []
|
||||
mergeParts [n1] = [n1]
|
||||
mergeParts (n1:n2:rest)
|
||||
| n1 < n2 = n2 - n1 : mergeParts rest
|
||||
| otherwise = n1 : mergeParts (n2:rest)
|
||||
|
||||
fromRoman :: Char -> Int
|
||||
fromRoman c = case c of
|
||||
'I' -> 1
|
||||
'V' -> 5
|
||||
'X' -> 10
|
||||
'L' -> 50
|
||||
'C' -> 100
|
||||
'D' -> 500
|
||||
'M' -> 1_000
|
||||
_ -> 0
|
Loading…
Reference in a new issue