Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Une version Kotlin d'une solution vachement chouette au problème classique des nombres arabes exprimés avec des chiffres romains
val MAPPINGS = java.util.TreeMap(mapOf(1 to "I",
4 to "IV",
5 to "V",
9 to "IX",
10 to "X",
40 to "XL",
50 to "L",
90 to "XC",
100 to "C",
400 to "CD",
500 to "D",
900 to "CM",
1000 to "M"))
fun toRoman(number:Int, text:String = ""):String {
var entry = MAPPINGS.floorEntry(number)
return when(entry) {
null -> text + ""
else -> toRoman(number - entry.key, text + entry.value)
}
}
fun toRomanExplained(number:Int):String = String.format("%d = %s", number, toRoman(number))
fun main() {
for(index in 1..1000)
println(toRomanExplained(index))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment