1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#!/usr/bin/env python2 def bin_search(a, val, lo, hi): print "%d %d"%(lo, hi) if hi < lo: return -1 mid = (lo+hi)/2 if a[mid] > val: return bin_search(a, val, lo, mid-1) elif a[mid] < val: return bin_search(a, val, mid+1, hi) else: return mid a = [1, 2, 3, 4, 5, 6, 7, 8] # 0 1 2 3 4 5 6 7 if bin_search(a, 3, 0, len(a)-1): print "Yay!" else: print ":-("