forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivity_selection.py
More file actions
46 lines (35 loc) · 1.3 KB
/
Copy pathactivity_selection.py
File metadata and controls
46 lines (35 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
Author: OMKAR PATHAK
Created On: 26th August 2017
"""
import inspect
# TODO: Explain what this is / how it works
def activity_selection(start_times, finish_times):
"""
:param start_times: An array that contains start time of all activities
:param finish_times: An array that conatins finish time of all activities
"""
if type(start_times) is not list:
raise TypeError("Activity selection problem only accepts lists, not {}".format(str(type(start_times))))
if type(finish_times) is not list:
raise TypeError("Activity selection problem only accepts lists, not {}".format(str(type(finish_times))))
if len(start_times) != len(finish_times):
raise ValueError('Length of start_times list and finish_times list must be same')
n = len(start_times)
activity = []
# first activity is also selected
i = 0
activity.append(i)
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_times[j] >= finish_times[i]:
activity.append(j)
i = j
return activity
def get_code():
"""
returns the code for the activity_selection function
"""
return inspect.getsource(activity_selection)