23
3
This challenge is related to Flipping Pancakes.
You may have heard of pancake sorting, where a stack of pancakes is sorted by size by inserting spatula into the stack and flipping all of the pancakes above the spatula, until the pancakes are sorted smallest to largest on the plate. The burnt pancake problem is slightly different. All pancakes now have one side that is burnt, and the burnt side of each pancake must face the plate once the sorting is completed.
For example, given the following stack (size of pancake on the left. 0
meaning burnt-side down and 1
meaning burnt-side up on the right):
1 0
3 1
2 1
You may flip the whole stack to get 20 30 11
, flip the top two to get 31 21 11
and flip the whole stack again to get 10 20 30
, a sorted stack of burnt pancakes. This sequence of moves, flip 3, flip 2, flip 3, could be represented as 3 2 3
.
The Challenge
- Given an array of pancakes sizes (not necessarily unique) and their orientations, output any valid burnt pancake sorting sequence, that is, a sequence of flips that leads to the stack of pancakes being sorted from smallest to largest with burnt sides down.
- Input and output may be any sane format with separators, but please specify which formats you use and state which end of your input format is the top of the stack (TOS).
- Flipping zero pancakes is allowed.
- Mixing separators in the input/output is allowed.
Test cases
For all of the following test cases, input is a list and output is a space-separated string, and TOS is on the left.
[[1, 0], [3, 1], [2, 1]]
"3 2 3"
[[5, 1], [3, 0], [4, 1], [2, 1], [1, 0]]
"5 3 4 1 3 2 1"
[[5, 1], [3, 0], [3, 0], [1, 1]]
"4 3 2 3"
As always, if anything is unclear or incorrect, please let me know in the comments. Good luck and good golfing!
2Apparently I'm an idiot. – Leaky Nun – 2016-08-28T10:37:47.060
Is
0
in the output list allowed? – Leaky Nun – 2016-08-28T10:39:48.86719@LeakyNun Flipping 0 pancakes is eminently possible. In fact, I'm doing it right now. – feersum – 2016-08-28T10:41:10.977
@daniero The top of stack is on the right side. – Leaky Nun – 2016-08-28T18:51:21.543
@LeakyNun oh sorry, my bad – daniero – 2016-08-28T20:02:11.310