Files
troupe-standup/wizard-work/beam/test_binary_search_bounds.py
T

56 lines
2.0 KiB
Python

"""Bounds cells for `binary_search_bounds.py`, the module BESIDE this file.
Q166. This file landed at `2829ff0` with
`sys.path.insert(0, "/root/.hermes/wizard-outputs/beam")`, a directory outside
the repository, so it graded whatever that host directory held and no clone
could reproduce its reading: `tools/import_resolve_check.py` read
`FAULT OUTSIDE_REPO wizard-work/beam/test_binary_search_bounds.py:2`. It now
binds its own directory and asserts which file it read, the same repair Q160
and Q162 made in this lane.
The same edit makes the checks attributable. The file shipped with its
assertions at module level and no `test_*` function, so the lane's runner read
it as `unittest=0 bare=0`: a wrong bound surfaced as an import error against
the file, never as a named failing cell.
"""
import os
import random
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import binary_search_bounds # noqa: E402
from binary_search_bounds import bisect_left, bisect_right, contains # noqa: E402
assert os.path.realpath(binary_search_bounds.__file__) == \
os.path.realpath(os.path.join(HERE, "binary_search_bounds.py")), \
"graded %s, not the module beside this file" % binary_search_bounds.__file__
def _brute_left(a, x):
return sum(1 for v in a if v < x)
def _brute_right(a, x):
return sum(1 for v in a if v <= x)
def test_bounds_brute_cases():
for a in ([], [1], [1, 2, 3], [1, 1, 2, 2, 3]):
for x in (0, 1, 2, 3, 4):
assert bisect_left(a, x) == _brute_left(a, x), (a, x)
assert bisect_right(a, x) == _brute_right(a, x), (a, x)
assert contains(a, x) == (x in a), (a, x)
def test_bounds_random_1000():
random.seed(42)
for _ in range(1000):
n = random.randint(0, 20)
a = sorted(random.randint(-5, 5) for _ in range(n))
x = random.randint(-6, 6)
assert bisect_left(a, x) == _brute_left(a, x), (a, x)
assert bisect_right(a, x) == _brute_right(a, x), (a, x)
assert contains(a, x) == (x in a), (a, x)