Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The second task in this assignment is to create a Python program called pancakes

ID: 3842384 • Letter: T

Question

The second task in this assignment is to create a Python program called pancakes.py that will determine the final order of a stack of pancakes after a series of flips.PYTHON 3)

Problem Task

In this problem, your input will consist of an integer n, followed by some number of other integers. You will create an initial stack of n pancakes, with pancake 1 on top and pancake n on the bottom. Then for each k in the remaining set of integers in the input, you will flip the top k pancakes on the top of the stack.

Hint: try using the Stack ADT in your solution.

Your program must read the input from an input file (see sample input provided for format), and print the final order of pancakes from top to bottom. Your program should ask for the name of the input file when it runs.

Sample Input

11 5 2 10

Sample Output

10
9
8
7
6
1
2
3
5
4
11

Explanation / Answer

import fileinput print # process each line in each of the files for line in fileinput.input(): pancakes = line.split() # Split on whitespace # convert the sequence to integers (from strings) pancakes = [int(i) for i in pancakes] n = len(pancakes) if n: # Skip over empty lines in the file. # sorted_list = sorted(pancakes) # only used when testing # print (" Sorted sequence: ",str(sorted_list).strip('[]')) print str(pancakes).strip('[]')," Flip sequence:", for bottom in range(n-1, 0, -1): max_so_far, index = pancakes[bottom], bottom for i in range(0,bottom): # find the max of # the short stack on top if pancakes[i] > max_so_far: max_so_far, index = pancakes[i], i if index != bottom: # if we have to flip anything... if index != 0: # if we have to flip something # other than the one on top... print index+1, print bottom+1, # then we flip this one # now construct the new stack by taking the relevant # pieces in the right order (after doing either one # or two flips, check that this works..., recall that # "append" adds item to the end of an array) temp = []; # start with empty array for i in range(bottom, index, -1): temp.append(pancakes[i]) for i in range(0,index): temp.append(pancakes[i]) temp.append(pancakes[index]) for i in range(bottom + 1, n): temp.append(pancakes[i]) pancakes = temp[:] # copy array back into pancakes # for the next pass print "0 " # we're done, so print out a "0"

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote