Skip to content

Instantly share code, notes, and snippets.

@Canx
Created February 12, 2012 16:48
Show Gist options
  • Save Canx/1809602 to your computer and use it in GitHub Desktop.
Save Canx/1809602 to your computer and use it in GitHub Desktop.
Karate Chop
def chop(valor, lista)
case(lista.size)
when 0 then return -1
when 1 then return (lista[0] != valor)? -1: 0
else
mitad = lista.size/2
if lista[mitad] > valor
return chop(valor, lista[0..mitad - 1])
else
posicion = chop(valor, lista[mitad..lista.size])
return (posicion == -1)? -1: posicion + mitad
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment