0% found this document useful (0 votes)
12 views1 page

Py Code

Uploaded by

shraddha30405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Py Code

Uploaded by

shraddha30405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

def activity_selection(start , finish):

n = len(start)
selected = [0]

last = 0
for i in range(1, n):
if start[i] >= finish[last]:
selected.append(i)
last = i

return selected

start = [1 , 3 , 0 , 5 , 7 , 5 , 10 , 12 , 17 , 20 ]
finish= [2 , 4 , 5 , 6 , 7 , 9 , 9 , 10 , 12 , 15 ]

activities = list(enumerate(zip(start , finish)))


activities.sort(key=lambda x: x[1][1])

original_indexes = [index for index , _ in activities]


start = [ s for _, (s , _) in activities]
finish = [ f for _, (_, f) in activities]

selected_local_indexes = activity_selection(start , finish)

selected_original_indexes = [ original_indexes[i] for i in selected_local_indexes]

print("Selected Activities : ")


for idx in selected_original_indexes:
print(f"Activity {idx + 1} : Start =
{start[selected_local_indexes[selected_original_indexes.index(idx)]]} , Finish =
{finish[selected_local_indexes[selected_original_indexes.index(idx)]]}")

You might also like