Created
May 23, 2019 10:02
-
-
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
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
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