Full story

Posted on 05-22-2008 under Python

The final chop method I came up with was rather uninspired. I used a for loop instead a while loop and used some array manipulation.

Here’s the code:

def chopFive(value, list):
if len(list) == 0:
return -1

index = 0
newList = list
found = False

for number in range(0, len(list) / 2 + 1):
tempIndex = len(newList) / 2
if value == newList[tempIndex]:
return index + tempIndex
elif value > newList[tempIndex]:
newList = newList[tempIndex+1:len(newList)]
if len(newList) == 0:
return -1
index += tempIndex + 1
elif value < newList[tempIndex]:
newList = newList[0:tempIndex]
if len(newList) == 0:
return -1

return -1

No comment yet

Don't be shy, express yourself ! ;)

Post your comments

Fill up the fields below. Email is required but won't be revealed to anyone. Some (simple) html is allowed. If you want to cite another comment use the "Quote" link located beside each author. Finally be nice or at least keep it polite. Thanks for posting.