From 290ae2341d76433d77730e2853c174adb96c16e5 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sat, 14 Oct 2023 09:20:20 +0530 Subject: [PATCH 1/6] To add wildcard_matching.py --- dynamic_programming/wildcard_matching.py | 64 ++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 dynamic_programming/wildcard_matching.py diff --git a/dynamic_programming/wildcard_matching.py b/dynamic_programming/wildcard_matching.py new file mode 100644 index 000000000000..be0c78cb7549 --- /dev/null +++ b/dynamic_programming/wildcard_matching.py @@ -0,0 +1,64 @@ +""" +Given two strings, an input string and a pattern, +this program checks if the input string matches the pattern. + +Example : +input_string = "baaabab" +pattern = "*****ba*****ab" +Output: True + +This problem can be solved using the concept of "DYNAMIC PROGRAMMING". + +We create a 2D boolean matrix, where each entry match_matrix[i][j] is True +if the first i characters in input_string match the first j characters +of pattern. We initialize the first row and first column based on specific +rules, then fill up the rest of the matrix using a bottom-up dynamic +programming approach. + +The amount of match that will be determined is equal to match_matrix[n][m] +where n and m are lengths of the input_string and pattern respectively. + +""" + + +def is_pattern_match(input_string : str, pattern : str) -> bool: + """ + is_pattern_match("baaabab","*****ba*****ba") + >>> False + is_pattern_match("baaabab","*****ba*****ab") + >>> True + is_pattern_match("aa","*") + >>> True + + """ + input_length = len(input_string) + pattern_length = len(pattern) + + match_matrix = [[False for i in range(pattern_length + 1)] for j in range(input_length + 1)] + + match_matrix[0][0] = True + + for i in range(1, input_length + 1): + match_matrix[i][0] = False + + for j in range(1, pattern_length + 1): + if pattern[j - 1] == '*': + match_matrix[0][j] = match_matrix[0][j - 1] + + for i in range(1, input_length + 1): + for j in range(1, pattern_length + 1): + if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == '?': + match_matrix[i][j] = match_matrix[i - 1][j - 1] + elif pattern[j - 1] == '*': + match_matrix[i][j] = match_matrix[i - 1][j] or match_matrix[i][j - 1] + else: + match_matrix[i][j] = False + + return match_matrix[input_length][pattern_length] + + +if __name__ == "__main__": + import doctest + doctest.testmod() + + print(is_pattern_match("baaabab","*****ba*****ab")) From c7bad5855b2e9082dfe946dc5a06edc2fb7b8386 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 Oct 2023 03:52:43 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/wildcard_matching.py | 31 +++++++++++++----------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/dynamic_programming/wildcard_matching.py b/dynamic_programming/wildcard_matching.py index be0c78cb7549..e9f487060d2c 100644 --- a/dynamic_programming/wildcard_matching.py +++ b/dynamic_programming/wildcard_matching.py @@ -1,5 +1,5 @@ """ -Given two strings, an input string and a pattern, +Given two strings, an input string and a pattern, this program checks if the input string matches the pattern. Example : @@ -9,19 +9,19 @@ This problem can be solved using the concept of "DYNAMIC PROGRAMMING". -We create a 2D boolean matrix, where each entry match_matrix[i][j] is True -if the first i characters in input_string match the first j characters -of pattern. We initialize the first row and first column based on specific -rules, then fill up the rest of the matrix using a bottom-up dynamic +We create a 2D boolean matrix, where each entry match_matrix[i][j] is True +if the first i characters in input_string match the first j characters +of pattern. We initialize the first row and first column based on specific +rules, then fill up the rest of the matrix using a bottom-up dynamic programming approach. -The amount of match that will be determined is equal to match_matrix[n][m] -where n and m are lengths of the input_string and pattern respectively. +The amount of match that will be determined is equal to match_matrix[n][m] +where n and m are lengths of the input_string and pattern respectively. """ -def is_pattern_match(input_string : str, pattern : str) -> bool: +def is_pattern_match(input_string: str, pattern: str) -> bool: """ is_pattern_match("baaabab","*****ba*****ba") >>> False @@ -29,12 +29,14 @@ def is_pattern_match(input_string : str, pattern : str) -> bool: >>> True is_pattern_match("aa","*") >>> True - + """ input_length = len(input_string) pattern_length = len(pattern) - match_matrix = [[False for i in range(pattern_length + 1)] for j in range(input_length + 1)] + match_matrix = [ + [False for i in range(pattern_length + 1)] for j in range(input_length + 1) + ] match_matrix[0][0] = True @@ -42,14 +44,14 @@ def is_pattern_match(input_string : str, pattern : str) -> bool: match_matrix[i][0] = False for j in range(1, pattern_length + 1): - if pattern[j - 1] == '*': + if pattern[j - 1] == "*": match_matrix[0][j] = match_matrix[0][j - 1] for i in range(1, input_length + 1): for j in range(1, pattern_length + 1): - if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == '?': + if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == "?": match_matrix[i][j] = match_matrix[i - 1][j - 1] - elif pattern[j - 1] == '*': + elif pattern[j - 1] == "*": match_matrix[i][j] = match_matrix[i - 1][j] or match_matrix[i][j - 1] else: match_matrix[i][j] = False @@ -59,6 +61,7 @@ def is_pattern_match(input_string : str, pattern : str) -> bool: if __name__ == "__main__": import doctest + doctest.testmod() - print(is_pattern_match("baaabab","*****ba*****ab")) + print(is_pattern_match("baaabab", "*****ba*****ab")) From 31a87c1d8f80686e9f7a001fb923a22c9319454c Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sat, 14 Oct 2023 09:38:01 +0530 Subject: [PATCH 3/6] changes for doctest errors --- dynamic_programming/wildcard_matching.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/dynamic_programming/wildcard_matching.py b/dynamic_programming/wildcard_matching.py index be0c78cb7549..9776b7c9f767 100644 --- a/dynamic_programming/wildcard_matching.py +++ b/dynamic_programming/wildcard_matching.py @@ -20,17 +20,16 @@ """ - def is_pattern_match(input_string : str, pattern : str) -> bool: """ - is_pattern_match("baaabab","*****ba*****ba") - >>> False - is_pattern_match("baaabab","*****ba*****ab") - >>> True - is_pattern_match("aa","*") - >>> True - + >>> is_pattern_match('baaabab','*****ba*****ba') + False + >>> is_pattern_match('baaabab','*****ba*****ab') + True + >>> is_pattern_match('aa','*') + True """ + input_length = len(input_string) pattern_length = len(pattern) @@ -61,4 +60,4 @@ def is_pattern_match(input_string : str, pattern : str) -> bool: import doctest doctest.testmod() - print(is_pattern_match("baaabab","*****ba*****ab")) + print(f"{is_pattern_match('baaabab','*****ba*****ab')}") From 0f487b36339d9459f23e2e1075419cc35cd9a6c0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 Oct 2023 04:10:16 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/wildcard_matching.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/wildcard_matching.py b/dynamic_programming/wildcard_matching.py index 40b4e48bb6f7..5ffd042b911b 100644 --- a/dynamic_programming/wildcard_matching.py +++ b/dynamic_programming/wildcard_matching.py @@ -20,7 +20,8 @@ """ -def is_pattern_match(input_string : str, pattern : str) -> bool: + +def is_pattern_match(input_string: str, pattern: str) -> bool: """ >>> is_pattern_match('baaabab','*****ba*****ba') False @@ -29,7 +30,7 @@ def is_pattern_match(input_string : str, pattern : str) -> bool: >>> is_pattern_match('aa','*') True """ - + input_length = len(input_string) pattern_length = len(pattern) From a7557bfbdcf5add423b4ce61323d20a4b9f6de5b Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 15 Oct 2023 15:39:04 +0530 Subject: [PATCH 5/6] code changes --- dynamic_programming/wildcard_matching.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/dynamic_programming/wildcard_matching.py b/dynamic_programming/wildcard_matching.py index 5ffd042b911b..791fb1196141 100644 --- a/dynamic_programming/wildcard_matching.py +++ b/dynamic_programming/wildcard_matching.py @@ -20,8 +20,7 @@ """ - -def is_pattern_match(input_string: str, pattern: str) -> bool: +def is_pattern_match(input_string : str, pattern : str) -> bool: """ >>> is_pattern_match('baaabab','*****ba*****ba') False @@ -30,26 +29,21 @@ def is_pattern_match(input_string: str, pattern: str) -> bool: >>> is_pattern_match('aa','*') True """ - + input_length = len(input_string) pattern_length = len(pattern) - match_matrix = [ - [False for i in range(pattern_length + 1)] for j in range(input_length + 1) - ] + match_matrix = [[False] * (pattern_length + 1) for _ in range(input_length + 1)] match_matrix[0][0] = True - for i in range(1, input_length + 1): - match_matrix[i][0] = False - for j in range(1, pattern_length + 1): if pattern[j - 1] == "*": match_matrix[0][j] = match_matrix[0][j - 1] for i in range(1, input_length + 1): for j in range(1, pattern_length + 1): - if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == "?": + if pattern[j - 1] in ("?", input_string[i - 1]): match_matrix[i][j] = match_matrix[i - 1][j - 1] elif pattern[j - 1] == "*": match_matrix[i][j] = match_matrix[i - 1][j] or match_matrix[i][j - 1] @@ -58,7 +52,6 @@ def is_pattern_match(input_string: str, pattern: str) -> bool: return match_matrix[input_length][pattern_length] - if __name__ == "__main__": import doctest From 3e15591796386fb27e20ae68f7aadc02dd16a418 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Oct 2023 10:09:48 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/wildcard_matching.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/wildcard_matching.py b/dynamic_programming/wildcard_matching.py index 791fb1196141..4ffc4b5d46aa 100644 --- a/dynamic_programming/wildcard_matching.py +++ b/dynamic_programming/wildcard_matching.py @@ -20,7 +20,8 @@ """ -def is_pattern_match(input_string : str, pattern : str) -> bool: + +def is_pattern_match(input_string: str, pattern: str) -> bool: """ >>> is_pattern_match('baaabab','*****ba*****ba') False @@ -29,7 +30,7 @@ def is_pattern_match(input_string : str, pattern : str) -> bool: >>> is_pattern_match('aa','*') True """ - + input_length = len(input_string) pattern_length = len(pattern) @@ -52,6 +53,7 @@ def is_pattern_match(input_string : str, pattern : str) -> bool: return match_matrix[input_length][pattern_length] + if __name__ == "__main__": import doctest