Cody

# Problem 42498. Pancake sorting - minimum flips.

Solution 1940594

Submitted on 18 Sep 2019 by Augusto Mazzei
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

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

st = 4 3 2 1 5 y = 4 4 st = 1 2 3 4 5 st = 3 2 1 4 5 y = 4 4 3 3 st = 1 2 3 4 5 st = 2 1 3 4 5 y = 4 4 3 3 2 2 st = 1 2 3 4 5

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

y = 6 st = 1 3 5 2 4 6 st = 5 3 1 2 4 6 y = 6 3 5 st = 4 2 1 3 5 6 y = 6 3 5 4 st = 3 1 2 4 5 6 y = 6 3 5 4 3 st = 2 1 3 4 5 6 y = 6 3 5 4 3 2 st = 1 2 3 4 5 6

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

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

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

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

6   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

st = 2 1 3 y = 2 2 st = 1 2 3 st = 4 3 1 2 y = 2 4 st = 2 1 3 4 st = 3 1 2 4 y = 2 4 3 3 st = 2 1 3 4 y = 2 4 3 3 2 st = 1 2 3 4 y = 5 st = 1 4 2 3 5 st = 4 1 2 3 5 y = 5 2 4 st = 3 2 1 4 5 y = 5 2 4 3 st = 1 2 3 4 5 st = 2 1 3 4 5 y = 5 2 4 3 2 2 st = 1 2 3 4 5 st = 6 3 4 1 2 5 y = 2 6 st = 5 2 1 4 3 6 y = 2 6 5 st = 3 4 1 2 5 6 st = 4 3 1 2 5 6 y = 2 6 5 2 4 st = 2 1 3 4 5 6 st = 3 1 2 4 5 6 y = 2 6 5 2 4 3 3 st = 2 1 3 4 5 6 y = 2 6 5 2 4 3 3 2 st = 1 2 3 4 5 6 st = 7 4 2 3 1 5 6 y = 3 7 st = 6 5 1 3 2 4 7 y = 3 7 6 st = 4 2 3 1 5 6 7 st = 5 1 3 2 4 6 7 y = 3 7 6 5 5 st = 4 2 3 1 5 6 7 y = 3 7 6 5 5 4 st = 1 3 2 4 5 6 7 st = 3 1 2 4 5 6 7 y = 3 7 6 5 5 4 2 3 st = 2 1 3 4 5 6 7 y = 3 7 6 5 5 4 2 3 2 st = 1 2 3 4 5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1 3 5 2 4 6 7 8 9 10 st = 6 4 2 5 3 1 7 8 9 10 y = 6 10 7 9 6 8 5 7 6 6 st = 1 3 5 2 4 6 7 8 9 10 st = 5 3 1 2 4 6 7 8 9 10 y = 6 10 7 9 6 8 5 7 6 6 3 5 st = 4 2 1 3 5 6 7 8 9 10 y = 6 10 7 9 6 8 5 7 6 6 3 5 4 st = 3 1 2 4 5 6 7 8 9 10 y = 6 10 7 9 6 8 5 7 6 6 3 5 4 3 st = 2 1 3 4 5 6 7 8 9 10 y = 6 10 7 9 6 8 5 7 6 6 3 5 4 3 2 st = 1 2 3 4 5 6 7 8 9 10 st = 11 8 4 6 3 5 2 7 1 10 9 y = 7 11 st = 9 10 1 7 2 5 3 6 4 8 11 st = 10 9 1 7 2 5 3 6 4 8 11 y = 7 11 2 10 st = 8 4 6 3 5 2 7 1 9 10 11 st = 9 1 7 2 5 3 6 4 8 10 11 y = 7 11 2 10 9 9 st = 8 4 6 3 5 2 7 1 9 10 11 y = 7 11 2 10 9 9 8 st = 1 7 2 5 3 6 4 8 9 10 11 st = 7 1 2 5 3 6 4 8 9 10 11 y = 7 11 2 10 9 9 8 2 7 st = 4 6 3 5 2 1 7 8 9 10 11 st = 6 4 3 5 2 1 7 8 9 10 11 y = 7 11 2 10 9 9 8 2 7 2 6 st = 1 2 5 3 4 6 7 8 9 10 11 st = 5 2 1 3 4 6 7 8 9 10 11 y = 7 11 2 10 9 9 8 2 7 2 6 3 5 st = 4 3 1 2 5 6 7 8 9 10 11 y = 7 11 2 10 9 9 8 2 7 2 6 3 5 4 st = 2 1 3 4 5 6 7 8 9 10 11 st = 3 1 2 4 5 6 7 8 9 10 11 y = 7 11 2 10 9 9 8 2 7 2 6 3 5 4 3 3 st = 2 1 3 4 5 6 7 8 9 10 11 y = 7 11 2 10 9 9 8 2 7 2 6 3 5 4 3 3 2 st = 1 2 3 4 5 6 7 8 9 10 11 st = 12 3 6 10 1 11 8 9 5 2 4 7 y = 2 12 st = 7 4 2 5 9 8 11 1 10 6 3 12 st = 11 8 9 5 2 4 7 1 10 6 3 12 y = 2 12 7 11 st = 3 6 10 1 7 4 2 5 9 8 11 12 st = 10 6 3 1 7 4 2 5 9 8 11 12 y = 2 12 7 11 3 10 st = 8 9 5 2 4 7 1 3 6 10 11 12 st = 9 8 5 2 4 7 1 3 6 10 11 12 y = 2 12 7 11 3 10 2 9 st = 6 3 1 7 4 2 5 8 9 10 11 12 st = 8 5 2 4 7 1 3 6 9 10 11 12 y = 2 12 7 11 3 10 2 9 8 8 st = 6 3 1 7 4 2 5 8 9 10 11 12 st = 7 1 3 6 4 2 5 8 9 10 11 12 y = 2 12 7 11 3 10 2 9 8 8 4 7 st = 5 2 4 6 3 1 7 8 9 10 11 12 st = 6 4 2 5 3 1 7 8 9 10 11 12 y = 2 12 7 11 3 10 2 9 8 8 4 7 4 6 st = 1 3 5 2 4 6 7 8 9 10 11 12 st = 5 3 1 2 4 6 7 8 9 10 11 12 y = 2 12 7 11 3 10 2 9 8 8 4 7 4 6 3 5 st = 4 2 1 3 5 6 7 8 9 10 11 12 y = 2 12 7 11 3 10 2 9 8 8 4 7 4 6 3 5 4 st = 3 1 2 4 5 6 7 8 9 10 11 12 y = 2 12 7 11 3 10 2 9 8 8 4 7 4 6 3 5 4 3 st = 2 ...

