Skip to main content
added 673 characters in body
Source Link
mdatsev
  • 111
  • 4

JavaScript, 150 bytes

(a,f=n=>(a=[...a.slice(0, n).reverse(),...a.slice(n)],n),l=a.length,i=0)=>a.reduce(c=>[...c,f(a.indexOf(Math.max(...a.slice(0, l-i)))+1),f(l-i++)],[]) 

Try it online!

JavaScript, 151 bytes

a=>{f=n=>(a=[...a.slice(0,n).reverse(),...a.slice(n)],n),r=[];for(i=a.length+1;--i>0;)r.push(f(a.indexOf(Math.max(...a.slice(0, i)))+1),f(i));return r} 

[Try it online!][TIO-jd34lh7b] [TIO-jd34lh7b]: https://tio.run/##VY5BasMwEEX3OYV2mcHyECe7GPkGpQcwXgz2yFZQJSMpIVBydsftqt18PrwH/9/4wXlMbi11iJNsXoqazcam@7YmmA7Y9ETElL0bBU46ICV5SMoCqP@QgMPOdDL90NqYwBkmL2EuS9W0de26U4uJ1ntewAKTC5M8Py18cFnoi5/wb0Q5RKwa1Bb21iYp9xRUev3em7iwMqpv9Flf9J7D4TDGkKMX8nGGo/VxzVd1VJWa4cdG3N4 "JavaScript (Node.js) – Try It Online"

ThisBoth basically sortssort the array by flipping the max number to the beginning and then flipping it to the back, repeating this with the remaining array. The first one uses reduce, the second one uses a for loop.

##Ungolfed:

array => { let flop = n => { array = [...array.slice(0, n).reverse(), ...array.slice(n)]; return n; } let flops = []; for (let i = array.length + 1; --i > 0;) { let maxIndex = array.indexOf(Math.max(...array.slice(0, i))); flops.push(flop(maxIndex + 1), flop(i)); } return flops; } 

JavaScript, 151 bytes

a=>{f=n=>(a=[...a.slice(0,n).reverse(),...a.slice(n)],n),r=[];for(i=a.length+1;--i>0;)r.push(f(a.indexOf(Math.max(...a.slice(0, i)))+1),f(i));return r} 

[Try it online!][TIO-jd34lh7b] [TIO-jd34lh7b]: https://tio.run/##VY5BasMwEEX3OYV2mcHyECe7GPkGpQcwXgz2yFZQJSMpIVBydsftqt18PrwH/9/4wXlMbi11iJNsXoqazcam@7YmmA7Y9ETElL0bBU46ICV5SMoCqP@QgMPOdDL90NqYwBkmL2EuS9W0de26U4uJ1ntewAKTC5M8Py18cFnoi5/wb0Q5RKwa1Bb21iYp9xRUev3em7iwMqpv9Flf9J7D4TDGkKMX8nGGo/VxzVd1VJWa4cdG3N4 "JavaScript (Node.js) – Try It Online"

This basically sorts the array by flipping the max number to the beginning and then flipping it to the back, repeating this with the remaining array.

##Ungolfed:

array => { let flop = n => { array = [...array.slice(0, n).reverse(), ...array.slice(n)]; return n; } let flops = []; for (let i = array.length + 1; --i > 0;) { let maxIndex = array.indexOf(Math.max(...array.slice(0, i))); flops.push(flop(maxIndex + 1), flop(i)); } return flops; } 

JavaScript, 150 bytes

(a,f=n=>(a=[...a.slice(0, n).reverse(),...a.slice(n)],n),l=a.length,i=0)=>a.reduce(c=>[...c,f(a.indexOf(Math.max(...a.slice(0, l-i)))+1),f(l-i++)],[]) 

Try it online!

JavaScript, 151 bytes

a=>{f=n=>(a=[...a.slice(0,n).reverse(),...a.slice(n)],n),r=[];for(i=a.length+1;--i>0;)r.push(f(a.indexOf(Math.max(...a.slice(0, i)))+1),f(i));return r} 

[Try it online!][TIO-jd34lh7b] [TIO-jd34lh7b]: https://tio.run/##VY5BasMwEEX3OYV2mcHyECe7GPkGpQcwXgz2yFZQJSMpIVBydsftqt18PrwH/9/4wXlMbi11iJNsXoqazcam@7YmmA7Y9ETElL0bBU46ICV5SMoCqP@QgMPOdDL90NqYwBkmL2EuS9W0de26U4uJ1ntewAKTC5M8Py18cFnoi5/wb0Q5RKwa1Bb21iYp9xRUev3em7iwMqpv9Flf9J7D4TDGkKMX8nGGo/VxzVd1VJWa4cdG3N4 "JavaScript (Node.js) – Try It Online"

Both basically sort the array by flipping the max number to the beginning and then flipping it to the back, repeating this with the remaining array. The first one uses reduce, the second one uses a for loop.

##Ungolfed:

array => { let flop = n => { array = [...array.slice(0, n).reverse(), ...array.slice(n)]; return n; } let flops = []; for (let i = array.length + 1; --i > 0;) { let maxIndex = array.indexOf(Math.max(...array.slice(0, i))); flops.push(flop(maxIndex + 1), flop(i)); } return flops; } 
added 560 characters in body
Source Link
mdatsev
  • 111
  • 4

JavaScript, 151 bytes

a=>{f=n=>(a=[...a.slice(0,n).reverse(),...a.slice(n)],n),r=[];for(i=a.length+1;--i>0;)r.push(f(a.indexOf(Math.max(...a.slice(0, i)))+1),f(i));return r} 

[Try it online!][TIO-jd34lh7b] [TIO-jd34lh7b]: https://tio.run/##VY5BasMwEEX3OYV2mcHyECe7GPkGpQcwXgz2yFZQJSMpIVBydsftqt18PrwH/9/4wXlMbi11iJNsXoqazcam@7YmmA7Y9ETElL0bBU46ICV5SMoCqP@QgMPOdDL90NqYwBkmL2EuS9W0de26U4uJ1ntewAKTC5M8Py18cFnoi5/wb0Q5RKwa1Bb21iYp9xRUev3em7iwMqpv9Flf9J7D4TDGkKMX8nGGo/VxzVd1VJWa4cdG3N4 "JavaScript (Node.js) – Try It Online" 

This basically sorts the array by flipping the max number to the beginning and then flipping it to the back, repeating this with the remaining array.

##Ungolfed:

array => { let flop = n => { array = [...array.slice(0, n).reverse(), ...array.slice(n)]; return n; } let flops = []; for (let i = array.length + 1; --i > 0;) { let maxIndex = array.indexOf(Math.max(...array.slice(0, i))); flops.push(flop(maxIndex + 1), flop(i)); } return flops; } 

JavaScript, 151 bytes

a=>{f=n=>(a=[...a.slice(0,n).reverse(),...a.slice(n)],n),r=[];for(i=a.length+1;--i>0;)r.push(f(a.indexOf(Math.max(...a.slice(0, i)))+1),f(i));return r} 

[Try it online!][TIO-jd34lh7b] [TIO-jd34lh7b]: https://tio.run/##VY5BasMwEEX3OYV2mcHyECe7GPkGpQcwXgz2yFZQJSMpIVBydsftqt18PrwH/9/4wXlMbi11iJNsXoqazcam@7YmmA7Y9ETElL0bBU46ICV5SMoCqP@QgMPOdDL90NqYwBkmL2EuS9W0de26U4uJ1ntewAKTC5M8Py18cFnoi5/wb0Q5RKwa1Bb21iYp9xRUev3em7iwMqpv9Flf9J7D4TDGkKMX8nGGo/VxzVd1VJWa4cdG3N4 "JavaScript (Node.js) – Try It Online"

JavaScript, 151 bytes

a=>{f=n=>(a=[...a.slice(0,n).reverse(),...a.slice(n)],n),r=[];for(i=a.length+1;--i>0;)r.push(f(a.indexOf(Math.max(...a.slice(0, i)))+1),f(i));return r} 

[Try it online!][TIO-jd34lh7b] [TIO-jd34lh7b]: https://tio.run/##VY5BasMwEEX3OYV2mcHyECe7GPkGpQcwXgz2yFZQJSMpIVBydsftqt18PrwH/9/4wXlMbi11iJNsXoqazcam@7YmmA7Y9ETElL0bBU46ICV5SMoCqP@QgMPOdDL90NqYwBkmL2EuS9W0de26U4uJ1ntewAKTC5M8Py18cFnoi5/wb0Q5RKwa1Bb21iYp9xRUev3em7iwMqpv9Flf9J7D4TDGkKMX8nGGo/VxzVd1VJWa4cdG3N4 "JavaScript (Node.js) – Try It Online" 

This basically sorts the array by flipping the max number to the beginning and then flipping it to the back, repeating this with the remaining array.

##Ungolfed:

array => { let flop = n => { array = [...array.slice(0, n).reverse(), ...array.slice(n)]; return n; } let flops = []; for (let i = array.length + 1; --i > 0;) { let maxIndex = array.indexOf(Math.max(...array.slice(0, i))); flops.push(flop(maxIndex + 1), flop(i)); } return flops; } 
added 395 characters in body
Source Link
mdatsev
  • 111
  • 4

JavaScript, 151 bytes

a=>{f=n=>(a=[...a.slice(0,n).reverse(),...a.slice(n)],n),r=[];for(i=a.length+1;--i>0;)r.push(f(a.indexOf(Math.max(...a.slice(0, i)))+1),f(i));return r} 

#Javascript, 151 bytes[Try it online!][TIO-jd34lh7b] [TIO-jd34lh7b]: https://tio.run/##VY5BasMwEEX3OYV2mcHyECe7GPkGpQcwXgz2yFZQJSMpIVBydsftqt18PrwH/9/4wXlMbi11iJNsXoqazcam@7YmmA7Y9ETElL0bBU46ICV5SMoCqP@QgMPOdDL90NqYwBkmL2EuS9W0de26U4uJ1ntewAKTC5M8Py18cFnoi5/wb0Q5RKwa1Bb21iYp9xRUev3em7iwMqpv9Flf9J7D4TDGkKMX8nGGo/VxzVd1VJWa4cdG3N4 "JavaScript (Node.js) – Try It Online"

a=>{f=n=>(a=[...a.slice(0,n).reverse(),...a.slice(n)],n),r=[];for(i=a.length+1;--i>0;)r.push(f(a.indexOf(Math.max(...a.slice(0, i)))+1),f(i));return r} 

#Javascript, 151 bytes

a=>{f=n=>(a=[...a.slice(0,n).reverse(),...a.slice(n)],n),r=[];for(i=a.length+1;--i>0;)r.push(f(a.indexOf(Math.max(...a.slice(0, i)))+1),f(i));return r} 

JavaScript, 151 bytes

a=>{f=n=>(a=[...a.slice(0,n).reverse(),...a.slice(n)],n),r=[];for(i=a.length+1;--i>0;)r.push(f(a.indexOf(Math.max(...a.slice(0, i)))+1),f(i));return r} 

[Try it online!][TIO-jd34lh7b] [TIO-jd34lh7b]: https://tio.run/##VY5BasMwEEX3OYV2mcHyECe7GPkGpQcwXgz2yFZQJSMpIVBydsftqt18PrwH/9/4wXlMbi11iJNsXoqazcam@7YmmA7Y9ETElL0bBU46ICV5SMoCqP@QgMPOdDL90NqYwBkmL2EuS9W0de26U4uJ1ntewAKTC5M8Py18cFnoi5/wb0Q5RKwa1Bb21iYp9xRUev3em7iwMqpv9Flf9J7D4TDGkKMX8nGGo/VxzVd1VJWa4cdG3N4 "JavaScript (Node.js) – Try It Online"

Source Link
mdatsev
  • 111
  • 4
Loading