Skip to content

Commit 8366782

Browse files
swatiprajapati08cclauss
authored andcommitted
Create ActivitySelection (TheAlgorithms#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
1 parent 43f99e5 commit 8366782

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

other/activity_selection.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""The following implementation assumes that the activities
2+
are already sorted according to their finish time"""
3+
4+
"""Prints a maximum set of activities that can be done by a
5+
single person, one at a time"""
6+
# n --> Total number of activities
7+
# start[]--> An array that contains start time of all activities
8+
# finish[] --> An array that contains finish time of all activities
9+
10+
def printMaxActivities(start, finish):
11+
"""
12+
>>> start = [1, 3, 0, 5, 8, 5]
13+
>>> finish = [2, 4, 6, 7, 9, 9]
14+
>>> printMaxActivities(start, finish)
15+
The following activities are selected:
16+
0 1 3 4
17+
"""
18+
n = len(finish)
19+
print("The following activities are selected:")
20+
21+
# The first activity is always selected
22+
i = 0
23+
print(i, end=" ")
24+
25+
# Consider rest of the activities
26+
for j in range(n):
27+
28+
# If this activity has start time greater than
29+
# or equal to the finish time of previously
30+
# selected activity, then select it
31+
if start[j] >= finish[i]:
32+
print(j, end=" ")
33+
i = j
34+
35+
# Driver program to test above function
36+
start = [1, 3, 0, 5, 8, 5]
37+
finish = [2, 4, 6, 7, 9, 9]
38+
printMaxActivities(start, finish)
39+
40+
"""
41+
The following activities are selected:
42+
0 1 3 4
43+
"""

0 commit comments

Comments
 (0)