Kata Two — Karate Chop – Part 4

by byamabe on May 22, 2008

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

Leave a Comment

Previous post:

Next post: