Cody

# Problem 42497. Pancake sorting

Solution 2450423

Submitted on 1 Jun 2020
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   Fail
x = 1:5; for spatula = sort_pancakes(x) x(1:spatula) = flip(x(1:spatula)); end assert(issorted(x))

t = 1 5 4 3 2 part1 = 1 part2 = 2 3 4 5 stack = 1 2 3 4 5 y = 1 pointer = 4 part1 = 1 2 3 4 part2 = 5 stack = 1 2 3 4 5 y = 1 4 pointer = 3 part1 = 1 2 3 part2 = 4 5 stack = 1 2 3 4 5 y = 1 4 3 pointer = 2 part1 = 1 2 part2 = 3 4 5 stack = 1 2 3 4 5 y = 1 4 3 2 pointer = 1 part1 = 1 part2 = 2 3 4 5 stack = 1 2 3 4 5 y = 1 4 3 2 1

Assertion failed.

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

t = 1 6 5 4 3 2 part1 = 6 4 2 5 3 1 part2 = 1×0 empty double row vector stack = 6 4 2 5 3 1 y = 6 pointer = 0 part1 = 1×0 empty double row vector

Array indices must be positive integers or logical values. Error in sort_pancakes (line 11) part2 = stack(pointer:end); Error in Test2 (line 2) for spatula = sort_pancakes(x)

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

t = 1 12 11 10 9 8 7 6 5 4 3 2 part1 = 12 11 6 10 4 2 1 part2 = 3 8 5 7 9 stack = 12 11 6 10 4 2 1 3 8 5 7 9 y = 7 pointer = 0 part1 = 1×0 empty double row vector

Array indices must be positive integers or logical values. Error in sort_pancakes (line 11) part2 = stack(pointer:end); Error in Test3 (line 2) for spatula = sort_pancakes(x)

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

t = 1 13 12 11 10 9 8 7 6 5 4 3 2 part1 = 9 5 13 8 6 7 1 part2 = 2 11 4 3 12 10 stack = 9 5 13 8 6 7 1 2 11 4 3 12 10 y = 7 pointer = 2 part1 = 9 5 part2 = 13 8 6 7 1 2 11 4 3 12 10 stack = 9 5 13 8 6 7 1 2 11 4 3 12 10 y = 7 2 pointer = 11 part1 = 9 5 13 8 6 7 1 2 11 4 3 part2 = 12 10 stack = 9 5 13 8 6 7 1 2 11 4 3 12 10 y = 7 2 11 pointer = 8 part1 = 9 5 13 8 6 7 1 2 part2 = 11 4 3 12 10 stack = 9 5 13 8 6 7 1 2 11 4 3 12 10 y = 7 2 11 8 pointer = 12 part1 = 9 5 13 8 6 7 1 2 11 4 3 12 part2 = 10 stack = 9 5 13 8 6 7 1 2 11 4 3 12 10 y = 7 2 11 8 12 pointer = 0 part1 = 1×0 empty double row vector

Array indices must be positive integers or logical values. Error in sort_pancakes (line 11) part2 = stack(pointer:end); Error in Test4 (line 2) for spatula = sort_pancakes(x)

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

t = 1 part1 = 1 part2 = 1×0 empty double row vector stack = 1 y = 1 t = 1 2 part1 = 1 part2 = 2 stack = 1 2 y = 1 pointer = 1 part1 = 1 part2 = 2 stack = 1 2 y = 1 1 t = 1 3 2 part1 = 3 2 1 part2 = 1×0 empty double row vector stack = 3 2 1 y = 3 pointer = 0 part1 = 1×0 empty double row vector

Array indices must be positive integers or logical values. Error in sort_pancakes (line 11) part2 = stack(pointer:end); Error in Test5 (line 3) for spatula = sort_pancakes(x)