Last active
September 3, 2019 05:47
-
-
Save SchlenkR/fad577f364f3e8401840eff771475a54 to your computer and use it in GitHub Desktop.
Funky Roman
This file contains 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
from functools import reduce | |
def convertToRoman(num): | |
roman_factors = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I') ] | |
return reduce(lambda state, factor: (state[0] % factor[0], state[1] + factor[1] * (state[0] // factor[0])), [(num, '')] + roman_factors)[1] | |
print(convertToRoman(2049)) |
This file contains 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
let toRoman num = | |
[ | |
num, "" | |
1000, "M" | |
900, "CM" | |
500, "D" | |
400, "CD" | |
100, "C" | |
90, "XC" | |
50, "L" | |
40, "XL" | |
10, "X" | |
9, "IX" | |
5, "V" | |
4, "IV" | |
1, "I" | |
] | |
|> List.reduce(fun (accMod,currentRes) (arabic,roman) -> | |
accMod % arabic, | |
currentRes + (String.replicate (accMod / arabic) roman)) | |
|> fun (_,res) -> res | |
toRoman 2049 |
This file contains 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
let toRoman num = | |
let romanFactors = [ | |
1000, "M" | |
900, "CM" | |
500, "D" | |
400, "CD" | |
100, "C" | |
90, "XC" | |
50, "L" | |
40, "XL" | |
10, "X" | |
9, "IX" | |
5, "V" | |
4, "IV" | |
1, "I" | |
] | |
let mutable accMod = num | |
let mutable currentRes = "" | |
for (arabic,roman) in romanFactors do | |
currentRes <- currentRes + (String.replicate (accMod / arabic) roman) | |
accMod <- accMod % arabic | |
currentRes | |
toRoman 2049 |
This file contains 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
type Semi = One of value : int | Many of last : int * result : int list | |
let moduloSemi a b = | |
match a,b with | |
| Many (x,res), One s -> let c = x % s in Many(c, res @ [c]) | |
| One s1, One s2 -> let c = s1 % s2 in Many(c, [c]) | |
let result (Many (_,res)) = res | |
let toRoman num = | |
// small helper for list ans string | |
let exceptLast (l: _ list) = l.[.. l.Length - 2] | |
let ( ** ) = String.replicate | |
let factors = [ | |
1000, "M" | |
900, "CM" | |
500, "D" | |
400, "CD" | |
100, "C" | |
90, "XC" | |
50, "L" | |
40, "XL" | |
10, "X" | |
9, "IX" | |
5, "V" | |
4, "IV" | |
1, "I" | |
] | |
factors | |
|> List.map (fun (a,_) -> a) | |
|> List.append [num] | |
|> List.map One | |
|> List.reduce moduloSemi | |
|> (result >> exceptLast) | |
|> List.append [num] | |
|> List.zip factors | |
|> List.map (fun ((arab,roman),accMod) -> (accMod / arab) ** roman) | |
|> List.reduce (+) | |
toRoman 2049 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment