Skip to content

Instantly share code, notes, and snippets.

@willmcgugan
Created July 25, 2022 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willmcgugan/acc9af480a58ab69c53d08a1816f3b38 to your computer and use it in GitHub Desktop.
Save willmcgugan/acc9af480a58ab69c53d08a1816f3b38 to your computer and use it in GitHub Desktop.
Ludicrous partition function
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