def test_text_has_prefix(self): """Search through a string like the search pattern w/a prefix. e.g. if the search pattern is 'abc', then the text may be 'aabc'. """ indices = kmp_matcher('a' + self.pattern, self.pattern) self.assertEqual(indices, [1])
def main(): """Parse arguments. Maybe call string matching logic and print results.""" parser = argparse.ArgumentParser( prog='python -m string_matcher', description='''\ Read in a search pattern and a body of text, and determine whether the former is in the latter. Print the results to stdout. ''', ) parser.add_argument('pattern', help='The pattern to search for.') parser.add_argument('text', help='The text to search.') args = parser.parse_args() indices = string_matcher.kmp_matcher(args.text, args.pattern) wrapper = textwrap.TextWrapper() with io.StringIO() as handle: if indices: handle.write(wrapper.fill(textwrap.dedent( '''\ The pattern is present in the text at the following (zero-based) indices: {} '''.format(indices) ))) else: handle.write(wrapper.fill( 'The pattern is not present in the text.' )) print(handle.getvalue())
def main(): """Parse arguments. Maybe call string matching logic and print results.""" parser = argparse.ArgumentParser( prog="python -m string_matcher", description="""\ Read in a search pattern and a body of text, and determine whether the former is in the latter. Print the results to stdout. """, ) parser.add_argument("pattern", help="The pattern to search for.") parser.add_argument("text", help="The text to search.") args = parser.parse_args() indices = string_matcher.kmp_matcher(args.text, args.pattern) wrapper = textwrap.TextWrapper() with io.StringIO() as handle: if indices: handle.write( wrapper.fill( textwrap.dedent( """\ The pattern is present in the text at the following (zero-based) indices: {} """.format( indices ) ) ) ) else: handle.write(wrapper.fill("The pattern is not present in the text.")) print(handle.getvalue())
def test_text_has_patterns(self): """Search through a string for which matches are present.""" indices = kmp_matcher('ababacababaca', self.pattern) self.assertEqual(indices, [0, 6])
def test_text_has_pattern(self): """Search through a string for which a match is present.""" indices = kmp_matcher('aabababacax', self.pattern) self.assertEqual(indices, [3])
def test_text_like_pattern(self): """Search through a string exactly like the search pattern.""" indices = kmp_matcher(self.pattern, self.pattern) self.assertEqual(indices, [0])
def test_text_missing_pattern(self): """Search through a string for which no match is present.""" indices = kmp_matcher('aabababacbx', self.pattern) self.assertEqual(indices, [])