Created
November 13, 2021 00:56
-
-
Save RHDZMOTA/07cd57b7d25ae8ebaba0bed545a653ca to your computer and use it in GitHub Desktop.
Code Challenge: https://twitter.com/willmcgugan/status/1459233690052141063
This file contains hidden or 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
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