diff --git a/utilities/python/binary_search.py b/utilities/python/binary_search.py index 76f02410..4e664c72 100644 --- a/utilities/python/binary_search.py +++ b/utilities/python/binary_search.py @@ -46,3 +46,24 @@ print(binary_search([1, 2, 3, 10], 4) == -1) print(binary_search([1, 2, 3, 10], 0) == -1) print(binary_search([1, 2, 3, 10], 11) == -1) print(binary_search([5, 7, 8, 10], 3) == -1) + +print(bisect_left([1, 2, 3, 3, 10], 1) == 0) +print(bisect_left([1, 2, 3, 3, 10], 2) == 1) +print(bisect_left([1, 2, 3, 3, 10], 3) == 2) # First "3" is at index 2 +print(bisect_left([1, 2, 3, 3, 10], 10) == 4) + +# These return a valid index despite target not being in array. +print(bisect_left([1, 2, 3, 3, 10], 9) == 4) +print(bisect_left([1, 2, 3, 3, 10], 0) == 0) # Insert "0" at front +print(bisect_left([1, 2, 3, 3, 10], 11) == 5) # Insert "5" at back + +print(bisect_right([1, 2, 3, 3, 10], 1) == 1) +print(bisect_right([1, 2, 3, 3, 10], 2) == 2) +print(bisect_right([1, 2, 3, 3, 10], 3) == 4) # Last "3" is at index 3, so insert new "3" at index 4 +print(bisect_right([1, 2, 3, 3, 10], 10) == 5) + +# These return a valid index despite target not being in array. +print(bisect_right([1, 2, 3, 3, 10], 9) == 4) +print(bisect_right([1, 2, 3, 3, 10], 0) == 0) # Insert "0" at front +print(bisect_right([1, 2, 3, 3, 10], 11) == 5) # Insert "5" at back +