From 7a043be8d51763017f722d29ee438412bcf392a0 Mon Sep 17 00:00:00 2001 From: Swati Prajapati <42577922+swatiprajapati08@users.noreply.github.com> Date: Thu, 17 Oct 2019 11:15:50 +0530 Subject: [PATCH 01/16] Create ActivitySelection --- ActivitySelection | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 ActivitySelection diff --git a/ActivitySelection b/ActivitySelection new file mode 100644 index 000000000000..5795b2bab871 --- /dev/null +++ b/ActivitySelection @@ -0,0 +1,36 @@ +"""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 +# s[]--> An array that contains start time of all activities +# f[] --> An array that contains finish time of all activities + +def printMaxActivities(s, f ): + n = len(f) + print "The following activities are selected" + + # The first activity is always selected + i = 0 + print i, + + # Consider rest of the activities + for j in xrange(n): + + # If this activity has start time greater than + # or equal to the finish time of previously + # selected activity, then select it + if s[j] >= f[i]: + print j, + i = j + +# Driver program to test above function +s = [1, 3, 0, 5, 8, 5] +f = [2, 4, 6, 7, 9, 9] +printMaxActivities(s, f) + +OUTPUT: + +The following activities are selected +0 1 3 4 From 7a62cb145dbf29c4fe7e23353680a9fe52a20edf Mon Sep 17 00:00:00 2001 From: Swati Prajapati <42577922+swatiprajapati08@users.noreply.github.com> Date: Thu, 17 Oct 2019 20:15:17 +0530 Subject: [PATCH 02/16] Update and rename ActivitySelection to activity_selection.py --- ActivitySelection => activity_selection.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename ActivitySelection => activity_selection.py (91%) diff --git a/ActivitySelection b/activity_selection.py similarity index 91% rename from ActivitySelection rename to activity_selection.py index 5795b2bab871..32bcfcaf4dad 100644 --- a/ActivitySelection +++ b/activity_selection.py @@ -9,11 +9,11 @@ def printMaxActivities(s, f ): n = len(f) - print "The following activities are selected" + print(The following activities are selected) # The first activity is always selected i = 0 - print i, + print(i), # Consider rest of the activities for j in xrange(n): @@ -22,7 +22,7 @@ def printMaxActivities(s, f ): # or equal to the finish time of previously # selected activity, then select it if s[j] >= f[i]: - print j, + print(j) i = j # Driver program to test above function From 6c3ac742f5dbcc7a369e13f4deca3814a149a9ec Mon Sep 17 00:00:00 2001 From: Swati Prajapati <42577922+swatiprajapati08@users.noreply.github.com> Date: Thu, 17 Oct 2019 20:43:40 +0530 Subject: [PATCH 03/16] Update activity_selection.py --- activity_selection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activity_selection.py b/activity_selection.py index 32bcfcaf4dad..8f42ccf0a4ef 100644 --- a/activity_selection.py +++ b/activity_selection.py @@ -9,7 +9,7 @@ def printMaxActivities(s, f ): n = len(f) - print(The following activities are selected) + print("The following activities are selected") # The first activity is always selected i = 0 From fabebff27518be7f7c46f6c7d2773b89252b5535 Mon Sep 17 00:00:00 2001 From: Swati Prajapati <42577922+swatiprajapati08@users.noreply.github.com> Date: Thu, 17 Oct 2019 20:47:22 +0530 Subject: [PATCH 04/16] Update activity_selection.py --- activity_selection.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/activity_selection.py b/activity_selection.py index 8f42ccf0a4ef..0a4c2fe3e00c 100644 --- a/activity_selection.py +++ b/activity_selection.py @@ -30,7 +30,8 @@ def printMaxActivities(s, f ): f = [2, 4, 6, 7, 9, 9] printMaxActivities(s, f) -OUTPUT: + +OUTPUT The following activities are selected 0 1 3 4 From b932461e48c997e93487bdad03a00ec41a50b196 Mon Sep 17 00:00:00 2001 From: Swati Prajapati <42577922+swatiprajapati08@users.noreply.github.com> Date: Thu, 17 Oct 2019 20:51:07 +0530 Subject: [PATCH 05/16] Update activity_selection.py --- activity_selection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activity_selection.py b/activity_selection.py index 0a4c2fe3e00c..7eb5f0f85564 100644 --- a/activity_selection.py +++ b/activity_selection.py @@ -31,7 +31,7 @@ def printMaxActivities(s, f ): printMaxActivities(s, f) -OUTPUT +"""OUTPUT The following activities are selected -0 1 3 4 +0 1 3 4""" From 25be0891d362936925a061a1cb361f23a444b009 Mon Sep 17 00:00:00 2001 From: Swati Prajapati <42577922+swatiprajapati08@users.noreply.github.com> Date: Thu, 17 Oct 2019 20:56:05 +0530 Subject: [PATCH 06/16] Update activity_selection.py --- activity_selection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activity_selection.py b/activity_selection.py index 7eb5f0f85564..8a06eb50b31a 100644 --- a/activity_selection.py +++ b/activity_selection.py @@ -16,7 +16,7 @@ def printMaxActivities(s, f ): print(i), # Consider rest of the activities - for j in xrange(n): + for j in range(n): # If this activity has start time greater than # or equal to the finish time of previously From cd1155c5f5ae375778c5e7424a955a987a43e803 Mon Sep 17 00:00:00 2001 From: Swati Prajapati <42577922+swatiprajapati08@users.noreply.github.com> Date: Thu, 17 Oct 2019 21:03:46 +0530 Subject: [PATCH 07/16] Update activity_selection.py --- activity_selection.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/activity_selection.py b/activity_selection.py index 8a06eb50b31a..249cd84c228b 100644 --- a/activity_selection.py +++ b/activity_selection.py @@ -30,6 +30,9 @@ def printMaxActivities(s, f ): f = [2, 4, 6, 7, 9, 9] printMaxActivities(s, f) +if __name__ == "__main__": + main() + """OUTPUT From aa0fe804525ef1d0aab260785f8b74e85d5e0ab7 Mon Sep 17 00:00:00 2001 From: Swati Prajapati <42577922+swatiprajapati08@users.noreply.github.com> Date: Thu, 17 Oct 2019 21:35:53 +0530 Subject: [PATCH 08/16] Update activity_selection.py --- activity_selection.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/activity_selection.py b/activity_selection.py index 249cd84c228b..50af2aff84de 100644 --- a/activity_selection.py +++ b/activity_selection.py @@ -13,7 +13,7 @@ def printMaxActivities(s, f ): # The first activity is always selected i = 0 - print(i), + print(i) # Consider rest of the activities for j in range(n): @@ -30,11 +30,9 @@ def printMaxActivities(s, f ): f = [2, 4, 6, 7, 9, 9] printMaxActivities(s, f) -if __name__ == "__main__": - main() -"""OUTPUT -The following activities are selected + +"""The following activities are selected 0 1 3 4""" From e5688b329151883ba0bcdbb61c6e2a6bd0e85617 Mon Sep 17 00:00:00 2001 From: Swati Prajapati <42577922+swatiprajapati08@users.noreply.github.com> Date: Thu, 17 Oct 2019 23:57:02 +0530 Subject: [PATCH 09/16] Rename activity_selection.py to other/activity_selection.py --- activity_selection.py => other/activity_selection.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename activity_selection.py => other/activity_selection.py (100%) diff --git a/activity_selection.py b/other/activity_selection.py similarity index 100% rename from activity_selection.py rename to other/activity_selection.py From d8af953d9d64df32122867922dd1760156de6804 Mon Sep 17 00:00:00 2001 From: Swati Prajapati <42577922+swatiprajapati08@users.noreply.github.com> Date: Sat, 19 Oct 2019 00:48:36 +0530 Subject: [PATCH 10/16] Update activity_selection.py --- other/activity_selection.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/other/activity_selection.py b/other/activity_selection.py index 50af2aff84de..23f2095e23eb 100644 --- a/other/activity_selection.py +++ b/other/activity_selection.py @@ -4,10 +4,10 @@ """Prints a maximum set of activities that can be done by a single person, one at a time""" # n --> Total number of activities -# s[]--> An array that contains start time of all activities -# f[] --> An array that contains finish time of all activities +# start[]--> An array that contains start time of all activities +# finish[] --> An array that contains finish time of all activities -def printMaxActivities(s, f ): +def printMaxActivities(start, finish): n = len(f) print("The following activities are selected") @@ -21,14 +21,14 @@ def printMaxActivities(s, f ): # If this activity has start time greater than # or equal to the finish time of previously # selected activity, then select it - if s[j] >= f[i]: + if start[j] >= finish[i]: print(j) i = j # Driver program to test above function -s = [1, 3, 0, 5, 8, 5] -f = [2, 4, 6, 7, 9, 9] -printMaxActivities(s, f) +start = [1, 3, 0, 5, 8, 5] +finish = [2, 4, 6, 7, 9, 9] +printMaxActivities(start, finish) From eda9ed8b834bfc5968b2f4e495ffb95af2ce95ac Mon Sep 17 00:00:00 2001 From: Swati Prajapati <42577922+swatiprajapati08@users.noreply.github.com> Date: Sat, 19 Oct 2019 00:53:05 +0530 Subject: [PATCH 11/16] Update activity_selection.py --- other/activity_selection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/activity_selection.py b/other/activity_selection.py index 23f2095e23eb..e79472d0be7b 100644 --- a/other/activity_selection.py +++ b/other/activity_selection.py @@ -8,7 +8,7 @@ # finish[] --> An array that contains finish time of all activities def printMaxActivities(start, finish): - n = len(f) + n = len(finish) print("The following activities are selected") # The first activity is always selected From 32d9d3915858ebaa8100f06df672b4b3c6064970 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 18 Oct 2019 23:18:24 +0200 Subject: [PATCH 12/16] Add a doctest --- other/activity_selection.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/other/activity_selection.py b/other/activity_selection.py index e79472d0be7b..a2ab85d6ead2 100644 --- a/other/activity_selection.py +++ b/other/activity_selection.py @@ -7,7 +7,14 @@ # start[]--> An array that contains start time of all activities # finish[] --> An array that contains finish time of all activities -def printMaxActivities(start, finish): +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") @@ -30,9 +37,7 @@ def printMaxActivities(start, finish): finish = [2, 4, 6, 7, 9, 9] printMaxActivities(start, finish) - - - - -"""The following activities are selected -0 1 3 4""" +""" +The following activities are selected +0 1 3 4 +""" From 6222c04982e731a40f56a3183ad8a74ef998536d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 18 Oct 2019 23:26:46 +0200 Subject: [PATCH 13/16] print(j, end=" ") --- other/activity_selection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/activity_selection.py b/other/activity_selection.py index a2ab85d6ead2..8a9a3f040710 100644 --- a/other/activity_selection.py +++ b/other/activity_selection.py @@ -29,7 +29,7 @@ def printMaxActivities(start, finish): # or equal to the finish time of previously # selected activity, then select it if start[j] >= finish[i]: - print(j) + print(j, end=" ") i = j # Driver program to test above function From 2def0bc8c09a0c9e2f81e81c1181ded7b88f3539 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 18 Oct 2019 23:32:59 +0200 Subject: [PATCH 14/16] print(i, end=" ") --- other/activity_selection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/activity_selection.py b/other/activity_selection.py index 8a9a3f040710..add83309bf5f 100644 --- a/other/activity_selection.py +++ b/other/activity_selection.py @@ -20,7 +20,7 @@ def printMaxActivities(start, finish): # The first activity is always selected i = 0 - print(i) + print(i, end=" ") # Consider rest of the activities for j in range(n): From 4dfd35959ffa4e269e1f0e5fa5ed7045d50bbc1c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 18 Oct 2019 23:34:46 +0200 Subject: [PATCH 15/16] colons --- other/activity_selection.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/other/activity_selection.py b/other/activity_selection.py index add83309bf5f..2fd84b2a81cb 100644 --- a/other/activity_selection.py +++ b/other/activity_selection.py @@ -12,11 +12,11 @@ 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 + The following activities are selected: 0 1 3 4 """ n = len(finish) - print("The following activities are selected") + print("The following activities are selected:") # The first activity is always selected i = 0 @@ -38,6 +38,6 @@ def printMaxActivities(start, finish): printMaxActivities(start, finish) """ -The following activities are selected +The following activities are selected: 0 1 3 4 """ From 8d7a6b7ebc242527c06e0bdbf65315479b2014d3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 18 Oct 2019 23:38:31 +0200 Subject: [PATCH 16/16] Add trailing space --- other/activity_selection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/activity_selection.py b/other/activity_selection.py index 2fd84b2a81cb..5c14df7d6aa7 100644 --- a/other/activity_selection.py +++ b/other/activity_selection.py @@ -13,7 +13,7 @@ def printMaxActivities(start, finish): >>> finish = [2, 4, 6, 7, 9, 9] >>> printMaxActivities(start, finish) The following activities are selected: - 0 1 3 4 + 0 1 3 4 """ n = len(finish) print("The following activities are selected:")