Cody

# Problem 42497. Pancake sorting

Solution 3658123

Submitted on 14 Nov 2020 by Nikolaos Nikolaou
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
x = 1:5; for spatula = sort_pancakes(x) x(1:spatula) = flip(x(1:spatula)); end assert(issorted(x))

stack = 1 2 3 4 5 s1 = 5 s1 = 4 s1 = 3 s1 = 2 s1 = 1 stack = 1 2 3 4 5 y = 1

2   Pass
x = [6 4 2 5 3 1]; for spatula = sort_pancakes(x) x(1:spatula) = flip(x(1:spatula)); end assert(issorted(x))

stack = 6 4 2 5 3 1 s1 = 1 stack = 1 3 5 2 4 6 y = 6 s1 = 3 stack = 5 3 1 2 4 6 y = 6 3 stack = 4 2 1 3 5 6 y = 6 3 5 s1 = 1 stack = 3 1 2 4 5 6 y = 6 3 5 4 s1 = 1 stack = 2 1 3 4 5 6 y = 6 3 5 4 3 s1 = 1 stack = 1 2 3 4 5 6 y = 6 3 5 4 3 2 s1 = 1 stack = 1 2 3 4 5 6 y = 6 3 5 4 3 2 1

3   Pass
x = randperm(12); for spatula = sort_pancakes(x) x(1:spatula) = flip(x(1:spatula)); end assert(issorted(x))

stack = 5 7 3 9 1 8 6 4 12 11 10 2 s1 = 9 stack = 12 4 6 8 1 9 3 7 5 11 10 2 y = 9 stack = 2 10 11 5 7 3 9 1 8 6 4 12 y = 9 12 s1 = 3 stack = 11 10 2 5 7 3 9 1 8 6 4 12 y = 9 12 3 stack = 4 6 8 1 9 3 7 5 2 10 11 12 y = 9 12 3 11 s1 = 10 s1 = 5 stack = 9 1 8 6 4 3 7 5 2 10 11 12 y = 9 12 3 11 5 stack = 2 5 7 3 4 6 8 1 9 10 11 12 y = 9 12 3 11 5 9 s1 = 7 stack = 8 6 4 3 7 5 2 1 9 10 11 12 y = 9 12 3 11 5 9 7 stack = 1 2 5 7 3 4 6 8 9 10 11 12 y = 9 12 3 11 5 9 7 8 s1 = 4 stack = 7 5 2 1 3 4 6 8 9 10 11 12 y = 9 12 3 11 5 9 7 8 4 stack = 6 4 3 1 2 5 7 8 9 10 11 12 y = 9 12 3 11 5 9 7 8 4 7 s1 = 1 stack = 5 2 1 3 4 6 7 8 9 10 11 12 y = 9 12 3 11 5 9 7 8 4 7 6 s1 = 1 stack = 4 3 1 2 5 6 7 8 9 10 11 12 y = 9 12 3 11 5 9 7 8 4 7 6 5 s1 = 1 stack = 2 1 3 4 5 6 7 8 9 10 11 12 y = 9 12 3 11 5 9 7 8 4 7 6 5 4 s1 = 3 s1 = 1 stack = 1 2 3 4 5 6 7 8 9 10 11 12 y = 9 12 3 11 5 9 7 8 4 7 6 5 4 2 s1 = 1 stack = 1 2 3 4 5 6 7 8 9 10 11 12 y = 9 12 3 11 5 9 7 8 4 7 6 5 4 2 1

4   Pass
x = randperm(13); for spatula = sort_pancakes(x) x(1:spatula) = flip(x(1:spatula)); end assert(issorted(x))

stack = 9 3 10 7 13 11 12 8 4 2 1 6 5 s1 = 5 stack = 13 7 10 3 9 11 12 8 4 2 1 6 5 y = 5 stack = 5 6 1 2 4 8 12 11 9 3 10 7 13 y = 5 13 s1 = 7 stack = 12 8 4 2 1 6 5 11 9 3 10 7 13 y = 5 13 7 stack = 7 10 3 9 11 5 6 1 2 4 8 12 13 y = 5 13 7 12 s1 = 5 stack = 11 9 3 10 7 5 6 1 2 4 8 12 13 y = 5 13 7 12 5 stack = 8 4 2 1 6 5 7 10 3 9 11 12 13 y = 5 13 7 12 5 11 s1 = 8 stack = 10 7 5 6 1 2 4 8 3 9 11 12 13 y = 5 13 7 12 5 11 8 stack = 9 3 8 4 2 1 6 5 7 10 11 12 13 y = 5 13 7 12 5 11 8 10 s1 = 1 stack = 7 5 6 1 2 4 8 3 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 s1 = 7 stack = 8 4 2 1 6 5 7 3 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 7 stack = 3 7 5 6 1 2 4 8 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 7 8 s1 = 2 stack = 7 3 5 6 1 2 4 8 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 7 8 2 stack = 4 2 1 6 5 3 7 8 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 7 8 2 7 s1 = 4 stack = 6 1 2 4 5 3 7 8 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 7 8 2 7 4 stack = 3 5 4 2 1 6 7 8 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 7 8 2 7 4 6 s1 = 2 stack = 5 3 4 2 1 6 7 8 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 7 8 2 7 4 6 2 stack = 1 2 4 3 5 6 7 8 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 7 8 2 7 4 6 2 5 s1 = 3 stack = 4 2 1 3 5 6 7 8 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 7 8 2 7 4 6 2 5 3 stack = 3 1 2 4 5 6 7 8 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 7 8 2 7 4 6 2 5 3 4 s1 = 1 stack = 2 1 3 4 5 6 7 8 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 7 8 2 7 4 6 2 5 3 4 3 s1 = 1 stack = 1 2 3 4 5 6 7 8 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 7 8 2 7 4 6 2 5 3 4 3 2 s1 = 1 stack = 1 2 3 4 5 6 7 8 9 10 11 12 13 y = 5 13 7 12 5 11 8 10 9 7 8 2 7 4 6 2 5 3 4 3 2 1

