r/dailyprogrammer • u/jnazario 2 0 • Mar 07 '18
[2018-03-07] Challenge #353 [Intermediate]
Description
I work as a waiter at a local breakfast establishment. The chef at the pancake house is sloppier than I like, and when I deliver the pancakes I want them to be sorted biggest on bottom and smallest on top. Problem is, all I have is a spatula. I can grab substacks of pancakes and flip them over to sort them, but I can't otherwise move them from the middle to the top.
How can I achieve this efficiently?
This is a well known problem called the pancake sorting problem. Bill Gates once wrote a paper on this that established the best known upper bound for 30 years.
This particular challenge is two-fold: implement the algorithm, and challenge one another for efficiency.
Input Description
You'll be given a pair of lines per input. The first line tells you how many numbers to read in the next line. The second line tells you the pancake sizes as unsigned integers. Read them in order and imagine them describing pancakes of given sizens from the top of the plate to the bottom. Example:
3
3 1 2
Output Description
Your program should emit the number of spatula flips it took to sort the pancakes from smallest to largest. Optionally show the intermediate steps. Remember, all you have is a spatula that can grab the pancakes from the 0th to the _n_th position and flip them. Example:
2 flips: 312 -> 213 -> 123
Challenge Input
8
7 6 4 2 6 7 8 7
----
8
11 5 12 3 10 3 2 5
----
10
3 12 8 12 4 7 10 3 8 10
Bonus
In a variation called the burnt pancake problem, the bottom of each pancake in the pile is burnt, and the sort must be completed with the burnt side of every pancake down. It is a signed permutation.
4
u/zqvt Mar 07 '18 edited Mar 07 '18
Clojure
naive version, so the runtime is always O(n²)
4
u/thestoicattack Mar 07 '18
Here's a simple algorithm that takes 2N flips. Since a flip is O(n) on average (reversing something about half the size of the entire stack) the entire runtime should be n2 ish. There are three insights:
So we can use a recursive process where we find the largest pancake and move it to the bottom with two flips, then sort the remaining stack not including that largest one.
C++17.
2
u/gandalfx Mar 07 '18 edited Mar 07 '18
Python 3 naive approach, O(n²)
Note: In order to follow the rules as closely as possible I implemented the
flipfunction strictly in-place. A more pythonic way would probably be to use something likexs[n::-1] + xs[n+1:]to flip the elements at indexes 0 through n.edit: failed an index boundary
Input/Output:
Explanation:
This has to search for the largest element in the unsorted part of the list n-2 times, so it's runtime approximation is O(n²). Since it has to do this exactly n-2 times the resulting number of flips is alway 2n-4, so I didn't bother outputting that.
edit: Based on this observation the bonus is always fulfilled.
Now I'll try and see if I can find a more efficient algorithm.
2
u/pancakeflippersunite Mar 11 '18
Java, I'm a beginner programmer so I'm sure it could be done much better. If you have any feedback, I would love to hear it!
2
u/MiL0101 Mar 11 '18
C# Beginner solution, feedback welcome
2
u/buddycool Mar 12 '18 edited Mar 12 '18
JAVA
OUTPUT
2
u/pheipl Mar 14 '18 edited Apr 15 '18
Java 8
I really loved this test. I haven't done a lot of programming lately and I've completely forgot how horrible 'off by 1' errors are, especially when you have a list [0 ... n] containing [1 ... n+1] and that some of the logic ties n and n+1, leading to a few index out of bounds errors.
Disclaimer
I do not approve of minimalist, unreadable code, mine is and will always be verbose.
Main
The intent is to:
a) Always look if the top pancake is the largest, then just flip everything
b) Always look to see if the pancakes aren't already ordered
c) Look for the next largest pancake from bottom to top
d) Flip it to the top
f) Flip everything over the previous largest pancake
Pancake
Here are a few results:
#1
#2
#3
2
u/discusdude14 Mar 23 '18
PowerShell:
Output:
2)
3)
2
u/ShironeShong Aug 22 '18 edited Aug 22 '18
C# implementation that only calculates the minimum amount of steps (it will not show the flips). The problem is growing exponentially with each number of flips and it calculates the first input in 4ms, the second input in 249ms, and the last input in about 16500ms. This soultion garuantees the minimum amount of steps by starting with the pancakes sorted, then queing every possible flip that can be done from the starting position and the total number of flips to reach that stage and then compare it to the starting point. If there's a match, we're done otherwise it repeats this process for all possible outcomes from the previous state.
Output 1:
7 6 4 2 6 7 8 7
8 7 6 2 4 6 7 7
7 7 6 4 2 6 7 8
6 2 4 6 7 7 7 8
4 2 6 6 7 7 7 8
2 4 6 6 7 7 7 8
Number of flips: 5
Output 2:
11 5 12 3 10 3 2 5
5 11 12 3 10 3 2 5
12 11 5 3 10 3 2 5
5 2 3 10 3 5 11 12
3 2 5 10 3 5 11 12
10 5 2 3 3 5 11 12
5 3 3 2 5 10 11 12
2 3 3 5 5 10 11 12
Number of flips: 7
Output 3:
3 12 8 12 4 7 10 3 8 10
10 7 4 12 8 12 3 3 8 10
4 7 10 12 8 12 3 3 8 10
12 8 12 10 7 4 3 3 8 10
10 8 3 3 4 7 10 12 8 12
7 4 3 3 8 10 10 12 8 12
12 10 10 8 3 3 4 7 8 12
8 7 4 3 3 8 10 10 12 12
3 3 4 7 8 8 10 10 12 12
Number of flips: 8
EDIT: I changed the code to save all past states as well and prints them out. I hade to change the integer arrays into byte arrays since I ran out of RAM after the change (it uses about 4 GB of RAM for the last challange input after I changed to byte array).
1
u/gabyjunior 1 2 Mar 07 '18 edited Mar 08 '18
C naive algorithm similar to insertion sort, with bonus.
Pancakes with the burnt side up are specified using negative numbers.
I think number of flips in the worst case is 3n-2 (when all pancakes are sorted in input but with their burnt face up).
Challenge output
2)
3)
EDIT New version with small optimization on number of flips.
1
u/Scara95 Mar 08 '18 edited Mar 08 '18
Haskell
Pretty stupid, flipping min to end so you can flip it to start of pile. Just smart enough to handle things already in position. Show intermediate steps.
edit: did not care about the actual runtime, only about counting the flips. Should be O( n2 ) anyway
output:
1
u/[deleted] Mar 08 '18 edited Mar 08 '18
Python 3
Challenge Output
NR1 Input:[7, 6, 4, 2, 6, 7, 8, 7]
NR2 Input:[11, 5, 12, 3, 10, 3, 2, 5]
NR3 Input:[3, 12, 8, 12, 4, 7, 10, 3, 8, 10]
1
u/hellajacked Mar 08 '18
C++
Took a total of 36 flips to sort all 3 stacks.
{7 6 4 2 6 7 8 7} Result: {2, 4, 6, 6, 7, 7, 7, 8} [13 Flips]
{11 5 12 3 10 3 2 5} Result: {2, 3, 3, 5, 5, 10, 11, 12} [13 Flips]
{3 12 8 12 4 7 10 3 8 10} Result: {3, 3, 4, 7, 8, 8, 10, 10, 12, 12} [13 Flips]
My code: #include <iostream> #include <vector> #include <fstream> #include <string> #include <algorithm> using namespace std;
1
u/conceptuality Mar 09 '18
Haskell: This very simple algorithm works in O(n2 ) time, with a trivial maximum of 2n flips. The heuristic is as follows:
Implementation: I wrote this once with a custom counter, but then adapted it to the writer monad, learning it along the way!
Solutions:
1
u/ff8c00 Mar 09 '18
JavaScript Gist
1
u/TiZ_EX1 Mar 09 '18
Crystal. Takes each stack as space-separated uints which means you'll have to quote each stack. Omit the number of pancakes.
I did this one by iterating through subsets from the bottom upward and potentially doing two flips per subset. If the back of the subset is already the largest number in it, then do nothing. If the largest number is in the middle, flip from that index. And then flip the entire subset.
Most of the iteration is going through the subsets and then finding the biggest number, so I guess runtime is O(n²).
1
u/octolanceae Mar 10 '18
C++11
Late to the party here, but, managed to put something together. Metal Mayhem on Mtv Classic is good for coding I think.
** Output **
Total Number of Flips: 5
Total Number of Flips: 9
Total Number of Flips: 11
1
u/Rock_Crusher Mar 11 '18
C#
This is a very naive implementation. It could really use a lot of optimization.
Output 1:
Output 2:
Output 3:
1
u/[deleted] Mar 11 '18 edited Mar 24 '18
Python 3.6
I may be late to the party, but criticism is very appreciated.
Code
Input
Output
Edit: small optimization for step efficiency
1
u/Chomatic Mar 11 '18
Scala
Short naive implementation that should be O(n2). It recursively moves the largest element to the end of the stack. This could be much faster with a better algorithm.
1
u/BlasphemousJoshua Mar 14 '18
Swift 4 and overly verbose
1
u/Xeonfobia Mar 15 '18 edited Mar 15 '18
Lua 5.2
1
u/crudkin Mar 21 '18
Late to the game, but here's a Python 3.6 solution:
1
u/zer0tonine Apr 08 '18 edited Apr 08 '18
Java
With more colors here.
The idea is quite similar to this C++ solution. Just a lot longer.
1
u/Bob_Dunbar Apr 11 '18
1
u/TN888 Apr 11 '18 edited Apr 12 '18
Java, any feedback would be amazing.
Output
A video visualizes the algorithm I've used: here
1
u/sha3bani Apr 13 '18
C++, Order(2n)
1
u/NatePfrom93 Apr 29 '18 edited Apr 29 '18
Ruby
1
u/2kofawsome Jul 04 '18 edited Jul 04 '18
! python3.6
No bonus, Im also not sure if this is the fewest flips possible, but as far as I can tell it is.
Output:
1
u/forlornsisu Aug 23 '18
Python 3
Flip count is always 2n (where n = number of pancakes).
I am still learning and trying to get better so any and all feedback is appreciated. Thanks!
1
u/cubetastic33 Aug 25 '18
Rust
I'm still a beginner to Rust, so suggestions to improve the code would be nice!
Here is a gist with the full program.
Here are the main functions:
Here is the challenge output:
1
u/TheMsDosNerd Mar 07 '18 edited Mar 07 '18
Solution in Python 3. Requires 3 flips for sorting 3 pancakes. Includes bonus.
Edit: fixed indentation and made algorithm approx 40% faster.
0
u/[deleted] Mar 24 '18
[deleted] — view deleted comment
1
u/[deleted] Mar 11 '18
[removed] — view removed comment