Last active
November 20, 2023 14:24
-
-
Save maxost/2aeb9540d4e4275cc0caf2325de485f3 to your computer and use it in GitHub Desktop.
Kotlin: Russian word generator for counts
This file contains hidden or 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
/** | |
* genitivePlural - родительный падеж, множественное число (примеры: "часов", "минут", "секунд") | |
* nominativeSingular - именительный падеж, единственное число (примеры: "час", "минута", "секунда") | |
* genitiveSingular - родительный падеж, единственное число (примеры: "часа", "минуты", "секунды") | |
*/ | |
fun getCountNoun(count: Int, genitivePlural: String, nominativeSingular: String, genitiveSingular: String): String { | |
val lastDigit = count.toString().last().toString().toInt() | |
val word = if (count in 5..20) genitivePlural | |
else if (lastDigit == 0 || lastDigit >= 5) genitivePlural | |
else if (lastDigit == 1) nominativeSingular | |
else if (lastDigit in 2..4) genitiveSingular | |
else throw IllegalArgumentException("incorrect input") | |
return "$count $word" | |
} | |
fun getOrgCountString(count: Int) = getCountNoun(count, "организаций", "организация", "организации") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment