From 3657aedac7b7af82fbdd896da85c0b4be282318d Mon Sep 17 00:00:00 2001 From: dharni0607 Date: Mon, 1 Jul 2019 17:06:06 +0530 Subject: [PATCH 1/2] add ons in string directory - Bayer_Moore_Search --- strings/Boyer_Moore_Search.py | 74 +++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 strings/Boyer_Moore_Search.py diff --git a/strings/Boyer_Moore_Search.py b/strings/Boyer_Moore_Search.py new file mode 100644 index 000000000000..dfbf0cb2e883 --- /dev/null +++ b/strings/Boyer_Moore_Search.py @@ -0,0 +1,74 @@ +""" +This algorithm tries to find the pattern in given text using Bad Character Heuristic method. +The bad-character rule considers the character in Text at which there is a mis-match. The next occurrence of that character to the left in Pattern is found, and a shift which brings that occurrence in line with the mismatched occurrence in Text is proposed. If the mismatched character does not occur to the left in Pattern, a shift is proposed that moves the entirety of Pattern past the point of mismatch + +Complexity : O(n/m) + n=length of main string + m=length of pattern string +""" + +class BoyerMooreSearch: + def __init__(self, text, pattern): + self.text, self.pattern = text, pattern + self.textLen, self.patLen = len(text), len(pattern) + + def match_In_Pattern(self, char): + + """ finds the index of char in pattern in reverse order + + Paremeters : + char (chr): character to be searched + + Returns : + i (int): index of char from last in pattern + -1 (int): if char is not found in pattern + """ + + for i in range(self.patLen-1, -1, -1): + if char == self.pattern[i]: + return i + return -1 + + + def misMatch_In_Text(self, currentPos): + + """ finds the index of mis-matched character in text when compared with pattern from last + + Paremeters : + currentPos (int): current index position of text + + Returns : + i (int): index of mismatched char from last in text + -1 (int): if there is no mis-match between pattern and text block + """ + + for i in range(self.patLen-1, -1, -1): + if self.pattern[i] != self.text[currentPos + i]: + return currentPos + i + return -1 + + def bad_Character_Heuristic(self): + + """ searches pattern in text and returns index positions """ + positions = [] + for i in range(self.textLen - self.patLen + 1): + misMatch_Index = self.misMatch_In_Text(i) + if misMatch_Index == -1: + positions.append(i) + else: + match_Index = self.match_In_Pattern(self.text[misMatch_Index]) + i = misMatch_Index - match_Index #shifting index + return positions + + +text = "ABAABA" +pattern = "AB" +bms = BoyerMooreSearch(text, pattern) +positions = bms.bad_Character_Heuristic() +if len(positions) == 0: + print("No match found") +else: + print("Pattern found in following positions: ") + print(positions) + + From 3ec9e28e60f532d88d3b881f53cbf6eac4187aea Mon Sep 17 00:00:00 2001 From: dharni0607 Date: Tue, 2 Jul 2019 16:00:17 +0530 Subject: [PATCH 2/2] created divide_and_conquer folder and added max_sub_array_sum.py under it (issue #817) --- divide_and_conquer/max_sub_array_sum.py | 72 ++++++++++++++++++++++++ strings/Boyer_Moore_Search.py | 74 ------------------------- 2 files changed, 72 insertions(+), 74 deletions(-) create mode 100644 divide_and_conquer/max_sub_array_sum.py delete mode 100644 strings/Boyer_Moore_Search.py diff --git a/divide_and_conquer/max_sub_array_sum.py b/divide_and_conquer/max_sub_array_sum.py new file mode 100644 index 000000000000..531a45abca6f --- /dev/null +++ b/divide_and_conquer/max_sub_array_sum.py @@ -0,0 +1,72 @@ +""" +Given a array of length n, max_sub_array_sum() finds the maximum of sum of contiguous sub-array using divide and conquer method. + +Time complexity : O(n log n) + +Ref : INTRODUCTION TO ALGORITHMS THIRD EDITION (section : 4, sub-section : 4.1, page : 70) + +""" + + +def max_sum_from_start(array): + """ This function finds the maximum contiguous sum of array from 0 index + + Parameters : + array (list[int]) : given array + + Returns : + max_sum (int) : maximum contiguous sum of array from 0 index + + """ + array_sum = 0 + max_sum = float("-inf") + for num in array: + array_sum += num + if array_sum > max_sum: + max_sum = array_sum + return max_sum + + +def max_cross_array_sum(array, left, mid, right): + """ This function finds the maximum contiguous sum of left and right arrays + + Parameters : + array, left, mid, right (list[int], int, int, int) + + Returns : + (int) : maximum of sum of contiguous sum of left and right arrays + + """ + + max_sum_of_left = max_sum_from_start(array[left:mid+1][::-1]) + max_sum_of_right = max_sum_from_start(array[mid+1: right+1]) + return max_sum_of_left + max_sum_of_right + + +def max_sub_array_sum(array, left, right): + """ This function finds the maximum of sum of contiguous sub-array using divide and conquer method + + Parameters : + array, left, right (list[int], int, int) : given array, current left index and current right index + + Returns : + int : maximum of sum of contiguous sub-array + + """ + + # base case: array has only one element + if left == right: + return array[right] + + # Recursion + mid = (left + right) // 2 + left_half_sum = max_sub_array_sum(array, left, mid) + right_half_sum = max_sub_array_sum(array, mid + 1, right) + cross_sum = max_cross_array_sum(array, left, mid, right) + return max(left_half_sum, right_half_sum, cross_sum) + + +array = [-2, -5, 6, -2, -3, 1, 5, -6] +array_length = len(array) +print("Maximum sum of contiguous subarray:", max_sub_array_sum(array, 0, array_length - 1)) + diff --git a/strings/Boyer_Moore_Search.py b/strings/Boyer_Moore_Search.py deleted file mode 100644 index dfbf0cb2e883..000000000000 --- a/strings/Boyer_Moore_Search.py +++ /dev/null @@ -1,74 +0,0 @@ -""" -This algorithm tries to find the pattern in given text using Bad Character Heuristic method. -The bad-character rule considers the character in Text at which there is a mis-match. The next occurrence of that character to the left in Pattern is found, and a shift which brings that occurrence in line with the mismatched occurrence in Text is proposed. If the mismatched character does not occur to the left in Pattern, a shift is proposed that moves the entirety of Pattern past the point of mismatch - -Complexity : O(n/m) - n=length of main string - m=length of pattern string -""" - -class BoyerMooreSearch: - def __init__(self, text, pattern): - self.text, self.pattern = text, pattern - self.textLen, self.patLen = len(text), len(pattern) - - def match_In_Pattern(self, char): - - """ finds the index of char in pattern in reverse order - - Paremeters : - char (chr): character to be searched - - Returns : - i (int): index of char from last in pattern - -1 (int): if char is not found in pattern - """ - - for i in range(self.patLen-1, -1, -1): - if char == self.pattern[i]: - return i - return -1 - - - def misMatch_In_Text(self, currentPos): - - """ finds the index of mis-matched character in text when compared with pattern from last - - Paremeters : - currentPos (int): current index position of text - - Returns : - i (int): index of mismatched char from last in text - -1 (int): if there is no mis-match between pattern and text block - """ - - for i in range(self.patLen-1, -1, -1): - if self.pattern[i] != self.text[currentPos + i]: - return currentPos + i - return -1 - - def bad_Character_Heuristic(self): - - """ searches pattern in text and returns index positions """ - positions = [] - for i in range(self.textLen - self.patLen + 1): - misMatch_Index = self.misMatch_In_Text(i) - if misMatch_Index == -1: - positions.append(i) - else: - match_Index = self.match_In_Pattern(self.text[misMatch_Index]) - i = misMatch_Index - match_Index #shifting index - return positions - - -text = "ABAABA" -pattern = "AB" -bms = BoyerMooreSearch(text, pattern) -positions = bms.bad_Character_Heuristic() -if len(positions) == 0: - print("No match found") -else: - print("Pattern found in following positions: ") - print(positions) - -