Created
July 25, 2022 19:29
-
-
Save willmcgugan/acc9af480a58ab69c53d08a1816f3b38 to your computer and use it in GitHub Desktop.
Ludicrous partition function
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
def partition_will(pred, values): | |
if not values: | |
return [], [] | |
if len(values) == 1: | |
return ([], values) if pred(values[0]) else (values, []) | |
values = sorted(values, key=pred) | |
lower = 0 | |
upper = len(values) | |
index = (lower + upper) // 2 | |
try: | |
while index: | |
value = pred(values[index]) | |
if pred(values[index + 1]) and not value: | |
index += 1 | |
return values[:index], values[index:] | |
if value: | |
upper = index | |
else: | |
lower = index | |
index = (lower + upper) // 2 | |
except IndexError: | |
return values[:], [] | |
return [], values[:] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment