Skip to content

Instantly share code, notes, and snippets.

@vicxruiz
Created June 5, 2019 15:59
Show Gist options
  • Save vicxruiz/7d51a7ea0651d0172ca5da0f042ed26e to your computer and use it in GitHub Desktop.
Save vicxruiz/7d51a7ea0651d0172ca5da0f042ed26e to your computer and use it in GitHub Desktop.
func doAsTheRomansDoAndNumeralize(elementInt: Int) -> String {
var element = elementInt
var romanNumeralString: String = ""
if element >= 1000 {
//divide element by 1000 till it's less than 1000
let result = element / 1000
if result >= 1 {
element = element % 1000
romanNumeralString += "M"
doAsTheRomansDoAndNumeralize(elementInt: element)
}
}
if element >= 900 {
let result = element / 900
if result >= 1 {
element = element % 900
romanNumeralString += "CM"
doAsTheRomansDoAndNumeralize(elementInt: element)
}
}
if element >= 500 {
let result = element / 500
if result >= 1 {
element = element % 500
romanNumeralString += "D"
doAsTheRomansDoAndNumeralize(elementInt: element)
}
}
if element >= 400 {
let result = element / 400
if result >= 1 {
element = element % 400
romanNumeralString += "CD"
doAsTheRomansDoAndNumeralize(elementInt: element)
}
}
if element >= 100 {
let result = element / 100
if result >= 1 {
element = element % 100
romanNumeralString += "C"
doAsTheRomansDoAndNumeralize(elementInt: element)
}
}
if element >= 90 {
let result = element / 90
if result >= 1 {
element = element % 90
romanNumeralString += "XC"
doAsTheRomansDoAndNumeralize(elementInt: element)
}
}
if element >= 50 {
let result = element / 50
if result >= 1 {
element = element % 50
romanNumeralString += "L"
doAsTheRomansDoAndNumeralize(elementInt: element)
}
}
if element >= 40 {
let result = element / 40
if result >= 1 {
element = element % 40
romanNumeralString += "XL"
doAsTheRomansDoAndNumeralize(elementInt: element)
}
}
if element >= 10 {
print("element: \(element)")
let result = element / 10
print("result: \(result)")
if result >= 1 {
print("element: \(element)")
element = element % 10
print("element after divisible: \(element)")
romanNumeralString += "X"
doAsTheRomansDoAndNumeralize(elementInt: element)
}
}
if element >= 9 {
let result = element / 9
if result >= 1 {
element = element % 9
romanNumeralString += "IX"
doAsTheRomansDoAndNumeralize(elementInt: element)
}
}
if element >= 5 {
let result = element / 5
if result >= 1 {
element = element % 5
romanNumeralString += "V"
doAsTheRomansDoAndNumeralize(elementInt: element)
}
}
if element >= 4 {
let result = element / 4
if result >= 1 {
element = element % 4
romanNumeralString += "IV"
doAsTheRomansDoAndNumeralize(elementInt: element)
}
}
return romanNumeralString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment