Kotlin collections


Download 13.85 Kb.
Sana23.04.2023
Hajmi13.85 Kb.
#1392842
Bog'liq
3.1 mashinali o\'qitishga kirish


import kotlin.collections.ArrayList
import
kotlin.random.Random

fun main() {

val list = ArrayList()

for (i in 0 until 10){
list.add(Random.nextInt(2, 100))
}
println()
for (num in list){
println(num)
}

val secondMax = secondMaxNumber(list)
val textNumber = numberToTextNumber(secondMax)
println()
println(secondMax)
println()
println("Result: $textNumber")
}

fun secondMaxNumber(list : ArrayList) : Int{
var max = Int.MIN_VALUE
var secondMax = Int.MIN_VALUE

for (num in list){
if(num>max){
secondMax = max
max
= num
}else if(num > secondMax){
secondMax = num
}
}

return secondMax
}

fun numberToTextNumber(a : Int) : String{
val unitNumbersOfText = arrayListOf("bir", "ikki", "uch", "to'rt", "besh", "olti", "yetti", "sakkiz", "to'qqiz")
val tensNumbersOfText = arrayListOf("o'n", "yigirma", "o'ttiz", "qirq", "ellik", "oltmish", "yetmish", "sakson", "to'qson", "bir yuz")

return when(a.toString().length){
1 -> {
unitNumbersOfText[a-1]
}
2 -> {
val tenNumber = a / 10
val unitNumber = a % 10

"${tensNumbersOfText[tenNumber-1]} ${unitNumbersOfText[unitNumber-1]}"


}
else -> {
""
}
}

}

Download 13.85 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling