Skip to main content
added explanation
Source Link
ETHproductions
  • 50.3k
  • 6
  • 96
  • 241

JavaScript (ES6), 64 bytes

f=([n,...a],z=[],q=[n,...z])=>a+a?n<a[0]?[...q,...f(a)]:f(a,q):q 

Recursion FTW! The basic algorithm in use here is to keep track of the current non-ascending run in an array, "returning" it whenever an ascending element is found. We do this recursively, continually concatenating the results, until we run out of items. By creating each run in reverse ([n,...z] instead of [...z,n]), we can avoid the lengthy .reverse() at no cost.

Test snippet

f=([n,...a],z=[],q=[n,...z])=>a+a?n<a[0]?[...q,...f(a)]:f(a,q):q g=a=>console.log("Input:",`[${a}]`,"Output:",`[${f(a)}]`) g([1]) g([1,1]) g([1,2]) g([2,1]) g([3,2,1]) g([3,1,2]) g([3,1,1,2]) g([5,2,7,6,4,1,3])
<input id=I value="5, 2, 7, 6, 4, 1, 3"> <button onclick="g((I.value.match(/\d+/g)||[]).map(Number))">Run</button>

JavaScript (ES6), 64 bytes

f=([n,...a],z=[],q=[n,...z])=>a+a?n<a[0]?[...q,...f(a)]:f(a,q):q 

Recursion FTW!

Test snippet

f=([n,...a],z=[],q=[n,...z])=>a+a?n<a[0]?[...q,...f(a)]:f(a,q):q g=a=>console.log("Input:",`[${a}]`,"Output:",`[${f(a)}]`) g([1]) g([1,1]) g([1,2]) g([2,1]) g([3,2,1]) g([3,1,2]) g([3,1,1,2]) g([5,2,7,6,4,1,3])
<input id=I value="5, 2, 7, 6, 4, 1, 3"> <button onclick="g((I.value.match(/\d+/g)||[]).map(Number))">Run</button>

JavaScript (ES6), 64 bytes

f=([n,...a],z=[],q=[n,...z])=>a+a?n<a[0]?[...q,...f(a)]:f(a,q):q 

Recursion FTW! The basic algorithm in use here is to keep track of the current non-ascending run in an array, "returning" it whenever an ascending element is found. We do this recursively, continually concatenating the results, until we run out of items. By creating each run in reverse ([n,...z] instead of [...z,n]), we can avoid the lengthy .reverse() at no cost.

Test snippet

f=([n,...a],z=[],q=[n,...z])=>a+a?n<a[0]?[...q,...f(a)]:f(a,q):q g=a=>console.log("Input:",`[${a}]`,"Output:",`[${f(a)}]`) g([1]) g([1,1]) g([1,2]) g([2,1]) g([3,2,1]) g([3,1,2]) g([3,1,1,2]) g([5,2,7,6,4,1,3])
<input id=I value="5, 2, 7, 6, 4, 1, 3"> <button onclick="g((I.value.match(/\d+/g)||[]).map(Number))">Run</button>

added snippet
Source Link
ETHproductions
  • 50.3k
  • 6
  • 96
  • 241

JavaScript (ES6), 64 bytes

f=([n,...a],z=[],q=[n,...z])=>a+a?n<a[0]?[...q,...f(a)]:f(a,q):q 

Recursion FTW!

Test snippet

f=([n,...a],z=[],q=[n,...z])=>a+a?n<a[0]?[...q,...f(a)]:f(a,q):q g=a=>console.log("Input:",`[${a}]`,"Output:",`[${f(a)}]`) g([1]) g([1,1]) g([1,2]) g([2,1]) g([3,2,1]) g([3,1,2]) g([3,1,1,2]) g([5,2,7,6,4,1,3])
<input id=I value="5, 2, 7, 6, 4, 1, 3"> <button onclick="g((I.value.match(/\d+/g)||[]).map(Number))">Run</button>

JavaScript (ES6), 64 bytes

f=([n,...a],z=[],q=[n,...z])=>a+a?n<a[0]?[...q,...f(a)]:f(a,q):q 

Recursion FTW!

JavaScript (ES6), 64 bytes

f=([n,...a],z=[],q=[n,...z])=>a+a?n<a[0]?[...q,...f(a)]:f(a,q):q 

Recursion FTW!

Test snippet

f=([n,...a],z=[],q=[n,...z])=>a+a?n<a[0]?[...q,...f(a)]:f(a,q):q g=a=>console.log("Input:",`[${a}]`,"Output:",`[${f(a)}]`) g([1]) g([1,1]) g([1,2]) g([2,1]) g([3,2,1]) g([3,1,2]) g([3,1,1,2]) g([5,2,7,6,4,1,3])
<input id=I value="5, 2, 7, 6, 4, 1, 3"> <button onclick="g((I.value.match(/\d+/g)||[]).map(Number))">Run</button>

Source Link
ETHproductions
  • 50.3k
  • 6
  • 96
  • 241

JavaScript (ES6), 64 bytes

f=([n,...a],z=[],q=[n,...z])=>a+a?n<a[0]?[...q,...f(a)]:f(a,q):q 

Recursion FTW!