Skip to content

Instantly share code, notes, and snippets.

@RHDZMOTA
Created November 13, 2021 00:56
Show Gist options
  • Save RHDZMOTA/07cd57b7d25ae8ebaba0bed545a653ca to your computer and use it in GitHub Desktop.
Save RHDZMOTA/07cd57b7d25ae8ebaba0bed545a653ca to your computer and use it in GitHub Desktop.
from typing import List, Tuple, Optional
RETURN_TYPE = Optional[Tuple[List[int], List[int]]]
def bisect(my_list: List[int], max_val: int) -> RETURN_TYPE:
partial_sum = 0
for i, val in enumerate(my_list):
partial_sum += val
if partial_sum > max_val:
return my_list[:i], my_list[i:]
return (my_list, []) if partial_sum == max_val else None
# Test cases
assert bisect([1, 2, 1, 0], 3) == ([1, 2], [1, 0])
assert bisect([1, 2, 1, 0], 4) == ([1, 2, 1, 0], [])
assert bisect([1, 2, 0, 1, 0], 3) == ([1, 2, 0], [1, 0])
assert bisect([1, 1, 0], 10) is None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment