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)) 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)