Skip to content

Instantly share code, notes, and snippets.

@scuq
Created November 5, 2015 23:49
Show Gist options
  • Save scuq/8cd6014869345587c5a5 to your computer and use it in GitHub Desktop.
Save scuq/8cd6014869345587c5a5 to your computer and use it in GitHub Desktop.
dirty divide-and-conquer python function list_contains_fast.py
def list_contains_fast(largelist,startwithstr):
listsize=len(largelist)
#print "list_contains_fast"+str(listsize)
if listsize <= 500:
for listentry in largelist:
if startwithstr.startswith(listentry):
return True
return False
currentpick_index=int(listsize/2)
currentpick=largelist[currentpick_index]
currentpick_len=len(currentpick)
compare_len=len(currentpick)
if currentpick_len > len(startwithstr):
compare_len = len(startwithstr)
if currentpick.startswith(startwithstr) == True:
return True
if startwithstr[0:compare_len] > largelist[currentpick_index][0:compare_len]:
return list_contains_fast(largelist[currentpick_index-1:],startwithstr)
else:
return list_contains_fast(largelist[:currentpick_index+1],startwithstr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment