Skip to content

Instantly share code, notes, and snippets.

@Kwpolska
Created May 14, 2016 08:37
Show Gist options
  • Save Kwpolska/3c8d3e87caef439661c206eca5ac314c to your computer and use it in GitHub Desktop.
Save Kwpolska/3c8d3e87caef439661c206eca5ac314c to your computer and use it in GitHub Desktop.
import random
inp = list(range(1, 1001))
needle = random.randrange(1, 1001)
p = 0
q = len(inp)
pos = (p + q) // 2
while inp[pos] != needle:
print(pos)
if inp[pos] > needle:
q = pos
else:
p = pos
pos = (p + q) // 2
print(pos, inp[pos], needle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment