Skip to content

Instantly share code, notes, and snippets.

@CamDavidsonPilon
Last active March 4, 2017 17:47
Show Gist options
  • Save CamDavidsonPilon/ef1f6e8db40cf00156d8afdadf697d7e to your computer and use it in GitHub Desktop.
Save CamDavidsonPilon/ef1f6e8db40cf00156d8afdadf697d7e to your computer and use it in GitHub Desktop.
def mod_binary_search(round, previous_winner, dataset):
# round starts at 0, previous_winner starts at 0
if 2**round >= len(dataset):
return previous_winner
mod = 2 ** (round + 1)
if test(dataset, previous_winner, mod):
return mod_binary_search(round+1, previous_winner, dataset)
else:
return mod_binary_search(round+1, previous_winner + 2**round, dataset)
def test(dataset, target, modulus):
filtered_dataset = filter(lambda x: x % modulus == target, dataset)
if _test(filtered_dataset):
return True
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment