From 83667826884fae7aa3dfbcf3b73aedab9922a356 Mon Sep 17 00:00:00 2001 From: Swati Prajapati <42577922+swatiprajapati08@users.noreply.github.com> Date: Sat, 19 Oct 2019 03:13:33 +0530 Subject: [PATCH] Create ActivitySelection (#1384) * Create ActivitySelection * Update and rename ActivitySelection to activity_selection.py * Update activity_selection.py * Update activity_selection.py * Update activity_selection.py * Update activity_selection.py * Update activity_selection.py * Update activity_selection.py * Rename activity_selection.py to other/activity_selection.py * Update activity_selection.py * Update activity_selection.py * Add a doctest * print(j, end=" ") * print(i, end=" ") * colons * Add trailing space --- other/activity_selection.py | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 other/activity_selection.py diff --git a/other/activity_selection.py b/other/activity_selection.py new file mode 100644 index 000000000..5c14df7d6 --- /dev/null +++ b/other/activity_selection.py @@ -0,0 +1,43 @@ +"""The following implementation assumes that the activities +are already sorted according to their finish time""" + +"""Prints a maximum set of activities that can be done by a +single person, one at a time""" +# n --> Total number of activities +# start[]--> An array that contains start time of all activities +# finish[] --> An array that contains finish time of all activities + +def printMaxActivities(start, finish): + """ + >>> start = [1, 3, 0, 5, 8, 5] + >>> finish = [2, 4, 6, 7, 9, 9] + >>> printMaxActivities(start, finish) + The following activities are selected: + 0 1 3 4 + """ + n = len(finish) + print("The following activities are selected:") + + # The first activity is always selected + i = 0 + print(i, end=" ") + + # Consider rest of the activities + for j in range(n): + + # If this activity has start time greater than + # or equal to the finish time of previously + # selected activity, then select it + if start[j] >= finish[i]: + print(j, end=" ") + i = j + +# Driver program to test above function +start = [1, 3, 0, 5, 8, 5] +finish = [2, 4, 6, 7, 9, 9] +printMaxActivities(start, finish) + +""" +The following activities are selected: +0 1 3 4 +"""