-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrew Fasano
committed
Sep 5, 2019
1 parent
f90aa1c
commit 806e022
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
def bad_bin_search(args, fun): | ||
''' | ||
Given a list of items and a function that takes a list, | ||
do a binary search to remove all items that cause fun to fail | ||
Assumes args starts with > 1 element | ||
Returns a list of OK args | ||
''' | ||
if len(args) <= 1: # Already failed on this arg, don't retry it | ||
return [] | ||
|
||
mid = len(args)/2 | ||
left = args[:mid] | ||
right = args[mid:] | ||
|
||
if len(left): | ||
try: # If left still fails, reduce farther | ||
fun(left) | ||
except (AssertionError, RuntimeError): | ||
left = bad_bin_search(left, fun) | ||
|
||
if len(right): | ||
try: # If right still fails, reduce farther | ||
fun(right) | ||
except (AssertionError, RuntimeError): | ||
right = bad_bin_search(right, fun) | ||
|
||
return left + right | ||
|
||
if __name__ == "__main__": | ||
def test_fn(l): | ||
for item in l: | ||
if item % 3 == 0: | ||
raise RuntimeError("Test_fn: no factors of 3 allowed") | ||
return l | ||
|
||
orig_list = [1,2,3,4,5,6,7,8,9] | ||
r = bad_bin_search(orig_list, test_fn) | ||
assert (r == [1,2,4,5,7,8]), "bad_bin_search is broken" | ||
|
||
print("All utility tests pass") |
Binary file not shown.