Skip to main content
Commonmark migration
Source Link

Haskell, 98 71 bytes

h.r h(x:s)|(a,b)<-span(/=x)s=l b:l s:h(b++r a) h e=e r=reverse l=length 

Try it online!

###Explanation

Explanation

For a list of length n this method produces 2*n flops. It works by looking at the last element of the list, looking for the same element in the list before and flipping it to the second to last position. Then the list with the last element removed is recursively "neighboured".

For the list [1,2,3,1,2] the algorithm works like this:

[1,2,3,1,2] flip longest prefix that ends in 2: flop 2 [2,1,3,1,2] bring first element to second to last position: flop n-1 = flop 4 [1,3,1,2,2] recursively work on n-1 list [1,3,1,2] there is no other 2: flop 0 [1,3,1,2] flop n-1 = flop 3 [1,3,1,2] recurse [1,3,1] flop 1 [1,3,1] flop 2 [3,1,1] recurse [3,1] flop 0 [3,1] flop 1 ... 

All together this yields the flops [2,4,0,3,1,2,0,1,0,0] and the neighboured list [3,1,1,2,2].

Haskell, 98 71 bytes

h.r h(x:s)|(a,b)<-span(/=x)s=l b:l s:h(b++r a) h e=e r=reverse l=length 

Try it online!

###Explanation

For a list of length n this method produces 2*n flops. It works by looking at the last element of the list, looking for the same element in the list before and flipping it to the second to last position. Then the list with the last element removed is recursively "neighboured".

For the list [1,2,3,1,2] the algorithm works like this:

[1,2,3,1,2] flip longest prefix that ends in 2: flop 2 [2,1,3,1,2] bring first element to second to last position: flop n-1 = flop 4 [1,3,1,2,2] recursively work on n-1 list [1,3,1,2] there is no other 2: flop 0 [1,3,1,2] flop n-1 = flop 3 [1,3,1,2] recurse [1,3,1] flop 1 [1,3,1] flop 2 [3,1,1] recurse [3,1] flop 0 [3,1] flop 1 ... 

All together this yields the flops [2,4,0,3,1,2,0,1,0,0] and the neighboured list [3,1,1,2,2].

Haskell, 98 71 bytes

h.r h(x:s)|(a,b)<-span(/=x)s=l b:l s:h(b++r a) h e=e r=reverse l=length 

Try it online!

Explanation

For a list of length n this method produces 2*n flops. It works by looking at the last element of the list, looking for the same element in the list before and flipping it to the second to last position. Then the list with the last element removed is recursively "neighboured".

For the list [1,2,3,1,2] the algorithm works like this:

[1,2,3,1,2] flip longest prefix that ends in 2: flop 2 [2,1,3,1,2] bring first element to second to last position: flop n-1 = flop 4 [1,3,1,2,2] recursively work on n-1 list [1,3,1,2] there is no other 2: flop 0 [1,3,1,2] flop n-1 = flop 3 [1,3,1,2] recurse [1,3,1] flop 1 [1,3,1] flop 2 [3,1,1] recurse [3,1] flop 0 [3,1] flop 1 ... 

All together this yields the flops [2,4,0,3,1,2,0,1,0,0] and the neighboured list [3,1,1,2,2].

deleted 40 characters in body
Source Link
Laikoni
  • 26.4k
  • 7
  • 54
  • 116

Haskell, 9898 71 bytes

h.r s%n=take n s++r(drop n s) h(x:s)|(a,b)<-span(/=x)s=l b:l s:h(s%lb++r a%0a) h e=e r=reverse l=length 

Try it online!Try it online!

###Explanation

For a list of length n this method produces 2*n flops. It works by looking at the last element of the list, looking for the same element in the list before and flipping it to the second to last position. Then the list with the last element removed is recursively "neighboured".

For the list [1,2,3,1,2] the algorithm works like this:

