fcamuso

Kotlin video 29

Aug 31st, 2025
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.59 KB | None | 0 0
  1. import java.io.File
  2. import java.io.FileWriter
  3. import java.io.InvalidObjectException
  4. import javax.management.ImmutableDescriptor
  5. import kotlin.random.Random
  6.  
  7. //funzioni lambda
  8.  
  9. //fun greaterThanLength(s1: String, s2: String): Boolean = s1.length>s2.length
  10.  
  11. //fun contaVocali(s: String) : Int {
  12. //    var cont = 0
  13. //    for (c in s) if (c in "aAeEiIoIuU") cont++
  14. //
  15. //    return cont
  16. //}
  17. //
  18. //fun haPiuVocali(s1: String, s2: String): Boolean = contaVocali(s1) > contaVocali(s2)
  19.  
  20. fun String.contaVocali() : Int {
  21.     var cont = 0
  22.     for (c in this) if (c in "aAeEiIoIuU") cont++
  23.  
  24.     return cont
  25. }
  26.  
  27. fun String.countVowels(): Int {
  28.     val vowels = "aeiouAEIOU"
  29.     var count = 0
  30.     for (char in this) {
  31.         if (char in vowels) {
  32.             count++
  33.         }
  34.     }
  35.     return count
  36. }
  37.  
  38. fun posMassimo(elenco: MutableList<String>, comparatore: (s1: String, s2: String) -> Boolean ): Int
  39. {
  40.     if (elenco.count() == 0) return -1
  41.  
  42.     var pos = 0
  43.     if (elenco.size>=2)
  44.         for (i in 1..elenco.lastIndex)
  45.             if (comparatore(elenco[i], elenco[pos]))    //(greaterThan(elenco[i], elenco[pos]))                 //(elenco[i]>elenco[pos])
  46.                 pos = i
  47.  
  48.     return pos
  49. }
  50.  
  51.  
  52.  
  53. fun main() {
  54.  
  55.     val registroAula =
  56.         arrayListOf<String>("Azzino", "Castrovillari", "Aiuliao", "Amati", "Zucchetti", "Pinturin", "Singh")
  57.  
  58.  
  59.     println("Massimo: ${registroAula[posMassimo(registroAula) { s1: String, s2: String  ->
  60.         s1.contaVocali() > s2.contaVocali()}] }")
  61.  
  62.     //println("Massimo: ${registroAula[posMassimo(registroAula, ::haPiuVocali)]}")
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment