zain
02/14/2022, 9:38 AMThe logic for creating the fixture is in pairs. You have to form a pair of two teams in a random
fashion. For example - If I have 4 teams - T1, T2, T3, T4. I can have:
(T1 vs T2) & (T3 vs T4)
OR
(T1 vs T3) & (T2 vs T4)
OR
(T1 vs T4) & (T2 vs T3)
*Your function should accept any number n, where n is an even number > 0 and return you n/2
pairs/tuples*
Joffrey
02/14/2022, 9:39 AMzain
02/14/2022, 9:41 AMzain
02/14/2022, 9:43 AMJoffrey
02/14/2022, 9:47 AMval randomPairs = (1..n).shuffled().chunked(2)
ephemient
02/14/2022, 9:49 AM.windowed(2, 2) { (x, y) -> x to y }
, as .zipWithNext()
produces overlapping pairsJoffrey
02/14/2022, 9:51 AMzipWithNext
pairs, but I meant the behaviour of chunked()
actually, it doesn't return a Pair
type per se but we don't really know the required output type tbh, so lists of 2 elements might be enoughzain
02/14/2022, 9:52 AMval randomIndex = Random.nextInt(teams.size)
val team = teams[randomIndex]
ephemient
02/14/2022, 9:54 AMzain
02/14/2022, 9:55 AMreturn you n/2 pairs/tuples
ephemient
02/14/2022, 9:57 AM.windowed(2, 2) { (x, y) -> x to y }
or .chunked(2) { (x, y) -> x to y }
both give you List<Pair<T, T>>
ephemient
02/14/2022, 9:58 AM.shuffled()
itself does, which is a Fisher–Yates shufflezain
02/14/2022, 9:59 AMJoffrey
02/14/2022, 9:59 AMPair
. A list of 2 elements is also a pair or tuple. That's what I meant by "the output type is unspecified".
That said, as @ephemient pointed out, if you you really want the Pair
type you can use the overload of chunked
that takes a transform lambda. I don't like windowed()
for this because its semantics is in general to have a sliding window with overlaps, and using it with the same value for window size and step is better expressed by chunked
IMOephemient
02/14/2022, 10:01 AM.shuffle()
which will randomize a mutable list's elements, and .shuffled()
which returns a new list with a random disarrangement of the original's elementsephemient
02/14/2022, 10:02 AMchunked()
works in this scenario as it is stated that the input n
is even; in more general cases I prefer windowed()
because you can choose how to handle odd elements (if you enable partial windows)zain
02/14/2022, 10:02 AMchunked()
internally uses windowed()
Since Kotlin 1.2ephemient
02/14/2022, 10:03 AMchunked()
is a convenient special case of windowed()
ephemient
02/14/2022, 10:03 AMzain
02/14/2022, 10:04 AMPair
, should I map()
it?Joffrey
02/14/2022, 10:05 AMmap
@ephemient has shown it already:
val randomPairs = (1..n).shuffled().chunked(2) { (x, y) -> x to y }
This lambda takes each "chunk" (list of 2 elements) as input, destructures them into their first and second element by using (x, y)
(with parentheses syntax), and creates a Pair
using to
ephemient
02/14/2022, 10:06 AM.chunked()
produces a list whose elements are also lists. you can leave them as-is (if lists are acceptable output), or map them to Pair
, or use the .chunked()
overload that takes a transformation which is semantically equivalent to .chunked()
without the transformation followed by a .map()
with the transformationzain
02/14/2022, 10:08 AMtransform: (List<T>) -> R
Now I understood transformation in the real sense. It just overload got me confused.
Thank youJoffrey
02/14/2022, 10:13 AMzain
02/15/2022, 5:32 AMrandom()
to the internal list so that I can one element from each fixture and then shuffle and create another semi-final fixture.
What I noticed is the fixture is repeated in the semi-final.
So if T3 v/s T4 is the fixture it is again coming in semi final
val fixtures: List<List<Team>> = teams.shuffled().chunked(2)
assertEquals(fixtures.size, 4)
val semiFinalist = fixtures.map { fixture ->
fixture.random()
}.shuffled().chunked(2)
assertEquals(semiFinalist.size, 2)
Joffrey
02/15/2022, 8:03 AMzain
02/15/2022, 8:17 AMval semiFinalist by lazy {
randomFixturePairs.map { fixture ->
fixture.toList().random()
}.shuffled().chunked(2)
}
Joffrey
02/15/2022, 8:22 AMList
, you don't need toList
because you don't need to make a copy, especially if you just want to take a random element out of it. (Unless you changed it back to using pairs, in this case, ok).
I don't quite understand what you're trying to do with lazy
here. Running the program again will change the random elements anyway, it won't help "remember" state across runs (if that was your goal). If you want to "fix the random", you can use a custom seed.
That said the previous code looked good to me. Are you sure you had run into problems with that code?zain
02/15/2022, 8:42 AMlazy
to avoid rerunning when I move from one screen to another to remember the state.ephemient
02/15/2022, 8:46 AMephemient
02/15/2022, 8:47 AM