Skip to main content
added 137 characters in body
Source Link
Glory2Ukraine
  • 3.2k
  • 3
  • 12
  • 33

R, 79 bytes

\(v)for(j in sum(v|1):2){v[1:w]=v[(w=which.max(v)):1];v=rev(v[-1]);cat(w,j,"")} 

Attempt This Online!

A function that takes in an unsorted array and prints out the flipping sequence to sort the array. The algorithm is the one described on Wikipedia1.

  • for(j in sum(v|1):12) run \$n-1\$ iterations, where \$n\$ is the size of the array to sort;
  • w=which.max(v) find the position of the largest number;
  • v[1:w]=v[w:1] insert the spatula between the largest pancake and the next one. Flip the upper pancakes so that the largest one gets on top;
  • v=rev(v[-1]) flip the whole heap of pancakes, so that the largest is at the bottom and remove it from the heap;
  • cat(w,j,"") output both flips.

Now, the resulting sequence will always have \$(n-1)×2\$ flips, which is unnecessary long. Some flips could be skipped, like 1 or two flips of the whole stack - both of them do not change the order of pancakes. TheFor instance, the sequence 6 5 4 1 2 3 will produce the flip sequence 1 6 5 5 4 4 1 3 2 2, in which only 6 and 3 are important - other flips could be ignored. The reason why those redundant flips do appear in the output is to have the code as short as possible.

R, 79 bytes

\(v)for(j in sum(v|1):2){v[1:w]=v[(w=which.max(v)):1];v=rev(v[-1]);cat(w,j,"")} 

Attempt This Online!

A function that takes in an unsorted array and prints out the flipping sequence to sort the array. The algorithm is the one described on Wikipedia1.

  • for(j in sum(v|1):1) run \$n-1\$ iterations, where \$n\$ is the size of the array to sort;
  • w=which.max(v) find the position of the largest number;
  • v[1:w]=v[w:1] insert the spatula between the largest pancake and the next one. Flip the upper pancakes so that the largest one gets on top;
  • v=rev(v[-1]) flip the whole heap of pancakes, so that the largest is at the bottom and remove it from the heap;
  • cat(w,j,"") output both flips.

Now, the resulting sequence will always have \$(n-1)×2\$ flips, which is unnecessary long. Some flips could be skipped, like 1 or two flips of the whole stack - both of them do not change the order of pancakes. The reason why those redundant flips do appear in the output is to have the code as short as possible.

R, 79 bytes

\(v)for(j in sum(v|1):2){v[1:w]=v[(w=which.max(v)):1];v=rev(v[-1]);cat(w,j,"")} 

Attempt This Online!

A function that takes in an unsorted array and prints out the flipping sequence to sort the array. The algorithm is the one described on Wikipedia1.

  • for(j in sum(v|1):2) run \$n-1\$ iterations, where \$n\$ is the size of the array to sort;
  • w=which.max(v) find the position of the largest number;
  • v[1:w]=v[w:1] insert the spatula between the largest pancake and the next one. Flip the upper pancakes so that the largest one gets on top;
  • v=rev(v[-1]) flip the whole heap of pancakes, so that the largest is at the bottom and remove it from the heap;
  • cat(w,j,"") output both flips.

Now, the resulting sequence will always have \$(n-1)×2\$ flips, which is unnecessary long. Some flips could be skipped, like 1 or two flips of the whole stack - both of them do not change the order of pancakes. For instance, the sequence 6 5 4 1 2 3 will produce the flip sequence 1 6 5 5 4 4 1 3 2 2, in which only 6 and 3 are important - other flips could be ignored. The reason why those redundant flips do appear in the output is to have the code as short as possible.

Source Link
Glory2Ukraine
  • 3.2k
  • 3
  • 12
  • 33

R, 79 bytes

\(v)for(j in sum(v|1):2){v[1:w]=v[(w=which.max(v)):1];v=rev(v[-1]);cat(w,j,"")} 

Attempt This Online!

A function that takes in an unsorted array and prints out the flipping sequence to sort the array. The algorithm is the one described on Wikipedia1.

  • for(j in sum(v|1):1) run \$n-1\$ iterations, where \$n\$ is the size of the array to sort;
  • w=which.max(v) find the position of the largest number;
  • v[1:w]=v[w:1] insert the spatula between the largest pancake and the next one. Flip the upper pancakes so that the largest one gets on top;
  • v=rev(v[-1]) flip the whole heap of pancakes, so that the largest is at the bottom and remove it from the heap;
  • cat(w,j,"") output both flips.

Now, the resulting sequence will always have \$(n-1)×2\$ flips, which is unnecessary long. Some flips could be skipped, like 1 or two flips of the whole stack - both of them do not change the order of pancakes. The reason why those redundant flips do appear in the output is to have the code as short as possible.