please help
if the way i posted this or my question isn't clear i apologize in advance
def main():
mylist=[]
for i in range(20):
mylist.append(i*3)
mylist.sort()
print mylist
mylist.append(36)
binarySearch(mylist,0,len(mylist),3)
def binarySearch(thelist,lower,upper,item):
if upper<lower:
print 'item not in the list'
return
middle=(lower+upper)/2
if thelist[middle]<item:
lower=middle+1
binarySearch(thelist,lower,upper,item)
elif thelist[middle]>item:
upper=middle-1
binarySearch(thelist,lower,upper,item)
else:
print 'the item was found at index ',middle
return
main()
this is the list generated:[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 36, 39, 42, 45, 48, 51, 54, 57]
after running the code:
the item was found at index : 13
This post has been edited by trigger202: 13 August 2012 - 01:22 AM

New Topic/Question
Reply



MultiQuote




|