Question: In python, I have a greedy algorithm I am trying to make happen but it's not working. I have the sorted list of lists array

In python, I have a greedy algorithm I am trying to make happen but it's not working. I have the sorted list of lists array as follows:

1 1 4 2 3 5 3 0 6 4 5 7 5 3 9 6 5 9 7 6 10 8 8 11 9 8 12 10 2 14 11 12 16

The first number is the identifier, the second is start and the third is the finish. So when returning the correct answer, it should just return the first number to identify it. The problem I have is, what I am returning is leaving out the first identifier to the answer. For example, the above should return 2, 4, 8, 11 (after everything is sorted of course - the sorted list is [[1, 1, 4], [2, 3, 5], [3, 0, 6], [4, 5, 7], [6, 5, 9], [5, 3, 9], [7, 6, 10], [8, 8, 11], [9, 8, 12], [10, 2, 14], [11, 12, 16]]). But I am returning 4, 8, 11.

The following is my code for the greedy algorithm:

 def greedy(a, list): #ignore 'a' for now, that's for something else mergeSort(list) n = len(list) k = 0 s = [] for i in range(0, n): if list[i][1] >= list[k][2]: # print('i,', list[i][1]) # print('k,', list[k][2]) s.append(list[i][0]) k = i # print(k) print(s)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!