Skip to content

Instantly share code, notes, and snippets.

@Riduidel
Created May 23, 2019 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Riduidel/36368eabf59f8cfb7d415dbe772d434a to your computer and use it in GitHub Desktop.
Save Riduidel/36368eabf59f8cfb7d415dbe772d434a to your computer and use it in GitHub Desktop.
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