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! :)