Last active
July 17, 2020 15:55
-
-
Save marlonjames71/0b2948093c00d262d32f5bf9c5234a12 to your computer and use it in GitHub Desktop.
Roman Numerals
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
func doAsTheRomansDoAndNumeralize(number: Int) -> String { | |
let numberRepresentation = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] | |
let numerals = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"] | |
var result = "" | |
var number = number | |
while number > 0 { | |
for (index, num) in numberRepresentation.enumerated() { | |
if number - num >= 0 { | |
number -= num | |
result += numerals[index] | |
break | |
} | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment