From b8bbc60a664bd3009ef83fb910985adb8672784d Mon Sep 17 00:00:00 2001 From: kaneko Date: Wed, 6 Nov 2019 16:38:00 +0900 Subject: [PATCH] misc: Add Boyer-Moore string search algorithm --- .gitignore | 4 ++++ Pipfile | 15 +++++++++++++++ README.md | 19 +++++++++++++++++++ __init__.py | 10 ++++++++++ src/__init__.py | 0 src/bm.py | 23 +++++++++++++++++++++++ test/__init__.py | 0 test/test_bm.py | 21 +++++++++++++++++++++ 8 files changed, 92 insertions(+) create mode 100644 .gitignore create mode 100644 Pipfile create mode 100644 README.md create mode 100644 __init__.py create mode 100644 src/__init__.py create mode 100644 src/bm.py create mode 100644 test/__init__.py create mode 100644 test/test_bm.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a2aca7f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.pytest_cache/ +.venv/ + +Pipfile.lock diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..6dd7c83 --- /dev/null +++ b/Pipfile @@ -0,0 +1,15 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] +pytest = "*" + +[packages] + +[requires] +python_version = "3.7" + +[scripts] +unittest = "pipenv run pytest -s" diff --git a/README.md b/README.md new file mode 100644 index 0000000..e23929b --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +Algorithm test +=== + +### Usage + +```bash +$ python3 -m pip install pipenv + +# Use virtualenv +$ PIPENV_VENV_IN_PROJECT=true pipenv shell + +$ pipenv install --dev +``` + +- Unittest + +```bash +$ pipenv run unittest +``` diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..9b9eb92 --- /dev/null +++ b/__init__.py @@ -0,0 +1,10 @@ +import sys +import os + +sys.path.append(os.path.join( + os.path.dirname(os.path.realpath(__file__)), + "src" +)) +sys.path.append(os.path.join( + os.path.dirname(os.path.realpath(__file__)) +)) diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/bm.py b/src/bm.py new file mode 100644 index 0000000..5767ee9 --- /dev/null +++ b/src/bm.py @@ -0,0 +1,23 @@ +from typing import Dict + + +def make_km_table(pattern: str) -> Dict[str, int]: + table = dict() + raise Exception("TODO") + return table + + +class Bm(object): + def __init__(self, text: str, pattern: str): + self.text = text + self.pattern = pattern + self.table = make_km_table(pattern) + + def decide_slide_width(self, c: str) -> int: + assert len(c) == 1 + raise Exception("TODO") + return -1 + + def search(self) -> int: + raise Exception("TODO") + return -1 diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_bm.py b/test/test_bm.py new file mode 100644 index 0000000..2f3f3d5 --- /dev/null +++ b/test/test_bm.py @@ -0,0 +1,21 @@ +import pytest + +from src.bm import Bm + +@pytest.mark.parametrize("text,pattern,expected", [ + ("GCTCACTGAGCGCTCGT", "GCTCG", 11), + ("GCTCACTGAGCGCTCGT", "CACTGAG", 3), + ("GCTCACTGAGCGCTCGT", "AAAAA", -1), + ("GCTCACTGAGCGCTCGT", "GCTCGTT", -1), + ("GCTCACTGAGCGCTCGT", "GCTCACTGAGCGCTCGT", 0), + ("ANPANMAN", "PAN", 2), + ("ANPANMAN", "ANPAN", 0), + ("ANPANMAN", "BIKINMAN", -1), +]) +def test_bm(text, pattern, expected): + bm = Bm(text, pattern) + + actual = bm.search() + + assert actual == expected + # assert False # for print debugging