Advertisement
Guest User

random cases

a guest
Nov 15th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.42 KB | None | 0 0
  1.     fun Any.print() = println(this.toString())
  2.  
  3.     data class SomeObject(val id: Int, val title: String)
  4.  
  5.     var mQuestionsList: Array<Any>? = arrayOf()
  6.     for (i in 0..10) {
  7.         mQuestionsList = mQuestionsList?.plus(
  8.             SomeObject(
  9.                 i,
  10.                 ('a'..'z').toList().shuffled().take(10).joinToString().filter { it.isLetter() })
  11.         )
  12.     }
  13.  
  14.     ("item: " + mQuestionsList?.joinToString("\nitem: ")).print()
  15.  
  16.     mQuestionsList?.let {
  17.         val indicesCache = mutableSetOf<Int>()
  18. //        val itemsCache = mutableSetOf<Int>()
  19.         repeat(it.count()) {
  20.             val randomIdx = mQuestionsList.indices.random()
  21. //            val randomItem = mQuestionsList[mQuestionsList.indices.random()] as SomeObject
  22.  
  23.             if (randomIdx !in indicesCache) {
  24.                 indicesCache.add(randomIdx)
  25.                 ("index: ${mQuestionsList[randomIdx]}").print()
  26.             } else {
  27.                 ("cache already contains $randomIdx").print()
  28.             }
  29.  
  30. //            if (randomItem.id !in itemsCache) {
  31. //                itemsCache.add(randomItem.id)
  32. //                ("item: $randomItem").print()
  33. //            } else {
  34. //                ("cache already contains $randomItem").print()
  35. //            }
  36.  
  37.         }
  38.         "cache contains indices: ${indicesCache.joinToString()}".print()
  39. //        "cache contains items: ${itemsCache.joinToString()}".print()
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement