From 0c2d0072f7f33ac64414f0c154295f4914373fcc Mon Sep 17 00:00:00 2001 From: Amulya Kumar Sahoo Date: Fri, 11 Oct 2019 13:22:26 +0530 Subject: [PATCH 1/2] Add kadane's algorithm --- python_implemented/kadanes_algorithm.py | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 python_implemented/kadanes_algorithm.py diff --git a/python_implemented/kadanes_algorithm.py b/python_implemented/kadanes_algorithm.py new file mode 100644 index 0000000..65073d2 --- /dev/null +++ b/python_implemented/kadanes_algorithm.py @@ -0,0 +1,30 @@ +from sys import maxsize + +def maxSubArraySum(a, size): + max_so_far = -maxsize - 1 + max_ending_here = 0 + start = 0 + end = 0 + s = 0 + + for i in range(0, size): + + max_ending_here += a[i] + + if max_so_far < max_ending_here: + max_so_far = max_ending_here + start = s + end = i + + if max_ending_here < 0: + max_ending_here = 0 + s = i + 1 + + print ("Maximum contiguous sum is %d" % (max_so_far)) + print ("Starting Index %d" % (start)) + print ("Ending Index %d" % (end)) + + +# Driver program to test maxSubArraySum +a = [-2, -3, 4, -1, -2, 1, 5, -3] +maxSubArraySum(a, len(a)) From d356f023358babddcb9707e9bca52a0a265406ce Mon Sep 17 00:00:00 2001 From: Amulya Kumar Sahoo Date: Fri, 11 Oct 2019 13:27:24 +0530 Subject: [PATCH 2/2] Remove duplicates from given string --- .../remove_duplicates_from_string.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 python_implemented/remove_duplicates_from_string.py diff --git a/python_implemented/remove_duplicates_from_string.py b/python_implemented/remove_duplicates_from_string.py new file mode 100644 index 0000000..44485ae --- /dev/null +++ b/python_implemented/remove_duplicates_from_string.py @@ -0,0 +1,46 @@ +# Python program to remove duplicate characters from an +# input string +NO_OF_CHARS = 256 + + +# Since strings in Python are immutable and cannot be changed +# This utility function will convert the string to list +def toMutable(string): + List = [] + for i in string: + List.append(i) + return List + + +# Utility function that changes list to string +def toString(List): + return ''.join(List) + + +# Function removes duplicate characters from the string +# This function work in-place and fills null characters +# in the extra space left +def removeDups(string): + bin_hash = [0] * NO_OF_CHARS + ip_ind = 0 + res_ind = 0 + temp = '' + mutableString = toMutable(string) + + # In place removal of duplicate characters + while ip_ind != len(mutableString): + temp = mutableString[ip_ind] + if bin_hash[ord(temp)] == 0: + bin_hash[ord(temp)] = 1 + mutableString[res_ind] = mutableString[ip_ind] + res_ind += 1 + ip_ind += 1 + + # After above step string is stringiittg. + # Removing extra iittg after string + return toString(mutableString[0:res_ind]) + + +# Driver program to test the above functions +string = "amulyasahoo" +print removeDups(string)