Advertisement
Kostiggig

Random sequence

Aug 7th, 2022
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.96 KB | None | 0 0
  1. fun main() {
  2.     val numbers = listOf("0", "1", "2","3","4","5","92","21")
  3.     val users = listOf<TestUser>(
  4.         TestUser.Base(
  5.             "Vasya","26"
  6.         ),TestUser.Base(
  7.             "Petya","38"
  8.         ),TestUser.Base(
  9.             "Kostya","41"
  10.         ),TestUser.Base(
  11.             "Bob","18"
  12.         ),TestUser.Base(
  13.             "Victor","26"
  14.         ),TestUser.Base(
  15.             "Lolip","98"
  16.         ),
  17.     )
  18.  
  19.     val concat = Concat.String(
  20.         " "
  21.     )
  22.  
  23.  
  24.     val userConcat = Concat.User("\n\n")
  25.  
  26.     val usersRandomSequence = RandomSequence.User(
  27.         users,userConcat
  28.     )
  29.  
  30.     val randomSequence = RandomSequence.String(
  31.         numbers,concat
  32.     )
  33.  
  34.     randomSequence.generate()
  35.     usersRandomSequence.generate()
  36.  
  37.     println("Numbers sequence is ${concat.result()}")
  38.     println("Users sequence is ${userConcat.result()}")
  39. }
  40.  
  41. interface TestUser {
  42.  
  43.     data class Base(
  44.         private val name: String,
  45.         private val age: String,
  46.     ) : TestUser
  47.  
  48.     object Empty : TestUser
  49. }
  50.  
  51. interface Concat<T,R> {
  52.  
  53.     fun concat(data: T)
  54.  
  55.     fun last() : T
  56.  
  57.     fun result() : R
  58.  
  59.     class String(
  60.         private val split: kotlin.String = ""
  61.     ) : Concat<kotlin.String,kotlin.String> {
  62.         private var result = " "
  63.  
  64.         override fun concat(data: kotlin.String) {
  65.             result += split + data
  66.         }
  67.  
  68.         override fun last() = result.last().toString()
  69.  
  70.         override fun result() = result
  71.     }
  72.  
  73.     class User(
  74.         private val split: kotlin.String = ""
  75.     ) : Concat<TestUser,kotlin.String> {
  76.         private var last: TestUser = TestUser.Empty
  77.         private var result = " "
  78.  
  79.         override fun concat(data: TestUser) {
  80.             result += split + data
  81.             last = data
  82.         }
  83.  
  84.         override fun last() = last
  85.  
  86.         override fun result() = result
  87.     }
  88. }
  89.  
  90. interface RandomSequence {
  91.  
  92.     fun generate()
  93.  
  94.     abstract class Abstract<E,R>(
  95.         private val src: Collection<E>,
  96.         private val concat: Concat<E,R>
  97.     ) : RandomSequence {
  98.  
  99.         override fun generate() {
  100.             val size = src.size
  101.             var n = 0
  102.             while (n <= size) {
  103.                 val copy = ArrayList(src.shuffled())
  104.  
  105.                 val lastNumber = concat.last()
  106.                 for (index in 0 until size - 1) {
  107.                     val current = copy[index]
  108.                     val next = copy[index + 1]
  109.                     if (current != lastNumber && current != next) {
  110.                         concat.concat(current)
  111.                     }
  112.                 }
  113.                 n++
  114.             }
  115.         }
  116.     }
  117.     class String(
  118.         src: List<kotlin.String>,
  119.         concat: Concat<kotlin.String,kotlin.String>
  120.     ) : Abstract<kotlin.String,kotlin.String>(src,concat)
  121.  
  122.     class User(
  123.         src: List<TestUser>,
  124.         concat: Concat<TestUser,kotlin.String>
  125.     ) : Abstract<TestUser,kotlin.String>(src,concat)
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement