Exemplo n.º 1
0
    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])
Exemplo n.º 2
0
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())
Exemplo n.º 3
0
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())
Exemplo n.º 4
0
 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])
Exemplo n.º 5
0
 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])
Exemplo n.º 6
0
 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])
Exemplo n.º 7
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, [])