June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,19 @@
|
|||
fun Int.toRomanNumeral(): String {
|
||||
fun digit(k: Int, unit: String, five: String, ten: String): String {
|
||||
return when (k) {
|
||||
in 1..3 -> unit.repeat(k)
|
||||
4 -> unit + five
|
||||
in 5..8 -> five + unit.repeat(k - 5)
|
||||
9 -> unit + ten
|
||||
else -> throw IllegalArgumentException("$k not in range 1..9")
|
||||
}
|
||||
}
|
||||
return when (this) {
|
||||
0 -> ""
|
||||
in 1..9 -> digit(this, "I", "V", "X")
|
||||
in 10..99 -> digit(this / 10, "X", "L", "C") + (this % 10).toRomanNumeral()
|
||||
in 100..999 -> digit(this / 100, "C", "D", "M") + (this % 100).toRomanNumeral()
|
||||
in 1000..3999 -> "M" + (this - 1000).toRomanNumeral()
|
||||
else -> throw IllegalArgumentException("${this} not in range 0..3999")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue