From 04a031004c97e943ea6b7df8fe52a9e45d28bd2d Mon Sep 17 00:00:00 2001 From: anubhav1306 Date: Sat, 19 Oct 2019 23:31:06 +0530 Subject: [PATCH 01/10] Insertion_sort --- sorts/i_sort.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 sorts/i_sort.py diff --git a/sorts/i_sort.py b/sorts/i_sort.py new file mode 100644 index 000000000000..8a2a4878845c --- /dev/null +++ b/sorts/i_sort.py @@ -0,0 +1,17 @@ +def insertionSort(arr): + + + for i in range(1, len(arr)): + + key = arr[i] + j = i-1 + while j >=0 and key < arr[j] : + arr[j+1] = arr[j] + j -= 1 + arr[j+1] = key +arr = [12, 11, 13, 5, 6] +insertionSort(arr) +print ("Sorted array is:") +for i in range(len(arr)): + print ("%d" %arr[i]) + From 72cf5d220837c153706224f13891d6fe1b7c6f18 Mon Sep 17 00:00:00 2001 From: anubhav1306 Date: Sat, 19 Oct 2019 23:33:27 +0530 Subject: [PATCH 02/10] largest subarray sum --- other/largest_subarray_sum.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 other/largest_subarray_sum.py diff --git a/other/largest_subarray_sum.py b/other/largest_subarray_sum.py new file mode 100644 index 000000000000..355fa59d2bf8 --- /dev/null +++ b/other/largest_subarray_sum.py @@ -0,0 +1,18 @@ +from sys import maxint +def maxSubArraySum(a, size): + + max_so_far = -maxint - 1 + max_ending_here = 0 + + for i in range(0, size): + max_ending_here = max_ending_here + a[i] + if (max_so_far < max_ending_here): + max_so_far = max_ending_here + + if max_ending_here < 0: + max_ending_here = 0 + return max_so_far + +# Driver function to check the above function +a = [-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7] +print "Maximum contiguous sum is", maxSubArraySum(a, len(a)) From e0e7ddb9eee494227988d7db6bb0168058d8967f Mon Sep 17 00:00:00 2001 From: anubhav1306 Date: Mon, 21 Oct 2019 19:10:46 +0530 Subject: [PATCH 03/10] updated print command --- other/largest_subarray_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/largest_subarray_sum.py b/other/largest_subarray_sum.py index 355fa59d2bf8..af3500e84f71 100644 --- a/other/largest_subarray_sum.py +++ b/other/largest_subarray_sum.py @@ -15,4 +15,4 @@ def maxSubArraySum(a, size): # Driver function to check the above function a = [-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7] -print "Maximum contiguous sum is", maxSubArraySum(a, len(a)) +print ("Maximum contiguous sum is", maxSubArraySum(a, len(a))) From 3a2f1afe94e1053d4414b932e46296d74a973b1e Mon Sep 17 00:00:00 2001 From: anubhav1306 Date: Mon, 21 Oct 2019 19:19:27 +0530 Subject: [PATCH 04/10] removed extraspaces --- other/largest_subarray_sum.py | 6 +++--- sorts/i_sort.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/other/largest_subarray_sum.py b/other/largest_subarray_sum.py index af3500e84f71..905e1a5d7687 100644 --- a/other/largest_subarray_sum.py +++ b/other/largest_subarray_sum.py @@ -1,7 +1,7 @@ from sys import maxint def maxSubArraySum(a, size): - max_so_far = -maxint - 1 + max_so_far= -maxint - 1 max_ending_here = 0 for i in range(0, size): @@ -14,5 +14,5 @@ def maxSubArraySum(a, size): return max_so_far # Driver function to check the above function -a = [-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7] -print ("Maximum contiguous sum is", maxSubArraySum(a, len(a))) +a=[-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7] +print("Maximum contiguous sum is", maxSubArraySum(a, len(a))) diff --git a/sorts/i_sort.py b/sorts/i_sort.py index 8a2a4878845c..c7df96b9d87c 100644 --- a/sorts/i_sort.py +++ b/sorts/i_sort.py @@ -9,9 +9,9 @@ def insertionSort(arr): arr[j+1] = arr[j] j -= 1 arr[j+1] = key -arr = [12, 11, 13, 5, 6] +arr= [12, 11, 13, 5, 6] insertionSort(arr) -print ("Sorted array is:") +print("Sorted array is:") for i in range(len(arr)): - print ("%d" %arr[i]) + print("%d" %arr[i]) From 82473728157f69ab520244d70fede15f711a90aa Mon Sep 17 00:00:00 2001 From: anubhav1306 Date: Mon, 21 Oct 2019 19:59:47 +0530 Subject: [PATCH 05/10] removed sys.maxint --- other/largest_subarray_sum.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/other/largest_subarray_sum.py b/other/largest_subarray_sum.py index 905e1a5d7687..01dbc39d4666 100644 --- a/other/largest_subarray_sum.py +++ b/other/largest_subarray_sum.py @@ -1,7 +1,7 @@ -from sys import maxint -def maxSubArraySum(a, size): +from sys import maxsize +def maxSubArraySum(a,size): - max_so_far= -maxint - 1 + max_so_far = -maxsize - 1 max_ending_here = 0 for i in range(0, size): @@ -11,8 +11,7 @@ def maxSubArraySum(a, size): if max_ending_here < 0: max_ending_here = 0 - return max_so_far - -# Driver function to check the above function -a=[-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7] -print("Maximum contiguous sum is", maxSubArraySum(a, len(a))) + return max_so_far + +a= [-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7] +print(("Maximum contiguous sum is", maxSubArraySum(a,len(a)))) From 4dd768697d86fc8146ea7c196c6bad792b93439f Mon Sep 17 00:00:00 2001 From: anubhav1306 Date: Mon, 21 Oct 2019 21:01:40 +0530 Subject: [PATCH 06/10] added explaination --- other/largest_subarray_sum.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/other/largest_subarray_sum.py b/other/largest_subarray_sum.py index 01dbc39d4666..6d9a462e98e7 100644 --- a/other/largest_subarray_sum.py +++ b/other/largest_subarray_sum.py @@ -1,5 +1,10 @@ from sys import maxsize -def maxSubArraySum(a,size): +def maxSubArraySum(a: list, size: int = 0): + """ + >>> max_sub_array_sum([-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7]) + -3 + """ + size=size or len(a) max_so_far = -maxsize - 1 max_ending_here = 0 From 25aaa4bac6d808b43dace309860f1ce197d89bc9 Mon Sep 17 00:00:00 2001 From: anubhav1306 Date: Tue, 22 Oct 2019 04:06:59 +0530 Subject: [PATCH 07/10] Updated function style --- other/largest_subarray_sum.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/other/largest_subarray_sum.py b/other/largest_subarray_sum.py index 6d9a462e98e7..97c098eb49d8 100644 --- a/other/largest_subarray_sum.py +++ b/other/largest_subarray_sum.py @@ -1,5 +1,5 @@ from sys import maxsize -def maxSubArraySum(a: list, size: int = 0): +def max_Sub_Array_Sum(a: list, size: int = 0): """ >>> max_sub_array_sum([-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7]) -3 @@ -18,5 +18,5 @@ def maxSubArraySum(a: list, size: int = 0): max_ending_here = 0 return max_so_far -a= [-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7] -print(("Maximum contiguous sum is", maxSubArraySum(a,len(a)))) +a= [-13, -3, -25, -20, 1, -16, -23, -12, -5, -22, -15, -4, -7] +print(("Maximum contiguous sum is", max_Sub_Array_Sum(a,len(a)))) From c7b4ce07396547e82f5e48c4e1eec17be7535326 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 22 Oct 2019 09:20:12 +0200 Subject: [PATCH 08/10] Update largest_subarray_sum.py --- other/largest_subarray_sum.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/other/largest_subarray_sum.py b/other/largest_subarray_sum.py index 97c098eb49d8..0449e72e64e3 100644 --- a/other/largest_subarray_sum.py +++ b/other/largest_subarray_sum.py @@ -1,22 +1,23 @@ -from sys import maxsize -def max_Sub_Array_Sum(a: list, size: int = 0): +from sys import maxsize + + +def max_sub_array_sum(a: list, size: int = 0): """ >>> max_sub_array_sum([-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7]) -3 """ - size=size or len(a) - + size = size or len(a) max_so_far = -maxsize - 1 max_ending_here = 0 - - for i in range(0, size): - max_ending_here = max_ending_here + a[i] - if (max_so_far < max_ending_here): - max_so_far = max_ending_here - - if max_ending_here < 0: - max_ending_here = 0 - return max_so_far - -a= [-13, -3, -25, -20, 1, -16, -23, -12, -5, -22, -15, -4, -7] -print(("Maximum contiguous sum is", max_Sub_Array_Sum(a,len(a)))) + for i in range(0, size): + max_ending_here = max_ending_here + a[i] + if max_so_far < max_ending_here: + max_so_far = max_ending_here + if max_ending_here < 0: + max_ending_here = 0 + return max_so_far + + +if __name__ == "__main__": + a = [-13, -3, -25, -20, 1, -16, -23, -12, -5, -22, -15, -4, -7] + print(("Maximum contiguous sum is", max_sub_array_sum(a, len(a)))) From 844b883d139f91bb999eab0286c8e2f76bb80cbb Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 22 Oct 2019 09:23:40 +0200 Subject: [PATCH 09/10] Update i_sort.py --- sorts/i_sort.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/sorts/i_sort.py b/sorts/i_sort.py index c7df96b9d87c..f6100a8d0819 100644 --- a/sorts/i_sort.py +++ b/sorts/i_sort.py @@ -1,17 +1,21 @@ -def insertionSort(arr): - - - for i in range(1, len(arr)): - - key = arr[i] - j = i-1 - while j >=0 and key < arr[j] : - arr[j+1] = arr[j] - j -= 1 - arr[j+1] = key -arr= [12, 11, 13, 5, 6] -insertionSort(arr) -print("Sorted array is:") -for i in range(len(arr)): - print("%d" %arr[i]) - +def insertionSort(arr): + """ + >>> a = arr[:] + >>> insertionSort(a) + >>> a == sorted(a) + True + """ + for i in range(1, len(arr)): + key = arr[i] + j = i - 1 + while j >= 0 and key < arr[j]: + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = key + + +arr = [12, 11, 13, 5, 6] +insertionSort(arr) +print("Sorted array is:") +for i in range(len(arr)): + print("%d" % arr[i]) From ff125d0fb5f6ea7f715f27825fb4abab707cf403 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 22 Oct 2019 09:29:12 +0200 Subject: [PATCH 10/10] Delete bogo_bogo_sort.py --- sorts/bogo_bogo_sort.py | 54 ----------------------------------------- 1 file changed, 54 deletions(-) delete mode 100644 sorts/bogo_bogo_sort.py diff --git a/sorts/bogo_bogo_sort.py b/sorts/bogo_bogo_sort.py deleted file mode 100644 index f26a46e78645..000000000000 --- a/sorts/bogo_bogo_sort.py +++ /dev/null @@ -1,54 +0,0 @@ -""" -Python implementation of bogobogosort, a "sorting algorithm -designed not to succeed before the heat death of the universe -on any sizable list" - https://en.wikipedia.org/wiki/Bogosort. - -Author: WilliamHYZhang -""" - -import random - - -def bogo_bogo_sort(collection): - """ - returns the collection sorted in ascending order - :param collection: list of comparable items - :return: the list sorted in ascending order - - Examples: - >>> bogo_bogo_sort([0, 5, 3, 2, 2]) - [0, 2, 2, 3, 5] - >>> bogo_bogo_sort([-2, -5, -45]) - [-45, -5, -2] - >>> bogo_bogo_sort([420, 69]) - [69, 420] - """ - - def is_sorted(collection): - if len(collection) == 1: - return True - - clone = collection.copy() - while True: - random.shuffle(clone) - ordered = bogo_bogo_sort(clone[:-1]) - if clone[len(clone) - 1] >= max(ordered): - break - - for i in range(len(ordered)): - clone[i] = ordered[i] - - for i in range(len(collection)): - if clone[i] != collection[i]: - return False - return True - - while not is_sorted(collection): - random.shuffle(collection) - return collection - - -if __name__ == "__main__": - user_input = input("Enter numbers separated by a comma:\n").strip() - unsorted = [int(item) for item in user_input.split(",")] - print(bogo_bogo_sort(unsorted))