5   Pass
for k = 1:20 x = randperm(k); for spatula = sort_pancakes(x) x(1:spatula) = flip(x(1:spatula)); end assert(issorted(x)) end

stack = 1 s1 = 1 stack = 1 y = 1 stack = 2 1 s1 = 1 stack = 1 2 y = 2 s1 = 1 stack = 1 2 y = 2 1 stack = 3 1 2 s1 = 1 stack = 2 1 3 y = 3 s1 = 1 stack = 1 2 3 y = 3 2 s1 = 1 stack = 1 2 3 y = 3 2 1 stack = 1 2 4 3 s1 = 3 stack = 4 2 1 3 y = 3 stack = 3 1 2 4 y = 3 4 s1 = 1 stack = 2 1 3 4 y = 3 4 3 s1 = 1 stack = 1 2 3 4 y = 3 4 3 2 s1 = 1 stack = 1 2 3 4 y = 3 4 3 2 1 stack = 4 5 3 2 1 s1 = 2 stack = 5 4 3 2 1 y = 2 stack = 1 2 3 4 5 y = 2 5 s1 = 4 s1 = 3 s1 = 2 s1 = 1 stack = 1 2 3 4 5 y = 2 5 1 stack = 5 2 3 6 1 4 s1 = 4 stack = 6 3 2 5 1 4 y = 4 stack = 4 1 5 2 3 6 y = 4 6 s1 = 3 stack = 5 1 4 2 3 6 y = 4 6 3 stack = 3 2 4 1 5 6 y = 4 6 3 5 s1 = 3 stack = 4 2 3 1 5 6 y = 4 6 3 5 3 stack = 1 3 2 4 5 6 y = 4 6 3 5 3 4 s1 = 2 stack = 3 1 2 4 5 6 y = 4 6 3 5 3 4 2 stack = 2 1 3 4 5 6 y = 4 6 3 5 3 4 2 3 s1 = 1 stack = 1 2 3 4 5 6 y = 4 6 3 5 3 4 2 3 2 s1 = 1 stack = 1 2 3 4 5 6 y = 4 6 3 5 3 4 2 3 2 1 stack = 6 3 1 5 7 2 4 s1 = 5 stack = 7 5 1 3 6 2 4 y = 5 stack = 4 2 6 3 1 5 7 y = 5 7 s1 = 3 stack = 6 2 4 3 1 5 7 y = 5 7 3 stack = 5 1 3 4 2 6 7 y = 5 7 3 6 s1 = 1 stack = 2 4 3 1 5 6 7 y = 5 7 3 6 5 s1 = 2 stack = 4 2 3 1 5 6 7 y = 5 7 3 6 5 2 stack = 1 3 2 4 5 6 7 y = 5 7 3 6 5 2 4 s1 = 2 stack = 3 1 2 4 5 6 7 y = 5 7 3 6 5 2 4 2 stack = 2 1 3 4 5 6 7 y = 5 7 3 6 5 2 4 2 3 s1 = 1 stack = 1 2 3 4 5 6 7 y = 5 7 3 6 5 2 4 2 3 2 s1 = 1 stack = 1 2 3 4 5 6 7 y = 5 7 3 6 5 2 4 2 3 2 1 stack = 8 4 2 5 1 6 3 7 s1 = 1 stack = 7 3 6 1 5 2 4 8 y = 8 s1 = 1 stack = 4 2 5 1 6 3 7 8 y = 8 7 s1 = 5 stack = 6 1 5 2 4 3 7 8 y = 8 7 5 stack = 3 4 2 5 1 6 7 8 y = 8 7 5 6 s1 = 4 stack = 5 2 4 3 1 6 7 8 y = 8 7 5 6 4 stack = 1 3 4 2 5 6 7 8 y = 8 7 5 6 4 5 s1 = 3 stack = 4 3 1 2 5 6 7 8 y = 8 7 5 6 4 5 3 stack = 2 1 3 4 5 6 7 8 y = 8 7 5 6 4 5 3 4 s1 = 3 s1 = 1 stack = 1 2 3 4 5 6 7 8 y = 8 7 5 6 4 5 3 4 2 s1 = 1 stack = 1 2 3 4 5 6 7 8 y = 8 7 5 6 4 5 3 4 2 1 stack = 3 6 5 9 2 8 4 7 1 s1 = 4 stack = 9 5 6 3 2 8 4 7 1 y = 4 stack = 1 7 4 8 2 3 6 5 9 y = 4 9 s1 = 4 stack = 8 4 7 1 2 3 6 5 9 y = 4 9 4 stack = 5 6 3 2 1 7 4 8 9 y = 4 9 4 8 s1 = 6 stack = 7 1 2 3 6 5 4 8 9 y = 4 9 4 8 6 stack = 4 5 6 3 2 1 7 8 9 y = 4 9 4 8 6 7 s1 = 3 stack = 6 5 4 3 2 1 7 8 9 y = 4 9 4 8 6 7 3 stack = 1 2 3 4 5 6 7 8 9 y = 4 9 4 8 6 7 3 6 s1 = 5 s1 = 4 s1 = 3 s1 = 2 s1 = 1 stack = 1 2 3 4 5 6 7 8 9 y = 4 9 4 8 6 7 3 6 1 stack = 7 9 3 10 6 5 8 4 1 2 s1 = 4 stack = 10 3 9 7 6 5 8 4 1 2 y = 4 stack = 2 1 4 8 5 6 7 9 3 10 y = 4 10 s1 = 8 stack = 9 7 6 5 8 4 1 2 3 10 y = 4 10 8 stack = 3 2 1 4 8 5 6 7 9 10 y = 4 10 8 9 s1 = 5 stack = 8 4 1 2 3 5 6 7 9 10 y = 4 10 8 9 5 stack = 7 6 5 3 2 1 4 8 9 10 y = 4 10 8 9 5 8 s1 = 1 stack = 4 1 2 3 5 6 7 8 9 10 y = 4 10 8 9 5 8 7 s1 = 6 s1 = 5 s1 = 1 stack = 3 2 1 4 5 6 7 8 9 10 y = 4 10 8 9 5 8 7 4 s1 = 1 stack = 1 2 3 4 5 6 7 8 9 10 y = 4 10 8 9 5 8 7 4 3 s1 = 2 s1 = 1 stack = 1 2 3 4 5 6 7 8 9 10 y = 4 10 8 9 5 8 7 4 3 1 stack = 4 5 8 6 3 11 10 1 7 9 2 s1 = 6 stack = 11 3 6 8 5 4 10 1 7 9 2 y = 6 stack = 2 9 7 1 10 4 5 8 6 3 11 y = 6 11 s1 = 5 stack = 10 1 7 9 2 4 5 8 6 3 11 y = 6 11 5 stack = 3 6 8 5 4 2 9 7 1 10 11 y = 6 11 5 10 s1 = 7 stack = 9 2 4 5 8 6 3 7 1 10 11 y = 6 11 5 10 7 stack = 1 7 3 6 8 5 4 2 9 10 11 y = 6 11 5 10 7 9 s1 = 5 stack = 8 6 3 7 1 5 4 2 9 10 11 y = 6 11 5 10 7 9 5 stack = 2 4 5 1 7 3 6 8 9 10 11 y = 6 11 5 10 7 9 5 8 s1 = 5 stack = 7 1 5 4 2 3 6 8 9 10 11 y = 6 11 5 10 7 9 5 8 5 stack = 6 3 2 4 5 1 7 8 9 10 11 y = 6 11 5 10 7 9 5 8 5 7 s1 = 1 stack = 1 5 4 2 3 6 7 8 9 10 11 y = 6 11 5 10 7 9 5 8 5 7 6 s1 = 2 stack = 5 1 4 2 3 6 7 8 9 10 11 y = 6 11 5 10 7 9 5 8 5 7 6 2 stack = 3 2 4 1 5 6 7 8 9 10 11 y = 6 11 5 10 7 9 5 8 5 7 6 2 5 s1 = 3 stack = 4 2 3 1 5 6 7 8 9 10 11 y = 6 11 5 10 7 9 5 8 5 7 6 2 5 3 stack = 1 3 2 4 5 6 7 8 9 10 11 y = 6 11 5 10 7 9 5 8 5 7 6 2 5 3 4 s1 = 2 stack = 3 1 2 4 5 6 7 8 9 10 11 y = 6 11 5 10 7 9 5 8 5 7 6 2 5 3 4 2 stack = 2 1 3 4 5 6 7 8 9 10 11 y = 6 11 5 10 7 9 5 8 5 7 6 2 5 3 4 2 3 s1 = 1 stack = 1 2 3 4 5 6 7 8 9 10 11 y = 6 11 5 10 7 9 5 8 5 7 6 2 5 3 4 2 3 2 s1 = 1 stack = 1 2 3 4 5 6 7 8 9 10 11 y = 6 11 5 ...

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!