Skip to content

Commit ab709a2

Browse files
Add Jaro-Winkler regression tests and a large-input benchmark
Preserve all eight existing examples and append three regressions that fail before the fix. Add an optional 2,000-character timing comparison without changing the production implementation.
1 parent 5cb74bd commit ab709a2

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

‎strings/jaro_winkler.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ def jaro_winkler(str1: str, str2: str) -> float:
2323
0.4666666666666666
2424
>>> jaro_winkler("hell**o", "*world")
2525
0.4365079365079365
26+
27+
Matched positions cannot be reused as literal spaces.
28+
>>> round(jaro_winkler("aa ", "aa"), 6)
29+
0.911111
30+
>>> round(jaro_winkler("a a", "aa"), 6)
31+
0.9
32+
33+
Repeated characters must be consumed inside the matching window.
34+
>>> round(jaro_winkler("aabb", "bab"), 6)
35+
0.722222
2636
"""
2737

2838
def get_matched_characters(_str1: str, _str2: str) -> str:
@@ -76,6 +86,26 @@ def get_matched_characters(_str1: str, _str2: str) -> str:
7686

7787
if __name__ == "__main__":
7888
import doctest
89+
import sys
90+
from timeit import repeat
7991

8092
doctest.testmod()
8193
print(jaro_winkler("hello", "world"))
94+
95+
# Run with --benchmark on each revision using the same 2,000-character inputs.
96+
if "--benchmark" in sys.argv:
97+
size = 2000
98+
cases = (
99+
("identical", "a" * size, "a" * size),
100+
("no matches", "a" * size, "b" * size),
101+
("trailing space", "a" * (size - 1) + " ", "a" * (size - 1)),
102+
)
103+
for name, first, second in cases:
104+
score = jaro_winkler(first, second)
105+
timings = repeat(
106+
"jaro_winkler(first, second)", repeat=5, number=1, globals=globals()
107+
)
108+
print(
109+
f"{name}, {len(first)}/{len(second)} characters: "
110+
f"{min(timings):.6f} seconds (best of 5), score={score}"
111+
)

0 commit comments

Comments
 (0)