Skip to content

Instantly share code, notes, and snippets.

@marlonjames71
Last active July 17, 2020 15:55
Show Gist options
  • Save marlonjames71/0b2948093c00d262d32f5bf9c5234a12 to your computer and use it in GitHub Desktop.
Save marlonjames71/0b2948093c00d262d32f5bf9c5234a12 to your computer and use it in GitHub Desktop.
Roman Numerals
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