Advertisement
Al3XXX

pairs

Aug 31st, 2022 (edited)
1,075
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.59 KB | None | 0 0
  1. import java.lang.IllegalStateException
  2.  
  3. fun main() {
  4.     val elementsA = listOf(0, 1, 2, 3, 4)
  5.     val elementsB = listOf(5, 6, 7)
  6.     for (i in 0..1000) {
  7.         val pairs: MutableSet<Pair<Int, Int>> = combineToPairs(elementsA, elementsB)
  8.         check(pairs, elementsA.size)
  9.         println("$pairs\n")
  10.     }
  11. }
  12.  
  13. fun combineToPairs(listA: List<Int>, listB: List<Int>, combinations: Int = 10): MutableSet<Pair<Int, Int>> {
  14.     val result = mutableSetOf<Pair<Int, Int>>() // LinkedHashSet
  15.     val listACopy = listA.shuffled().toMutableList()
  16.     var i = 0
  17.     var j = 0
  18.     while (result.size < combinations) {
  19.         val added = result.add(listACopy[i++] to listB[j])
  20.         if (!added) {
  21.             // Rollback previously added elements
  22.             //println("${"-".repeat(20)}ROLLBACK${"-".repeat(20)}")
  23.             val timesListAAdded = result.size / listA.size
  24.             val uniquePairs = result.take(listA.size * timesListAAdded)
  25.             result.replaceWith(uniquePairs)
  26.             listACopy.replaceWith(uniquePairs.takeLast(listA.size).map { it.first })
  27.             j = listB.indexOf(result.last().second)
  28.         }
  29.         if (i == listACopy.size || !added) {
  30.             val listTemp = listACopy.toMutableList()
  31.             while (listTemp == listACopy || listTemp.first() == listACopy.last()) {
  32.                 listTemp.shuffle()
  33.             }
  34.             listACopy.replaceWith(listTemp)
  35.             i = 0
  36.         }
  37.         j = ++j % listB.size
  38.     }
  39.     return result
  40. }
  41.  
  42. fun <T> MutableCollection<T>.replaceWith(newElements: Collection<T>) {
  43.     clear()
  44.     addAll(newElements)
  45. }
  46.  
  47. fun check(pairs: MutableSet<Pair<Int, Int>>, size: Int) {
  48.     val halves = pairs.chunked(size)
  49.     val firstNumbers = halves.map { half ->
  50.         val nums = half.map { it.first }
  51.         print(nums)
  52.         LinkedHashSet(nums)
  53.     }
  54.     println(" <- first numbers of pairs")
  55.  
  56.     val isFirstNumbersCombinationsUnique = firstNumbers.map { it.toList() }.toSet().size == firstNumbers.size
  57.  
  58.     val actualSecondNumbers = pairs.map { it.second }
  59.     val expectedSecondNumbers = listOf(5, 6, 7, 5, 6, 7, 5, 6, 7, 5)
  60.  
  61.     val isHalvesWithCorrectSize = firstNumbers.all { it.size == size }
  62.  
  63.     if (!isHalvesWithCorrectSize) {
  64.         throw IllegalStateException("halves have duplicates of first numbers")
  65.     }
  66.     if (!isFirstNumbersCombinationsUnique){
  67.         throw IllegalStateException("halves have the same combinations of first numbers")
  68.     }
  69.     if (actualSecondNumbers != expectedSecondNumbers) {
  70.         throw IllegalStateException("seconds numbers are incorrect")
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement