Question
Solution in Python and follow the template at the bottom Pancake Sort (due 21 Feb 2020) A cook has made a stack of pancakes on
Solution in Python and follow the template at the bottom
Pancake Sort (due 21 Feb 2020)
A cook has made a stack of pancakes on a skillet. But the pancakes are all of different sizes. Since the cook has OCD he wants to rearrange the pancakes so that they are in order - the largest pancake is on the bottom and the smallest pancake is on the top. He has a flat spatula that he can insert anywhere in the stack of pancakes and then he can flip all the pancakes on top of the spatula so that they are in reverse order. Using just this one operation of reversing a sub-stack of pancakes devise an algorithm that will allow the cook to sort the stack of pancakes in the fewest number of flips.
Your input is going to be in a file called pancakes.txt.The diameters of the pancakes are expressed as positive integers. There is one or more spaces between each integer. The number of pancakes will vary from 1 to 100. The first number represents the top of the pancake stack and the last number represents the bottom of the pancake stack.
pancakes.txt file contains this: 9 4 7 2 8 1 5 6 3
Create an algorithm to sort the stack of pancakes. You may NOT use the built-in sort() function. You may only use the built-in max() and min() functions. You may also reverse, slice and concatenate lists using the built-in Python syntax.
For this assignment you may work with a partner. Both of you must read the paper on Pair Programming and abide by the ground rules as stated in that paper. If you are working with a partner then both of you will submit the same code. In the header make sure that you have your name and your partner's name. If you are working alone then remove the fields that has the partner's name and EID.
The file that you will be turning in will be called Pancake.py. You will follow the standard coding conventions that we have discussed. Your file will have the following template:
# File: Pancake.py # Description: # Student's Name: # Student's UT EID: # Partner's Name: # Partner's UT EID: # Course Name: CS 313E # Unique Number: # Date Created: # Date Last Modified: # Input: pancakes is a list of positive integers # Output: a list of the pancake stack each time you # have done a flip with the spatula # this is a list of lists # the last item in this list is the sorted stack def sort_pancakes ( pancakes ): every_flip = [] return every_flip # return a list of flipped pancake stacks def main(): # open the file pancakes.txt for reading in_file = open ("./pancakes.txt", "r") line = in_file.readline() line = line.strip() line = line.split() print (line) pancakes = [] for item in line: pancakes.append (int(item)) # print content of list before flipping print ("Initial Order of Pancakes = ", pancakes) # call the function to sort the pancakes every_flip = sort_pancakes ( pancakes ) # print the contents of the pancake stack after # every flip for i in range (len(every_flip)): print (every_flip[i]) # print content of list after all the flipping print ("Final Order of Pancakes = ", every_flip[-1]) if __name__ == "__main__": main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started