Cody

# Problem 42498. Pancake sorting - minimum flips.

Solution 869791

Submitted on 10 Apr 2016 by William
• Size: 90
• This is the leading solution.
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))

m = 5 m = 4 m = 3 m = 2

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))

m = 1 m = 3 m = 1 m = 1 m = 1

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

m = 2 m = 11 m = 3 m = 4 m = 4 m = 1 m = 6 m = 3 m = 3 m = 2 m = 1

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

m = 12 m = 10 m = 5 m = 3 m = 4 m = 3 m = 3 m = 2 m = 3 m = 1 m = 2 m = 1

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

m = 1 m = 1 m = 1 m = 3 m = 2 m = 1 m = 1 m = 3 m = 1 m = 1 m = 4 m = 1 m = 4 m = 1 m = 2 m = 7 m = 6 m = 1 m = 1 m = 2 m = 1 m = 4 m = 2 m = 2 m = 3 m = 1 m = 2 m = 1 m = 5 m = 1 m = 2 m = 2 m = 3 m = 3 m = 3 m = 1 m = 9 m = 3 m = 2 m = 7 m = 4 m = 5 m = 2 m = 2 m = 1 m = 5 m = 6 m = 9 m = 2 m = 6 m = 6 m = 4 m = 3 m = 2 m = 1 m = 10 m = 3 m = 2 m = 4 m = 1 m = 6 m = 2 m = 4 m = 3 m = 1 m = 1 m = 8 m = 9 m = 6 m = 8 m = 6 m = 4 m = 7 m = 3 m = 1 m = 2 m = 3 m = 2 m = 5 m = 5 m = 4 m = 5 m = 9 m = 1 m = 1 m = 5 m = 1 m = 3 m = 2 m = 3 m = 1 m = 13 m = 4 m = 5 m = 8 m = 8 m = 6 m = 8 m = 5 m = 6 m = 1 m = 2 m = 2 m = 1 m = 2 m = 9 m = 12 m = 13 m = 13 m = 9 m = 9 m = 5 m = 8 m = 6 m = 4 m = 1 m = 4 m = 3 m = 1 m = 1 m = 15 m = 6 m = 3 m = 9 m = 6 m = 9 m = 9 m = 3 m = 4 m = 6 m = 4 m = 3 m = 5 m = 4 m = 1 m = 2 m = 6 m = 2 m = 12 m = 5 m = 9 m = 4 m = 11 m = 7 m = 1 m = 9 m = 6 m = 1 m = 6 m = 3 m = 2 m = 2 m = 2 m = 16 m = 15 m = 6 m = 13 m = 10 m = 7 m = 7 m = 2 m = 3 m = 8 m = 9 m = 8 m = 7 m = 3 m = 2 m = 3 m = 1 m = 1 m = 7 m = 16 m = 12 m = 15 m = 14 m = 10 m = 5 m = 5 m = 2 m = 9 m = 9 m = 2 m = 4 m = 4 m = 3 m = 3 m = 3 m = 3 m = 1

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]);

m = 1 m = 1 m = 7 m = 1 m = 3 m = 5 m = 4 m = 1 m = 2 m = 1 m = 9 m = 8 m = 7 m = 6 m = 5 m = 4 m = 3 m = 2 m = 10 m = 9 m = 8 m = 7 m = 6 m = 5 m = 4 m = 3 m = 2 m = 5 m = 1 m = 1 m = 1 m = 1 m = 1 m = 1 m = 1 m = 1 m = 13 m = 5 m = 5 m = 6 m = 10 m = 6 m = 2 m = 7 m = 4 m = 6 m = 2 m = 2 m = 3 m = 2 m = 3 m = 9 m = 8 m = 9 m = 8 m = 8 m = 7 m = 2 m = 2 m = 6 m = 5 m = 4 m = 3 m = 2 m = 1 m = 12 m = 11 m = 9 m = 7 m = 6 m = 5 m = 10 m = 11 m = 8 m = 9 m = 2 m = 6 m = 6 m = 4 m = 1 m = 2 m = 3 m = 1 m = 2 m = 21 m = 13 m = 2 m = 10 m = 1 m = 3 m = 7 m = 6 m = 5 m = 12 m = 11 m = 3 m = 1 m = 4 m = 3 m = 3 m = 1 m = 2 m = 3 m = 1 m = 19 m = 10 m = 5 m = 20 m = 7 m = 13 m = 15 m = 14 m = 1 m = 13 m = 3 m = 8 m = 2 m = 10 m = 3 m = 4 m = 6 m = 3 m = 2 m = 2 m = 3 m = 1 m = 12 m = 11 m = 1 m = 8 m = 11 m = 16 m = 11 m = 10 m = 7 m = 11 m = 1 m = 10 m = 9 m = 10 m = 1 m = 2 m = 6 m = 1 m = 3 m = 3 m = 2 m = 1 m = 43 m = 15 m = 41 m = 60 m = 29 m = 16 m = 13 m = 66 m = 56 m = 59 m = 42 m = 13 m = 56 m = 33 m = 3 m = 31 m = 43 m = 48 m = 11 m = 16 m = 52 m = 37 m = 18 m = 38 m = 27 m = 22 m = 16 m = 37 m = 18 m = 22 m = 13 m = 42 m = 5 m = 9 m = 4 m = 8 m = 29 m = 33 m = 14 m = 23 m = 28 m = 31 m = 4 m = 29 m = 20 m = 14 m = 25 m = 16 m = 18 m = 23 m = 22 m = 19 m = 4 m = 18 m = 6 m = 2 m = 7 m = 8 m = 4 m = 5 m = 7 m = 11 m = 9 m = 3 m = 5 m = 7 m = 1 m = 4 m = 2 m = 1 m = 1 m = 1 Test 1. 10 pancakes, 8 flips Test 2. 10 pancakes, 1 flips Test 3. 10 pancakes, 0 flips Test 4. 10 pancakes, 10 flips Test 5. 15 pancakes, 22 flips Test 6. 16 pancakes, 29 flips Test 7. 20 pancakes, 34 flips Test 8. 21 pancakes, 28 flips Test 9. 23 pancakes, 36 flips Test 10. 23 pancakes, 37 flips Test 11. 73 pancakes, 136 flips

### Community Treasure Hunt

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

Start Hunting!