Solving this problem.
Right now, my solution entails finding the largest sorted sublist, starting from the beginning, all the way to the end. However, I feel like the way I wrote the
sorted_sublist function can definitely be improved. Besides that, I'm looking for any way to improve the way I write my code.
with open("sleepy.in", "r") as fin: n = int(next(fin)) nums = [int(i) for i in fin.readline().split()] def sorted_sublist(n, nums): for i in range(n): sublist = nums[i:n] if sublist == sorted(sublist): return sublist def main(n, nums): x = sorted_sublist(n, nums) return n - len(x) with open("sleepy.out", "w+") as fout: print(main(n, nums), file=fout)
Problem synopsis: Given a list of numbers, find the number of steps it would take to sort this list by repeatedly placing the first element of this list into another slot, provided that you do this in an optimal fashion.
4 4 3 2 1
Step 1. Place the 4 to the back of the list: 3, 2, 1, 4
Step 2. Place the 3 to the third index of the list: 2, 1, 3, 4
Step 3. Place the 2 to the second index of the list: 1, 2, 3, 4
Output: 3, as it took three optimal steps to reach a sorted list