Browse Source

misc: Add Boyer-Moore string search algorithm

master
kaneko 5 years ago
committed by rkaneko
commit
b8bbc60a66
  1. 4
      .gitignore
  2. 15
      Pipfile
  3. 19
      README.md
  4. 10
      __init__.py
  5. 0
      src/__init__.py
  6. 23
      src/bm.py
  7. 0
      test/__init__.py
  8. 21
      test/test_bm.py

4
.gitignore

@ -0,0 +1,4 @@
.pytest_cache/
.venv/
Pipfile.lock

15
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"

19
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
```

10
__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__))
))

0
src/__init__.py

23
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

0
test/__init__.py

21
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
Loading…
Cancel
Save