29
2
As computer scientists, you're probably all familiar with the basic list operations of pop and push. These are simple operations that modify a list of elements. However, have you ever heard of the operation flop? (as in flip-flop)? It's pretty simple. Given a number n, reverse the first n elements of the list. Here's an example:
>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a.flop(4)
[4, 3, 2, 1, 5, 6, 7, 8, 9, 10]
The cool thing about the flop operation, is that you can use it do some cool things to a list, such as sorting it. We're going to do something similar with flops:
Given a list of integers, "Neighbor it". In other words, sort it so that every duplicate element appears consecutively.
This can be done with flops! For example, take the following list:
>>> a = [3, 2, 1, 4, 3, 3, 2]
>>> a.flop(4)
[4, 1, 2, 3, 3, 3, 2]
>>> a.flop(3)
[2, 1, 4, 3, 3, 3, 2]
>>> a.flop(6)
[3, 3, 3, 4, 1, 2, 2]
This leads us to the definition of today's challenge:
Given a list of integers, output any set of flops that will result in the list being neighbored.
Using the last list as an example, you should output:
4
3
6
because flopping the list by 4, then by 3, then by 6 will result in a neighbored list. Keep in mind that you do not need to print the shortest possible list of flops that neighbors a list. If you had printed:
4
4
4
3
1
1
6
2
2
instead, this would still be a valid output. However, you may not ever output a number larger than the length of the list. This is because for a list a = [1, 2, 3]
, calling a.flop(4)
is nonsensical.
Here are some examples:
#Input:
[2, 6, 0, 3, 1, 5, 5, 0, 5, 1]
#Output
[3, 7, 8, 6, 9]
#Input
[1, 2]
#Output
<any list of integers under 3, including an empty list>
#Input
[2, 6, 0, 2, 1, 4, 5, 1, 3, 2, 1, 5, 6, 4, 4, 1, 4, 6, 6, 0]
#Output
[3, 19, 17, 7, 2, 4, 11, 15, 2, 7, 13, 4, 14, 2]
#Input
[1, 1, 1, 1, 2, 2, 2, -1, 4]
#Output
[]
#Input
[4, 4, 8, 8, 15, 16, 16, 23, 23, 42, 42, 15]
#Output
[12, 7]
Keep in mind that in each of these examples, the output given is just one potential valid output. As I said before, any set of flops that neighbors the given list is a valid output. You can use this python script to verify if a given list of flops correctly neighbors a list.
You may take input and output in any reasonable format. For example, function arguments/return value, STDIN/STDOUT, reading/writing a file etc. are all valid. As usual, this is code-golf, so make the shortest program you can, and have fun! :)
3I heard it as fl(oating point) op(eration). – Weijun Zhou – 2018-01-31T00:21:50.763
3
@WeijunZhou That's a measure of computational speed, for counting operations carried out py a piece of hardware. https://en.wikipedia.org/wiki/FLOPS
– iPhoenix – 2018-01-31T00:24:16.487Related – James – 2018-01-31T01:50:02.157
3Do submissions have to be deterministic or can I pseudo-randomly flop until the array is grouped? – Dennis – 2018-01-31T03:17:04.647
@Dennis As long as small inputs finish reasonably fast and reliably, I guess that's fine. – James – 2018-01-31T04:29:00.113
3Are zero flops allowed to appear in the output? – Laikoni – 2018-01-31T07:49:45.703
4Related. NB any answer to that question would be an answer to this one, but since being sorted is a stronger condition than being "neighboured" it may be possible to out-golf them so this might not be a duplicate (although the fact that the only answer so far sorts is not encouraging). – Peter Taylor – 2018-01-31T08:24:11.580
Would it be OK to use a string of characters as input ? So e.g.
ABA
instead of11 2 11
– Ton Hospel – 2018-01-31T10:33:43.407