Esa
09/27/2020, 12:15 PMnanodeath
09/27/2020, 3:49 PMEsa
09/27/2020, 4:13 PMitbhp
09/27/2020, 4:43 PMnanodeath
09/27/2020, 5:48 PMEsa
09/27/2020, 6:18 PMn
people, with each person having 1 computer each. Each computer will only work with the correct person. However, all computers are with the wrong person and need to be swapped, but you can only swap one at a time, and nobody can be without a computer. You have 1 computer that is not in use that can be used temporarily to free up the computer of a person to allow that person to return their current computer.
Given all of that, what'd be the best approach to swap every computer in one go?
That's what got me thinking about a function that transforms one element (swaps a computer) and uses the result (returned computer) to look up the next element (person who needs the computer of the previous person) to be transformed (swapped).Esa
09/27/2020, 6:20 PMEsa
09/27/2020, 6:21 PMpredicate
to find{}
the next element of the collection using the result of transform
. That already smells like horribly inefficient code, so I guess it's not exactly the best way to solve it..Esa
09/27/2020, 6:24 PMEsa
09/27/2020, 6:25 PMitbhp
09/27/2020, 6:26 PMitbhp
09/27/2020, 6:27 PMitbhp
09/27/2020, 6:28 PMEsa
09/27/2020, 6:38 PMn
with n-1
you may not end up with the correct pairing as n
could very well belong with n-7
. There's a big chance I misunderstood you as I am not familiar with bubblesort or insertion sortitbhp
09/27/2020, 6:47 PMEsa
09/27/2020, 6:52 PMEsa
09/27/2020, 7:11 PMEsa
09/27/2020, 7:20 PMMichael de Kaste
09/28/2020, 8:17 AMEsa
09/28/2020, 8:39 AM(this.isNotEmpty()) Collection.first()
for the initial transformation, if that's what you meantMichael de Kaste
09/28/2020, 8:39 AMMichael de Kaste
09/28/2020, 8:40 AMMichael de Kaste
09/28/2020, 8:40 AMEsa
09/28/2020, 8:41 AMfirst()
one. Could've just as well been last()
or shuffled().first()
😛 Or even provided as an argument in the extension functionEsa
09/28/2020, 8:42 AMMichael de Kaste
09/28/2020, 12:22 PMgiven previous outcome, what is the next one?
isn't an ordering.
• It seems you'd like to find a "path" of some sort of a list, which is an unique enough operation to not qualify it as an extension function, but I'm not stopping you.
• Your function signature seems correct, so what part do you have troubles with?Andrew
09/28/2020, 10:06 PM