7   Pass
% this test may be extended and rescored sets = { [10 3 2 1 6 5 4 8 7 9] [10:-1:1] [1:10] [2:2:10 1:2:9] [ 9 8 14 5 12 1 10 13 2 7 6 3 15 4 11] [ 7 8 16 13 11 1 9 15 2 6 3 12 10 4 14 5] [ 15 14 19 1 9 4 12 3 16 7 13 20 6 2 11 10 17 5 8 18] [ 2 15 18 4 8 16 10 6 3 14 1 7 20 9 11 12 13 5 19 17 21] [ 5 1 9 3 18 22 17 15 11 16 10 13 2 21 20 6 12 7 23 8 19 14 4] [ 3 9 18 15 7 8 6 10 11 17 21 23 22 1 14 5 2 13 16 12 20 4 19] [ 34 71 51 31 55 61 18 26 59 8 14 6 70 1 25 7 9 4 44 39 19 56 22 32 27 2 11 28 68 57 49 48 20 29 13 10 17 40 3 37 45 63 73 30 43 42 38 21 23 46 62 47 58 69 24 65 60 36 72 64 54 33 5 52 67 66 15 53 12 35 41 16 50]}; for k = 1:numel(sets) x = sets{k}; queue = sort_pancakes(x); for spatula = queue x(1:spatula) = flip(x(1:spatula)); end assert(issorted(x)) n(k) = numel(x); score(k) = numel(queue); end SetSolutionScore(sum(score)); fprintf('\nTest %3d. %3d pancakes, %3d flips',[1:numel(sets); n; score]);

