Skip to content

Instantly share code, notes, and snippets.

@maxost
Last active November 20, 2023 14:24
Show Gist options
  • Save maxost/2aeb9540d4e4275cc0caf2325de485f3 to your computer and use it in GitHub Desktop.
Save maxost/2aeb9540d4e4275cc0caf2325de485f3 to your computer and use it in GitHub Desktop.
Kotlin: Russian word generator for counts
/**
* 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