[1,2,3,1,2] flip longest prefix that ends in 2: flop 2 [2,1,3,1,2] bring first element to second to last position: flop n-1 = flop 4 [1,3,1,2,2] recursively work on n-1 list [1,3,1,2] there is no other 2: flop 0 [1,3,1,2] flop n-1 = flop 3 [1,3,1,2] recurse [1,3,1] flop 1 [1,3,1] flop 2 [3,1,1] recurse [3,1] flop 0 [3,1] flop 1 ... 

All together this yields the flops [2,4,0,3,1,2,0,1,0,0] and the neighboured list [3,1,1,2,2].

Haskell, 98 bytes

h.r s%n=take n s++r(drop n s) h(x:s)|(a,b)<-span(/=x)s=l b:l s:h(s%l a%0) h e=e r=reverse l=length 

Try it online!

###Explanation

For a list of length n this method produces 2*n flops. It works by looking at the last element of the list, looking for the same element in the list before and flipping it to the second to last position. Then the list with the last element removed is recursively "neighboured".

For the list [1,2,3,1,2] the algorithm works like this:

[1,2,3,1,2] flip longest prefix that ends in 2: flop 2 [2,1,3,1,2] bring first element to second to last position: flop n-1 = flop 4 [1,3,1,2,2] recursively work on n-1 list [1,3,1,2] there is no other 2: flop 0 [1,3,1,2] flop n-1 = flop 3 [1,3,1,2] recurse [1,3,1] flop 1 [1,3,1] flop 2 [3,1,1] recurse [3,1] flop 0 [3,1] flop 1 ... 

All together this yields the flops [2,4,0,3,1,2,0,1,0,0] and the neighboured list [3,1,1,2,2].

Haskell, 98 71 bytes

h.r h(x:s)|(a,b)<-span(/=x)s=l b:l s:h(b++r a) h e=e r=reverse l=length 

Try it online!

###Explanation

For a list of length n this method produces 2*n flops. It works by looking at the last element of the list, looking for the same element in the list before and flipping it to the second to last position. Then the list with the last element removed is recursively "neighboured".

For the list [1,2,3,1,2] the algorithm works like this:

[1,2,3,1,2] flip longest prefix that ends in 2: flop 2 [2,1,3,1,2] bring first element to second to last position: flop n-1 = flop 4 [1,3,1,2,2] recursively work on n-1 list [1,3,1,2] there is no other 2: flop 0 [1,3,1,2] flop n-1 = flop 3 [1,3,1,2] recurse [1,3,1] flop 1 [1,3,1] flop 2 [3,1,1] recurse [3,1] flop 0 [3,1] flop 1 ... 

All together this yields the flops [2,4,0,3,1,2,0,1,0,0] and the neighboured list [3,1,1,2,2].

Source Link
Laikoni
  • 26.4k
  • 7
  • 54
  • 116

Haskell, 98 bytes

h.r s%n=take n s++r(drop n s) h(x:s)|(a,b)<-span(/=x)s=l b:l s:h(s%l a%0) h e=e r=reverse l=length 

Try it online!

###Explanation

For a list of length n this method produces 2*n flops. It works by looking at the last element of the list, looking for the same element in the list before and flipping it to the second to last position. Then the list with the last element removed is recursively "neighboured".

For the list [1,2,3,1,2] the algorithm works like this:

[1,2,3,1,2] flip longest prefix that ends in 2: flop 2 [2,1,3,1,2] bring first element to second to last position: flop n-1 = flop 4 [1,3,1,2,2] recursively work on n-1 list [1,3,1,2] there is no other 2: flop 0 [1,3,1,2] flop n-1 = flop 3 [1,3,1,2] recurse [1,3,1] flop 1 [1,3,1] flop 2 [3,1,1] recurse [3,1] flop 0 [3,1] flop 1 ... 

All together this yields the flops [2,4,0,3,1,2,0,1,0,0] and the neighboured list [3,1,1,2,2].