y = 10 st = 9 7 8 4 5 6 1 2 3 10 y = 10 9 st = 3 2 1 6 5 4 8 7 9 10 st = 8 4 5 6 1 2 3 7 9 10 y = 10 9 7 8 st = 7 3 2 1 6 5 4 8 9 10 y = 10 9 7 8 7 st = 4 5 6 1 2 3 7 8 9 10 st = 6 5 4 1 2 3 7 8 9 10 y = 10 9 7 8 7 3 6 st = 3 2 1 4 5 6 7 8 9 10 st = 5 4 1 2 3 6 7 8 9 10 y = 10 9 7 8 7 3 6 5 5 st = 3 2 1 4 5 6 7 8 9 10 st = 4 1 2 3 5 6 7 8 9 10 y = 10 9 7 8 7 3 6 5 5 4 4 st = 3 2 1 4 5 6 7 8 9 10 y = 10 9 7 8 7 3 6 5 5 4 4 3 st = 1 2 3 4 5 6 7 8 9 10 st = 2 1 3 4 5 6 7 8 9 10 y = 10 9 7 8 7 3 6 5 5 4 4 3 2 2 st = 1 2 3 4 5 6 7 8 9 10 y = 10 st = 1 2 3 4 5 6 7 8 9 10 st = 9 8 7 6 5 4 3 2 1 10 y = 10 9 9 st = 1 2 3 4 5 6 7 8 9 10 st = 8 7 6 5 4 3 2 1 9 10 y = 10 9 9 8 8 st = 1 2 3 4 5 6 7 8 9 10 st = 7 6 5 4 3 2 1 8 9 10 y = 10 9 9 8 8 7 7 st = 1 2 3 4 5 6 7 8 9 10 st = 6 5 4 3 2 1 7 8 9 10 y = 10 9 9 8 8 7 7 6 6 st = 1 2 3 4 5 6 7 8 9 10 st = 5 4 3 2 1 6 7 8 9 10 y = 10 9 9 8 8 7 7 6 6 5 5 st = 1 2 3 4 5 6 7 8 9 10 st = 4 3 2 1 5 6 7 8 9 10 y = 10 9 9 8 8 7 7 6 6 5 5 4 4 st = 1 2 3 4 5 6 7 8 9 10 st = 3 2 1 4 5 6 7 8 9 10 y = 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 st = 1 2 3 4 5 6 7 8 9 10 st = 2 1 3 4 5 6 7 8 9 10 y = 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 st = 1 2 3 4 5 6 7 8 9 10 st = 9 8 7 6 5 4 3 2 1 10 y = 9 9 st = 1 2 3 4 5 6 7 8 9 10 st = 8 7 6 5 4 3 2 1 9 10 y = 9 9 8 8 st = 1 2 3 4 5 6 7 8 9 10 st = 7 6 5 4 3 2 1 8 9 10 y = 9 9 8 8 7 7 st = 1 2 3 4 5 6 7 8 9 10 st = 6 5 4 3 2 1 7 8 9 10 y = 9 9 8 8 7 7 6 6 st = 1 2 3 4 5 6 7 8 9 10 st = 5 4 3 2 1 6 7 8 9 10 y = 9 9 8 8 7 7 6 6 5 5 st = 1 2 3 4 5 6 7 8 9 10 st = 4 3 2 1 5 6 7 8 9 10 y = 9 9 8 8 7 7 6 6 5 5 4 4 st = 1 2 3 4 5 6 7 8 9 10 st = 3 2 1 4 5 6 7 8 9 10 y = 9 9 8 8 7 7 6 6 5 5 4 4 3 3 st = 1 2 3 4 5 6 7 8 9 10 st = 2 1 3 4 5 6 7 8 9 10 y = 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 st = 1 2 3 4 5 6 7 8 9 10 st = 10 8 6 4 2 1 3 5 7 9 y = 5 10 st = 9 7 5 3 1 2 4 6 8 10 y = 5 10 9 st = 8 6 4 2 1 3 5 7 9 10 y = 5 10 9 8 st = 7 5 3 1 2 4 6 8 9 10 y = 5 10 9 8 7 st = 6 4 2 1 3 5 7 8 9 10 y = 5 10 9 8 7 6 st = 5 3 1 2 4 6 7 8 9 10 y = 5 10 9 8 7 6 5 st = 4 2 1 3 5 6 7 8 9 10 y = 5 10 9 8 7 6 5 4 st = 3 1 2 4 5 6 7 8 9 10 y = 5 10 9 8 7 6 5 4 3 st = 2 1 3 4 5 6 7 8 9 10 y = 5 10 9 8 7 6 5 4 3 2 st = 1 2 3 4 5 6 7 8 9 10 st = 15 3 6 7 2 13 10 1 12 5 14 8 9 4 11 y = 13 15 st = 11 4 9 8 14 5 12 1 10 13 2 7 6 3 15 st = 14 8 9 4 11 5 12 1 10 13 2 7 6 3 15 y = 13 15 5 14 st = 3 6 7 2 13 10 1 12 5 11 4 9 8 14 15 st = 13 2 7 6 3 10 1 12 5 11 4 9 8 14 15 y = 13 15 5 14 5 13 st = 8 9 4 11 5 12 1 10 3 6 7 2 13 14 15 st = 12 5 11 4 9 8 1 10 3 6 7 2 13 14 15 y = 13 15 5 14 5 13 6 12 st = 2 7 6 3 10 1 8 9 4 11 5 12 13 14 15 st = 11 4 9 8 1 10 3 6 7 2 5 12 13 14 15 y = 13 15 5 14 5 13 6 12 10 11 st = 5 2 7 6 3 10 1 8 9 4 11 12 13 14 15 st = 10 3 6 7 2 5 1 8 9 4 11 12 13 14 15 y = 13 15 5 14 5 13 6 12 10 11 6 10 st = 4 9 8 1 5 2 7 6 3 10 11 12 13 14 15 st = 9 4 8 1 5 2 7 6 3 10 11 12 13 14 15 y = 13 15 5 14 5 13 6 12 10 11 6 10 2 9 st = 3 6 7 2 5 1 8 4 9 10 11 12 13 14 15 st = 8 1 5 2 7 6 3 4 9 10 11 12 13 14 15 y = 13 15 5 14 5 13 6 12 10 11 6 10 2 9 7 8 st = 4 3 6 7 2 5 1 8 9 10 11 12 13 14 15 st = 7 6 3 4 2 5 1 8 9 10 11 12 13 14 15 y = 13 15 5 14 5 13 6 12 10 11 6 10 2 9 7 8 4 7 st = 1 5 2 4 3 6 7 8 9 10 11 12 13 14 15 st = 6 3 4 2 5 1 7 8 9 10 11 12 13 14 15 y = 13 15 5 14 5 13 6 12 10 11 6 10 2 9 7 8 4 7 6 6 st = 1 5 2 4 3 6 7 8 9 10 11 12 13 14 15 st = 5 1 2 4 3 6 7 8 9 10 11 12 13 14 15 y = 13 15 5 14 5 13 6 12 10 11 6 10 2 9 7 8 4 7 6 6 2 5 st = 3 4 2 1 5 6 7 8 9 10 11 12 13 14 15 st = 4 3 2 1 5 6 7 8 9 10 11 12 13 14 15 y = 13 15 5 14 5 13 6 12 10 11 6 10 2 9 7 8 4 7 6 6 2 5 2 4 st = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 st = 3 2 1 4 5 6 7 8 9 10 11 12 13 14 15 y = 13 15 5 14 5 13 6 12 10 11 6 10 2 9 7 8 4 7 6 6 2 5 2 4 3 3 st = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 st = 2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 y = 13 15 5 14 5 13 6 12 10 11 6 10 2 9 7 8 4 7 6 6 2 5 2 4 3 3 2 2 st = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 st = 1...

### Community Treasure Hunt

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

Start Hunting!