Skip to main content
added 1349 characters in body
Source Link

Jelly, 9 bytes

Œ!IṠIAƑƲƇ 

Try it online!

How it works

Œ!IṠIAƑƲƇ - Main link. Takes an integer n on the left Œ! - Permutations of [1,2,...,n] ƲƇ - Keep those permutations for which the following is true: I - Forward increments Ṡ - Signs I - Forward increments AƑ - All non-negative? 

To see why this works, take four permutations for \$n = 4\$:

  • [2,1,3,4] which is a prepend-append permutation
  • [3,2,1,4] which is a prepend-append permutation
  • [3,1,4,2] which is not
  • [1,2,4,3] which is not

Let's work through each of the 4 commands (I, , I and ) to see the difference. Each command is applied sequentially to the result of the previous one:

Command[2,1,3,4][3,2,1,4][3,1,4,2][1,2,4,3]
I[-1,2,1][-1,-1,3][-2,3,-2][1,2,-1]
[-1,1,1][-1,-1,1][-1,1,-1][1,1,-1]
I[2,0][0,2][2,-2][0,-2]
1100

It turns out that always IṠI yields an array consisting of 2, 0 or -2, and that if a permutation does not contain -2 after IṠI is applied, the permutation is a prepend-append permutation.

Jelly, 9 bytes

Œ!IṠIAƑƲƇ 

Try it online!

Jelly, 9 bytes

Œ!IṠIAƑƲƇ 

Try it online!

How it works

Œ!IṠIAƑƲƇ - Main link. Takes an integer n on the left Œ! - Permutations of [1,2,...,n] ƲƇ - Keep those permutations for which the following is true: I - Forward increments Ṡ - Signs I - Forward increments AƑ - All non-negative? 

To see why this works, take four permutations for \$n = 4\$:

  • [2,1,3,4] which is a prepend-append permutation
  • [3,2,1,4] which is a prepend-append permutation
  • [3,1,4,2] which is not
  • [1,2,4,3] which is not

Let's work through each of the 4 commands (I, , I and ) to see the difference. Each command is applied sequentially to the result of the previous one:

Command[2,1,3,4][3,2,1,4][3,1,4,2][1,2,4,3]
I[-1,2,1][-1,-1,3][-2,3,-2][1,2,-1]
[-1,1,1][-1,-1,1][-1,1,-1][1,1,-1]
I[2,0][0,2][2,-2][0,-2]
1100

It turns out that always IṠI yields an array consisting of 2, 0 or -2, and that if a permutation does not contain -2 after IṠI is applied, the permutation is a prepend-append permutation.

Source Link

Jelly, 9 bytes

Œ!IṠIAƑƲƇ 

Try it online!