Función recursiva para devolver la cantidad de elementos en una lista que son seguidos directamente por el doble de ese elemento.
Estoy tratando de escribir una función recursiva para devolver el número de elementos en una lista que son seguidos directamente por ese elemento duplicado.
Por ejemplo, la lista que tengo actualmente, L = [1, 2, 4, 8, 16, 32, 64, 128, 256], devuelve el número 8.
Escribí la versión no recursiva de la función, pero no puedo averiguar cómo escribirla como una función recursiva (una función que se llama a sí misma). Lo que tengo se muestra a continuación.
def countDouble(L):
doubleCount = 0
index = 0
for num in range(0, len(L) - 1):
if int(L[index + 1])/2 == int(L[index]):
doubleCount += 1
index += 1
return doubleCount
L = [1, 2, 4, 8, 16, 32, 64, 128, 256]
print(countDouble(L))
Por favor ayuda si puedes.
davy.ai
Aquí hay una solución recursiva a tu problema: