Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,36 @@
|
|||
val romanNumerals = mapOf(
|
||||
1000 to "M",
|
||||
900 to "CM",
|
||||
500 to "D",
|
||||
400 to "CD",
|
||||
100 to "C",
|
||||
90 to "XC",
|
||||
50 to "L",
|
||||
40 to "XL",
|
||||
10 to "X",
|
||||
9 to "IX",
|
||||
5 to "V",
|
||||
4 to "IV",
|
||||
1 to "I"
|
||||
)
|
||||
|
||||
fun encode(number: Int): String? {
|
||||
if (number > 5000 || number < 1) {
|
||||
return null
|
||||
}
|
||||
var num = number
|
||||
var result = ""
|
||||
for ((multiple, numeral) in romanNumerals.entries) {
|
||||
while (num >= multiple) {
|
||||
num -= multiple
|
||||
result += numeral
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println(encode(1990))
|
||||
println(encode(1666))
|
||||
println(encode(2008))
|
||||
}
|
||||
|
|
@ -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