Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun main() {
- val numbers = listOf("0", "1", "2","3","4","5","92","21")
- val users = listOf<TestUser>(
- TestUser.Base(
- "Vasya","26"
- ),TestUser.Base(
- "Petya","38"
- ),TestUser.Base(
- "Kostya","41"
- ),TestUser.Base(
- "Bob","18"
- ),TestUser.Base(
- "Victor","26"
- ),TestUser.Base(
- "Lolip","98"
- ),
- )
- val concat = Concat.String(
- " "
- )
- val userConcat = Concat.User("\n\n")
- val usersRandomSequence = RandomSequence.User(
- users,userConcat
- )
- val randomSequence = RandomSequence.String(
- numbers,concat
- )
- randomSequence.generate()
- usersRandomSequence.generate()
- println("Numbers sequence is ${concat.result()}")
- println("Users sequence is ${userConcat.result()}")
- }
- interface TestUser {
- data class Base(
- private val name: String,
- private val age: String,
- ) : TestUser
- object Empty : TestUser
- }
- interface Concat<T,R> {
- fun concat(data: T)
- fun last() : T
- fun result() : R
- class String(
- private val split: kotlin.String = ""
- ) : Concat<kotlin.String,kotlin.String> {
- private var result = " "
- override fun concat(data: kotlin.String) {
- result += split + data
- }
- override fun last() = result.last().toString()
- override fun result() = result
- }
- class User(
- private val split: kotlin.String = ""
- ) : Concat<TestUser,kotlin.String> {
- private var last: TestUser = TestUser.Empty
- private var result = " "
- override fun concat(data: TestUser) {
- result += split + data
- last = data
- }
- override fun last() = last
- override fun result() = result
- }
- }
- interface RandomSequence {
- fun generate()
- abstract class Abstract<E,R>(
- private val src: Collection<E>,
- private val concat: Concat<E,R>
- ) : RandomSequence {
- override fun generate() {
- val size = src.size
- var n = 0
- while (n <= size) {
- val copy = ArrayList(src.shuffled())
- val lastNumber = concat.last()
- for (index in 0 until size - 1) {
- val current = copy[index]
- val next = copy[index + 1]
- if (current != lastNumber && current != next) {
- concat.concat(current)
- }
- }
- n++
- }
- }
- }
- class String(
- src: List<kotlin.String>,
- concat: Concat<kotlin.String,kotlin.String>
- ) : Abstract<kotlin.String,kotlin.String>(src,concat)
- class User(
- src: List<TestUser>,
- concat: Concat<TestUser,kotlin.String>
- ) : Abstract<TestUser,kotlin.String>(src,concat)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement