Aún no tienes acceso a esta clase

Crea una cuenta y continúa viendo este curso

# Mejorando nuestra sucesión de Fibonacci

15/21
Recursos

Aportes 342

Preguntas 12

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad? Crea una cuenta o inicia sesión.

Una versión más compacta:

``````from time import sleep
def fibonacci_gen():
a, b = 0, 1
while True:
yield a
a, b = b, a+b

if __name__ == "__main__":
fibonacci = fibonacci_gen()
for element in fibonacci:
print(element)
sleep(1)

``````

Mi solución:

``````import time

def fibo_gen(stop: int):
n1 = 0
n2 = 1
counter = 0

while True:

if counter == 0:
counter += 1
yield n1

elif counter == 1:
counter += 1
yield n2

else:
aux = n1 + n2

if not stop or aux <= stop:
n1, n2 = n2, aux
counter += 1
yield aux
else:
break

if __name__ == '__main__':
fibonacci = fibo_gen(5)

for element in fibonacci:
print(element)
time.sleep(1)
``````

Este es mi aporte 😃

``````<
import time

def fibo_gen(selecc:int) -> int:

n1 = 0
n2 = 1
counter = 0
aux = 0

while aux < selecc:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

def run():
selecc = int(input('Introduce your limit value for the fibonacci function:\n'))

fibonacci = fibo_gen(selecc)

for element in fibonacci:
print(element)
time.sleep(1)

if __name__ == '__main__':

run()

>
``````

Aporte 🐍🐍

#### Mejorando nuestra sucesión de Fibonacci.

• Solución al reto:

``````import time

def fibo_gen(max = None):
n1, n2 = 0, 1
counter = 0

while not max or counter <= max:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield n2

if __name__ == '__main__':
fibonacci = fibo_gen(10)
for element in fibonacci:
print(element)
time.sleep(0.5)
``````

Fascinante

``````import time

def fibo_gen():
n1 = 0
n2 = 1
counter = 0
limit = int(input("""Set a limit for the Fibonacci Succecsion: """))

while counter < limit:
if counter == 0:
counter +=1
yield n1
elif counter == 1:
counter +=1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter +=1
yield aux
raise StopIteration

if __name__ == '__main__':
fibonacci = fibo_gen()
for element in fibonacci:
print(element)
time.sleep(0.5)
``````

En mi caso, diseñé la solución para que tome en cuenta incluso si el usuario no ingresa nada.

``````import time
def fibonacci_gen(max=None):
n1 = 0
n2 = 1
count = 0
is_infinite = False
if max == None:
is_infinite = True

while is_infinite or count<max:
if count == 0:
count +=1
yield n1
elif count == 1:
count +=1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
count +=1
yield aux

if __name__ == "__main__":
fibonacci = fibonacci_gen(5)
for element in fibonacci:
print(element)
time.sleep(0.5)
``````

Aquí mi solución al reto después de pasearme por stackoverflow

``````from time import sleep

max = int(input('Max: '))

def fibo():
a, b = 0, 1
while True:
yield a
a, b = b, a + b

for n in fibo():
if n > max:
break
print(n)
sleep(0.5)
``````

mi reto 🎋🎋

``````import time

def fibo_gen(max:int):
n1=0
n2=1
counter=0

while True:
if not max or n2 <= max:
if counter == 0:
counter +=1
yield n1
elif counter==1:
counter +=1
yield n2
else:
aux = n1 + n2
n1 , n2 = n2, aux
counter +=1
yield aux
else:
break

if __name__=="__main__":
fibonacci=fibo_gen(max=10)
for element in fibonacci:
print(element )
time.sleep(0.1)
``````

Reto cumplido! 😃

Hola a todos, mi aporte.

``````import time

def fibo_gen(max:int):
n1 = 0
n2 = 1
counter =0
while True:
if counter == 0:
counter +=1
yield n1
elif counter ==1:
counter +=1
yield n2
else:

aux = n1 + n2
n1,n2 = n2, aux
if aux <= max:
yield aux
else:
raise StopIteration

if __name__ == '__main__':
fibonacci = fibo_gen(5)
for element in fibonacci:
print(element)
time.sleep(1)
``````
``````from time import sleep

def fibo_gen_max(maximo):
n1 = 0
n2 = 1
counter = 0
aux = 0
while True:
if counter == 0:
counter += 1
aux = 0
yield n1
elif counter == 1:
counter += 1
aux = 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
if aux <= maximo:
yield aux
else:
raise StopIteration

if __name__ == '__main__':
fibonacci_gen_max = fibo_gen_max(35)
for element in fibonacci_gen_max:
print(element)
time.sleep(1)
``````
``````import time

def fibo_gen(max = None):
n1 = 0
n2 = 1
counter = 0

while counter != max:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

if __name__ == "__main__":
fibonacci = fibo_gen(8)
for element in fibonacci:
print(element)
time.sleep(1)
``````

Este es mi código del reto con algo de static typing

``````import time
from typing import Generator

def fibonacci_gen(max: int = None) -> Generator:
n1: int = 0
n2: int = 1
counter: int = 0
while not max or max > counter:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
out: int = n1 + n2
n1, n2 = n2, out
counter += 1
yield out

if __name__ == '__main__':
fibonacci: Generator = fibonacci_gen(10)

for element in fibonacci:
print(element)
time.sleep(1)
``````

Pongo mi solución al reto!!

``````import time

def fibo_gen(max = None):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter +=1
yield n1
elif counter ==1:
counter +=1
yield n2

elif max == None:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

elif counter == max:
break

elif counter <= max:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

if __name__ == '__main__':
fibonacci = fibo_gen(8)
for element in fibonacci:
print(element)
time.sleep(1)
``````
``````import time

def fibo_gen(number):
n1 = 0
n2 = 1
counter = 0
max = number
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
if aux < max:
n1, n2 = n2, aux
counter += 1
yield aux
else:
break

if __name__=='__main__':
title = '''
_____  ____  ____    ___   ____    ____    __    __  ____
|     ||    ||    \  /   \ |    \  /    |  /  ]  /  ]|    |
|   __| |  | |  o  )|     ||  _  ||  o  | /  /  /  /  |  |
|  |_   |  | |     ||  O  ||  |  ||     |/  /  /  /   |  |
|   _]  |  | |  O  ||     ||  |  ||  _  /   \_/   \_  |  |
|  |    |  | |     ||     ||  |  ||  |  \     \     | |  |
|__|   |____||_____| \___/ |__|__||__|__|\____|\____||____|

'''
print(title)
max = int(input("Type a max number for your fibonacci sequence: "))
fibonacci = fibo_gen(max)
for element in fibonacci:
print(element)
time.sleep(0.5)

a = 1
while a==1:
option = int(input("Do you want to try again, if yes press (1) if not press (0): "))
if option == 1:
max = int(input("Type a max number for your fibonacci sequence: "))
fibonacci = fibo_gen(max)
for element in fibonacci:
print(element)
time.sleep(0.5)
elif option == 0:
print("Thanks :)")
a = 0
else:
print("You need to press 0 or 1")

``````

Esta es mi solución al reto

``````import time

def fibonacci_gen(max=None):
n1, n2, counter, aux = 0, 1, 0, 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
if aux <= max:
yield aux
else:
return aux

if __name__ == "__main__":
fibonacci = fibonacci_gen(5)

for element in fibonacci:
print(element)
time.sleep(1)
``````

Mi humilde aporte. Espero que a alguien le sirva.

``````import time

def fibo_gen():
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

def run():
number = int(input("Give me a number for stop: "))
fibonacci = fibo_gen()
for element in fibonacci:
print(element)
if element >= number:
break
time.sleep(1)

if __name__ == '__main__':
run()
``````

Mi ejemplo con las dos formas de ejecutar la secuencia de numeros:

``````import time

class FiboIter:

def __init__(self, max_number = None):
self.max_number = max_number

def __iter__(self):
self.n1 = 0
self.n2 = 1
self.counter = 0
self.aux = 0
return self

def __next__(self):
if (self.counter == 0):
self.aux = self.n1
elif self.counter == 1:
self.aux = self.n2
else:
self.aux = self.n1 + self.n2
self.n1, self.n2 = self.n2, self.aux
self.counter += 1
if self.max_number and self.aux >= self.max_number:
raise StopIteration
return self.aux

def run():
fibonacci = FiboIter(100)
for n in fibonacci:
print(n)
time.sleep(1)

if __name__ == '__main__':
run()
``````

Y con Generators

``````import time

def fibo_gen(max_number = None):
n1, n2, aux, counter = 0, 1, 0, 0
while True:
if counter == 0:
aux = n1
elif counter == 1:
aux = n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
if max_number and aux >= max_number:
break
yield aux

def run():
fibonacci = fibo_gen(100)
for element in fibonacci:
print(element)
time.sleep(1)

if __name__ == '__main__':
run()
``````

Mi código, espero les sirva

``````import time

# todo el codigo sigue igual, solo que en la función le agregamos un parametro que es el maximo
def fibo_gen(max):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2

# aqui es donde cambia. else entonces se crea un aux, se hace el swap y se suma el contador
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
# aca se pone a trabajar el aux normal, está trabajando
aux

# pero acá se pone un if y es que como ya está trabajando el aux, entonces ya se puede evaluar y acá se evalua si es menor que max entonces yield
if aux<max:
yield aux
# si no entonces se rompe
else:
break

if __name__ == '__main__':
fibonacci = fibo_gen(10)
for element in fibonacci:
print(element)
time.sleep(1)
``````

Mi solución:

``````from time import sleep

def fibonacci_gen(limit:int = None):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
if limit != None and aux >= limit:
raise StopIteration
counter += 1
n1, n2 = n2, aux
yield aux

if __name__ == '__main__':
fibonacci = fibonacci_gen(10)
for element in fibonacci:
print(element)
sleep(0.5)
``````

El usuario puede ingresar el límite y en caso de que sea 0 la función no imprime 1.

``````import time

def fibo_gen(limit = None):
a, b = 0, 1

while True:
yield a
a, b = b, a + b
if limit == 0 or a > limit:
break

if __name__ == '__main__':
user = int(input('Chose a number: '))
fibo = fibo_gen(user)

for element in fibo:
print(element)
time.sleep(0.5)
``````

Así quedó el mío (el usuario establece un número de iteraciones):

``````import time

def fibo_gen():
n1, n2 = 0, 1
while True:
yield n1
n1, n2, = n2, n1 + n2

if __name__ == "__main__":
max = input("Digite el valor máximo de iteraciones: ")
assert len(max) > 0, "Debes digitar un número válido"
for i, element in enumerate(fibo_gen()):
if i > int(max):
break
print(element)
time.sleep(1)>
``````

Mi aporte, pero con números menores a 100:

``````import time

def fibo_gen():
n1 = 0
n2 = 1
counter = 0

while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

if __name__ == '__main__':
fibonacci = fibo_gen()
for elemen in fibonacci:
print(elemen)
time.sleep(0.5)
if elemen > 100:
break
``````

Lo conseguí:

``````import time

def fibo_gen(iterMax=None):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux
if iterMax == counter:
break

if __name__ == '__main__':
fibonacci = fibo_gen(5)
for e in fibonacci:
print(e)
time.sleep(0.1)

``````

Reto:

``````from typing import NoReturn

def fibonacci_generator(n: int = 0) -> NoReturn:
n1: int = 0
n2: int = 1
counter: int = 0

while counter <= n:
yield n1
n1, n2 = n2, n1 + n2
counter += 1
``````

Le agregué también el ángulo de cada número y el número Phi, con los resultados coloreados en consola para que se entienda mejor:

``````import time

ORANGE = "\x1b[1;33m"
WHITE = "\x1b[1;37m"
CYAN = "\x1b[1;36m"

def to_angle(num, total):
degrees = num * 360 / total
minutes = degrees % 1 * 60
seconds = minutes % 1 * 60
return f"{int(degrees)}º {int(minutes)}' {int(seconds)}''"

def get_golden_angle(n1, n2):
total = n1 + n2
angle1 = to_angle(n1, total)
angle2 = to_angle(n2, total)
return f"{CYAN}#1:{WHITE} {angle1}     {CYAN}#2:{WHITE} {angle2}"

def fibo_gen(top=None):
n1 = 0
n2 = 1
counter = 0

while True:
if counter == top:
return n1
counter += 1
angles = get_golden_angle(n1, n2)
phi = n2/n1 if n1 > 0 else 1
yield (f"{ORANGE}Number:{WHITE} {n1}       "
f"{ORANGE}Angles:{WHITE} {angles}       "
f"{ORANGE}Phi:{WHITE} {phi}")
n1, n2 = n2, n1+n2

if __name__ == "__main__":
fibonacci = fibo_gen(10)
for i in fibonacci:
print(i)
time.sleep(0.05)
``````

el mio 😃

``````from time import sleep
def fibonacci_gen(max_num: int = None) -> int:
a, b = 0, 1
while not max_num or b < max_num:
yield a
a, b = b, a+b

if __name__ == "__main__":
fibonacci = fibonacci_gen(max_num=10000)
for element in fibonacci:
print(element)
sleep(0.3)
``````

lo mejor es el platzi day

``````import time

def fibo_gen(upper_limit):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1 = n2
n2 = aux
if aux > upper_limit:
break
yield aux

if __name__=="__main__":

fibonachi = fibo_gen(100)

for element in fibonachi:
print(element, end= " ")
time.sleep(1)
``````

Mi solución al reto propuesto en esta clase:

``````import time

def fibo_gen(max):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
if counter < max:
aux = n1 +n2
n1, n2 = n2, aux
counter += 1
yield  aux
else:
break

if __name__ == '__main__':
fibonacci = fibo_gen(10)
for element in fibonacci:
print(element)
time.sleep(1)
``````

Mi versión

``````import time
from typing import Generator

def fibo_gen(max_iteration: int = 0) -> Generator :
"""max_iteration contains the number of iterations. A value of 0 means no end"""

n1: int = 0
n2: int = 1
counter: int = 0

while True:
if max_iteration != 0 and counter > max_iteration:
break

if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
counter += 1
aux = n1 + n2
n1, n2 = n2, aux
yield aux

def run():
fibonacci: Generator = fibo_gen(20)

for element in fibonacci:
print(element)
time.sleep(0.5)

if __name__ == '__main__':
run()
``````
``````import time

def fibo_gen(max: int) -> int:
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
if aux <= max:
n1, n2 = n2, aux
counter += 1
yield aux
else:
break

if __name__ == "__main__":
num_max = int(input("Ingrese el número maximo: "))
fibonacci = fibo_gen(num_max)
for element in fibonacci:
print(element)
time.sleep(1)
``````

Esta es mi solución funciona CON o SIN Limites

``````import time
from typing import Counter

def fibo_gen(max=None):
n1=0
n2=1
Counter=0

while True:
if Counter==0:
Counter+=1
yield n1
elif Counter==1:
Counter+=1
yield n2
else:
if not max or Counter<=max:
aux=n1+n2
n1, n2 = n2, aux
Counter+=1
yield aux
else:
break

if __name__=="__main__":
# Con Limite
fibonacci=fibo_gen(5)
for element in fibonacci:
print(element)
time.sleep(0.1)

print("----------------")

# Sin Limite
fibonacci=fibo_gen()
for element in fibonacci:
print(element)
time.sleep(0.1)

``````

mi codigo del reto:

``````import time

def fibo_gen(max):
n1 = 0
n2 = 1
counter = 0
while counter < max:
print("counter: ", counter, "--", end="")
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

if __name__ == "__main__":
fibonacci = fibo_gen(5)
for element in fibonacci:
print(element)
time.sleep(.5)
``````

Mi código, el límite es 500 y el tiempo es .2, al final imprime un texto diciendo que se alcanzó el límite seleccionado.

``````import time

def fibo_gen(max: int):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
if aux<=max:
n1, n2 = n2, aux
counter += 1
yield aux
else:
print(f'Se alcanzó el límite seleccionado: {max}')
break

if __name__=="__main__":
fibonacci = fibo_gen(500)
for element in fibonacci:
print(element)
time.sleep(.2)
``````
``````import time

def fibo_gen(max: int):
maximo = max
n1 = 0
n2 = 1
counter = 0

while True:
if counter ==0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
if aux <= maximo:
n1, n2 = n2, aux
counter += 1
yield aux
else:
break

if __name__ == '__main__':
fibonacci = fibo_gen(22)
for element in fibonacci:
print(element)
time.sleep(1)
``````

👾

Mi aporte del reto, también por si no se le manda ningún parámetro

``````import time

def fibo_gen(max=None):
n1, n2 = 0, 1
counter=0

while True:
if not max or counter < max:
if counter==0:
counter+=1
yield n1
elif counter==1:
counter +=1
yield n2
else:
aux=n1+n2
n1, n2 = n2, aux
counter+=1
yield aux
else:
break

if __name__=="__main__":
fibonacci=fibo_gen(10)
for i in fibonacci:
print(i)
time.sleep(0.5)
``````
``````import time

def fibo_gen(max_counter: int):
a, b = 0, 1
while a <= max_counter:
yield a
a, b = b, a+b

if __name__ == "__main__":
for element in fibo_gen(3000):
print(element)
time.sleep(1)
``````

reto

``````import time
n = input("ingrese el numero de iteracciones ")
max = int(n)
def fibo_gen(max):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
if counter <= max:
yield aux
else:
break
if __name__ == "__main__":
fibonacci = fibo_gen(max)
for element in fibonacci:
print(element)
time.sleep(0.5)
``````

Mi aporte:

``````import time

def fibo_gen(num):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
elif counter == num:
raise StopIteration
else:
aux = n1 + n2
#n1=n2
#n2=aux
n1, n2 = n2, aux
counter += 1
yield aux

if __name__ == '__main__':
num = int(input('Max number: '))
fibonnacci = fibo_gen(num)
for element in fibonnacci:
print(element)
time.sleep(1)
``````
``````import time

def fibo_gen(max: int = None) -> int:
n1, n2 = 0, 1
while True:
if not max or n2 <= max:
yield n1
n1, n2 = n2, n1 + n2
else:
break

if __name__ == '__main__':
fibonacci = fibo_gen()
for element in fibonacci:
print(element)
time.sleep(.05)
``````
``````import time

def fibo_gen(max):
n1 = 0
n2 = 1
num_max = max
counter = 0

while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2

if aux > num_max:
raise StopIteration

n1, n2 = n2, aux
counter += 1
yield aux

def main():
try:
fibonacci = fibo_gen(100)
for element in fibonacci:
print(element)
time.sleep(0.5)
except RuntimeError:
print('Termina ejecución')

if __name__ == '__main__':
main()

``````

Aqui mi aporte:

``````import time

def fibo_gen(max=None):
max = max
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
if not max or aux <= max:
yield aux
else:
raise StopIteration

if __name__ == '__main__':
fibonacci = fibo_gen()
for element in fibonacci:
print(element)
time.sleep(0.1)
``````

Mi solución al reto. Funciona pero no estoy seguro si es correcto:

``````import time

def fibo_gen(max = None):
n1, n2, counter, maxi = 0, 1, 1, max
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
if aux <= maxi:
n1, n2 = n2, aux
counter += 1
yield aux
else:
break

if __name__ == '__main__':
max_num = int(input('Input an intiger: '))
fibonacci = fibo_gen(max_num)

for element in fibonacci:
print(element)
time.sleep(0.25)
``````
``````import time

def fibo_gen(limit: int) -> int:
n1, n2 = 0, 1
count = 0

while True:

if count <= limit :
if count == 0:
count += 1
yield n1

elif count == 1:
count += 1
yield n2

else:
aux = n1 + n2
n1, n2 = n2, aux
count += 1
yield aux
else:
raise StopIteration

if __name__ == "__main__":
fibbo = fibo_gen(4)
for i in fibbo:
print(i)
time.sleep(0.5)
``````

Mi solución

``````import time

def fibo_gen(max= None):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
elif counter <= max:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux
else:
raise StopIteration

if __name__ == '__main__':
fibonacci = fibo_gen(500)
for element in fibonacci:
print(element)
time.sleep(0.5)
``````

Les dejo mi resolución del reto:

``````import time

def fibo_gen():
n1 = 0
n2 = 1
counter = 0
maximo = 10000
while (n1+n2) <= maximo: # Ciclo infinito porque la secuencia es infinita
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

if __name__ == "__main__":
fibonacci = fibo_gen()
for element in fibonacci:
print(element)
time.sleep(0.1)
``````
``````import time

def fibo_gen(max=None):
n1=0
n2=1
counter=0
while True:
if counter==0:
counter+=1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
#yield aux
if not max or aux<=max:
yield aux
else:
break

if __name__=="__main__":
max = int(input("Introduce a number for the limit of the Fibonnaci function: "))
fibonacci = fibo_gen(max)
for element in fibonacci:
print(element)
time.sleep(1)
``````

Estoy sorprendido por el potencial de los generadores.
Comparto mi código a continuación.

Saludos!

``````from icecream import ic
import time

def fibo_gen():
a, b = 0, 1
while True:
yield b
a, b = b, a + b

if __name__ == '__main__':
value = input('''
FIBONACCI ITERATOR

I'm gonna print all values of the Fibonacci sequence until I reach the number you want.
If you wanna see an infinite sequence, press 0. If not, press any other number.
Give me the number: ''')

fibonacci = fibo_gen()
for element in fibonacci:
if element < int(value) or int(value) == 0:
ic(element)
time.sleep(0.5)
else:
break
``````

Este es mi aporte:

``````import time, os
def fibonacci_gen(num):
n1, n2 = 0, 1
count = 0
while count < num:
yield n1
count += 1
n1, n2 = n2, n1 + n2

if __name__ == "__main__":
os.system('cls')
num = int(input("Ingrese un número: "))
fibonacci = fibonacci_gen(num)
for element in fibonacci:
print(element)
time.sleep(1)
``````
``````import time

def fibonacci(n):
a, b = 0, 1
for i in range(n):
a, b = b, a + b
yield a

def run():
for i in fibonacci(4):
print(i)
time.sleep(1)

if __name__ == '__main__':
run()
``````

Mi solución, lo que no entiendo es porque aquí me funcionó el raise, pero cuando escribí el mismo condicional en el ejercicio de iteradores no funcionó.
Me revienta la cabeza¡¡

``````import time

def Fibo_gen():
n1 = 0
n2 = 1
counter = 0
limit: int = int(input('Enter number to where fibonacci succession will be executed : '))
while True :
if counter == 0 :
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else :
aux = n1 + n2
n1,n2 = n2,aux
counter += 1
if aux <= limit :
yield aux
else :
raise StopIteration

if __name__ == '__main__':
fibonacci = Fibo_gen()
for element in fibonacci:
print(element)
time.sleep(1)
``````

Mi solución con la propuesta de @José Ricardo Perez y @David Guerrero Morales Guerrero

``````import time
from typing import Counter

def fibo_gen(limit: int):
n1, n2  = 0, 1
counter = 1
while counter <= limit:
yield n1
n1, n2 = n2, n1+n2
counter += 1

if __name__ == '__main__':
limit = int(input("Ingrese el límite para la generación de la serie: "))
fibonacci = fibo_gen(limit)
for element in fibonacci:
print(element)
time.sleep(1)

``````

Este es mi código para el reto de esta clase:

``````import time

def fibo_gen(max_number):
n1 = 0
n2 = 1
counter = 0

while True:
if counter == 0 and n1 <= max_number:
counter += 1
yield n1
elif counter == 1 and n2 <= max_number:
counter += 1
yield n2
else:
aux = n1 + n2
if aux <= max_number:
n1, n2 = n2, aux
counter += 1
yield aux
else:
break

if __name__ == '__main__':
fibonacci = fibo_gen(0)
for element in fibonacci:
print(element)
time.sleep(0.5)
``````

``````import time

def fibo_gen(max = None):
n1 = 0
n2 = 1
counter = 0

while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2

if max == None or aux <= max:
n1, n2 = n2, aux
counter += 1
yield aux
else:
break

if __name__ == "__main__":
fibonacci = fibo_gen(15)
for element in fibonacci:
time.sleep(0.5)
print(element)
``````

Reto cumplido!!

``````import time

def func_fibo(max = 1):
n1 = 0
n2 = 1
counter = 0
while counter < max:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

if __name__ == '__main__':
fibonacci = func_fibo(10)
for element in fibonacci:
print(element)
time.sleep(1)
``````

Mi solución al Reto

Versión del Reto

``````import time

def fibo_gen():
n1=0
n2=1
counter=0

while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
elif counter == 25:
break
else:
aux = n1+n2
n1,n2 = n2,aux
counter +=1
yield aux

if __name__ == '__main__':
fibonacci = fibo_gen()
for element in fibonacci:
print(element)
time.sleep(0.05)
``````

CHALLENGE :

``````import time

def fibo_gen():
number=int(input('Enter a maximum number: '))
n1 = 0
n2 = 1
counter = 0
while counter <= number-1:
if counter == 0:
counter +=1
yield n1
elif counter ==1:
counter +=1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter +=1
if number < aux:
break
yield aux

if __name__ == '__main__':
fibonacci =fibo_gen()
for element in fibonacci:
print(element)
time.sleep(1)

``````
``````import time

def fibo_gen_challenge(max=None):
n1 = 0
n2 = 1
counter = 0

while True:
if counter == 0 and max >= n1:
counter += 1
yield n1
elif counter == 1 and max >= n2:
counter += 1
yield n2
else:
aux = n1 + n2
if aux <= max:
yield aux
elif aux > max:
break

n1, n2 = n2, aux
counter += 1

if __name__ == '__main__':
fibo_ch = fibo_gen_challenge(938)

for element in fibo_ch:
print(element)
time.sleep(1)
``````
``````import time

def fibo_gen(max):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
if not max or aux <= max:
n1, n2 = n2, aux
counter += 1
yield aux
else:
raise StopIteration

def run():
max = input("Ingrese un máximo: ")
assert max == "" or (max.isnumeric() and int(
max)) != 0, "Sólo se permiten enteros positivos, o presione enter para no ingresar un máximo"
if max.isnumeric():
max = int(max)
fibonacci = fibo_gen(max)
for element in fibonacci:
print(element)
time.sleep(1)

if __name__ == '__main__':
run()

``````
``````import time

def fibo_gen():
n1=0
n2=1
counter=0

while True:
if counter ==0:
counter+=1
aux=0
yield n1

elif counter ==1:
counter+=1
yield n2

else:
aux  = n1 + n2
if aux < max:
n1, n2 = n2 , aux
counter += 1
yield aux

else:
raise StopIteration

if __name__ == "__main__":
max = int(input("Ingresa el número máximo: "))
fibonacci = fibo_gen()
for element in fibonacci:
print(element)
time.sleep(1)
``````
``````def fibo_gen(Max):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
elif counter < Max:
counter +=1
naux = n1 +n2
n1, n2 = n2, naux
yield naux

if __name__ == '__main__':
fibonacci = fibo_gen(15)
for element in fibonacci:
print(element)

``````
``````import time

def fibo_gen(max: int):
n1, n2 = 0, 1
while n1 <= max:
yield n1
n1, n2 = n2, n1 + n2

if __name__ == '__main__':
max = int(input("Ingrese la cantidad: "))
fibonacci = fibo_gen(max)
for element in fibonacci:
print(element)
time.sleep(1)
``````
``````import time

def fibo_gen(max:int=None):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
if max is not None and aux >= max:
break
yield aux

if __name__ == '__main__':
fibonacci = fibo_gen(50)
for i in fibonacci:
print(i)
time.sleep(0.5)
if i > 100:
break
``````
``````import time

def Fibonacci_Gen(max):
n1 = 0
n2 = 1
counter = 1

while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
if aux <= max:
yield aux
else:
return False

if __name__ == '__main__':
fibonacci = Fibonacci_Gen

for element in fibonacci(22):
print(element)
time.sleep(0.5)

``````

``````from typing import Generator
import time

def fibo_gen(max:int) -> Generator:
n1: int = 0
n2: int = 1
counter: int = 0

while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux: int = n1 + n2

if aux > max:
raise StopIteration
else:

n1,n2 = n2, aux
counter += 1
yield aux

if __name__ == "__main__":
fibonacci = fibo_gen

for element in fibonacci(144):
print(element)
time.sleep(0.05)```
``````

Agregué la condición de finalización a un BOOLEAN, así se sale del while y termina la iteración

``````import time

def Fibo_gen(max):
a = True
n1 = 0
n2 = 1
counter = 0

while a == True:
if counter == 0:
counter += 1
yield n1

elif counter == 1:
counter += 1
yield n2
else:
if counter < max:
aux = n1 + n2
n1, n2 = n2 , aux
counter += 1
yield aux
else:
a = False

if __name__=='__main__':
max = int(input("Ingresa el el número máximo de elementos de la serie que quieres..."))
assert  type(max) == int, "debes ingresar un número entero"
fibonacci = Fibo_gen(max)
for element in fibonacci:
print (element)
time.sleep(0.5)
``````

lo que hice, fue incluir una variable ‘max’ que recibe el generador como parámetro y que condiciona la ejecución del ciclo while hasta que la variable counter llegue hasta el valor de max:

``````import time

def fibo_gen(max: int):
n1 = 0
n2 = 1
counter = 0
while counter<= max:
if counter == 0:
counter += 1
yield n1
elif counter ==1:
counter += 1
yield n2
else:
aux = n1+n2
n1, n2 = n2, aux
counter +=1
yield aux

if __name__ == '__main__':
fibonacci = fibo_gen(60)
for element in fibonacci:
print(element)
time.sleep(1)
``````

Aquí mi reto:

``````def fibo_gen(maximo: int):
n1 = 0
n2 = 1
counter = 0
num_maxi = 0

while num_maxi <= maximo:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
num_maxi = aux
yield aux

if __name__ == "__main__":
fibonacci = fibo_gen
for element in fibonacci(100):
print(element)
time.sleep(0.5)
``````

Va mi aporte!!

``````import time

def fibo_gen():
n1 = 0
n2 = 1
counter = 0
aux = 0

while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

if __name__ == '__main__':
fibonacci = fibo_gen()
num = int(input("Ingrese hasta que numero desea contar: "))
for element in fibonacci:
if element >= num:
break
print(element)
time.sleep(0.5)
print("Fin del ciclo!!!")
``````
``````import time

def fibo_gen(max):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter +=1
yield n1
elif counter ==1:
counter += 1
yield n2
elif n2 >= max:
break
else:
aux = n1 + n2
n1,n2 = n2,aux
counter +=1
yield aux

if __name__ == "__main__":
fibonnaci = fibo_gen(int(input("Ingrese el numero maximo: ")))
for element in fibonnaci:
print(element)
time.sleep(1)
``````

Acá dejo mi código:

``````#Fibonacci Susession
#0,1,1,2,3,5,8,13,21,34,55,89,144...

def fibonacci_gen(n):
"""
Fucntion that implements an iterator of all elements in
Fibonacci's susession.
"""
num1 = 0
num2 = 1
counter = 0
while counter <= n:
if counter == 0:
counter +=1
yield num1
elif counter == 1:
counter +=1
yield num2
elif not n or counter < n:
result = num1 + num2
num1,  num2 = num2, result
counter += 1
yield result

fibo = fibonacci_gen(13)
for i in fibo:
print(i)
``````

Buen ejercicio

Hola, mi solución lo mas sencillo posible.

### Mi solucion al reto

Mi versión del reto

``````import time

def fibo_gen(max = None):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
result = n1
elif counter == 1:
counter +=1
result = n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
result = aux
if max:
if result > max:
break
yield result

if __name__ == "__main__":
fibonacci = fibo_gen(200)
for element in fibonacci:
print(element)
time.sleep(0.05)
``````

Mi solución:

``````import time

def fibo_gen():
n1 = 0
n2 = 1
counter = 0
aux = 0
max_number = int(input("Escribe el número maximo que deseas: "))
while True:
if counter == 0:
counter+=1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
while (n2+n1) < max_number:
aux = n1 + n2
n1, n2 = n2, aux
counter+=1
yield aux
else:
break

if __name__ == "__main__":
fibonacci = fibo_gen()
for element in fibonacci:
print(element)
time.sleep(1)
``````
``````def fibo_gen(amount_max=None, number_max=None):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
counter += 1
aux = n1 + n2
n1 = n2
n2 = aux
yield n2

if (amount_max and counter >= amount_max) or \
(number_max and n2 >= number_max):
break

fibo_first5 = [i for i in fibo_gen(amount_max=5)]
fibo_number13 = [i for i in fibo_gen(number_max=13)]

print('Los numeros Fibonacci hasta el 13')
print(fibo_number13)

print('\n')
print('Los primeros 5 numeros de Fibonacci')
print(fibo_first5)
``````

Los numeros Fibonacci hasta el 13
[0, 1, 1, 2, 3, 5, 8, 13]

Los primeros 5 numeros de Fibonacci
[0, 1, 1, 2, 3]

Reto:

``````import time

def fibo_gen():
n1 = 0
n2 = 1
max = int(input("Ingrese el numero maximo de repeticiones "))
counter = 0
while max > counter:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

if __name__ == '__main__':
fibonacci = fibo_gen()
for element in fibonacci:
print(element)
time.sleep(1)
``````

import time

def fibo_gen(num_max:int):
n1 = 0
n2 = 1
counter= 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
if not num_max or aux < num_max:
n1, n2 = n2, aux
counter += 1
yield aux
else:
break

if name == “main”:
num = int(input("Indique un numero entero: "))
fibonacci = fibo_gen(num)
for element in fibonacci:
print(element)
time.sleep(.1)

Mi solución:

``````import time

def fibo_gen(maxi: int):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
if aux <= maxi:
n1, n2 = n2, aux
counter += 1
yield aux
else:
raise StopIteration

if __name__ == "__main__":
maxi = int(input('Escribe un límite mayor a cero: '))

while maxi == 0:
maxi = int(input('Escribe un número diferente a cero: '))

fibonacci = fibo_gen(maxi)
for element in fibonacci:
print(element)
time.sleep(.5)

print('\nFin de la sesión...')
``````

Reto:

Reto:

El Reto Que puso El Profe Acepto Cualquier Critica

``````import time

def fibo_gen():
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

def usuario_gen():
Maximo = int(input("Ingrese el numero maximo de fibo_gen: "))
return Maximo

if __name__ == '__main__':
maximo = usuario_gen()
fibonacci = fibo_gen()
for element in fibonacci:
if element > maximo:
break
print(element)
time.sleep(0.5)
``````
``````import time

def gen_fibo(max=None):

n1, n2, counter = 0, 1, 0
while not max or counter < max:
yield n1
counter += 1
n1, n2 = n2, n1 + n2

if __name__ == '__main__':
while True:
max_itter = input("Ingrese la cantidad de números de la succesion que "
"desea ver: ")
if max_itter:
try:
max_itter = int(max_itter)
break
except ValueError:
print("Error, Recuerde que debe ingresar números enteros o dejarlo"
"vacio en caso de querer ver la succesion infinitamente")
else:
break

fibonacci = gen_fibo(max_itter)
for element in fibonacci:
print(element)
time.sleep(1)
``````

Un par de modificaciones y veámonos

``````import time

maximo = int(input("Numero maximo: "))

def fibo_gen():
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
if not maximo or aux <= maximo:
n1, n2 = n2, aux
counter += 1
yield aux
else:
raise StopIteration

if __name__ == '__main__':
# Instance
fibonacci = fibo_gen()
for element in fibonacci:
print(element)
time.sleep(0.05)

``````

Yo le puse return, pero sería mejor un StopIteration? o un .close para cerrar el generador?

``````import time

def fibo_gen(stop):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
elif counter > stop - 1:
return
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

if __name__ == '__main__':
fibonacci = fibo_gen(5)
for element in fibonacci:
print(element)
time.sleep(0.5)
``````

coloque un maximo de 13 para el generador fibonacci

``````from time import sleep

def fiboGen():
a, b = 0, 1
while a <=13:
yield a
a , b = b, a+b
raise StopIteration

if __name__ == "__main__":
fibonacci = fiboGen()
for element in fibonacci:
print(element)
sleep(1)

``````

``````import time

def fibo_gen(max_counter:int):
n1 = 0
n2 = 1
counter = 0

while counter<=max_counter:
if counter == 0:
counter +=1
yield n1
elif counter == 1:
counter +=1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

if __name__ == '__main__':
numero = int(input("Ingrese el numero maximo fibonacci: "))
fibonacci = fibo_gen(numero)

for element in fibonacci:
print(element)
time.sleep(1)
``````

``````import time

def fibo_gen(max):
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
counter += 1
n1, n2 = n2, aux
if not max:
yield aux
if max:
if aux > max:
break
else:
yield aux

def main():
max = int(input('Enter a maximum number: ') or 0)
fibonacci = fibo_gen(max)
for i in fibonacci:
print(i)
time.sleep(0.05)

if __name__ == '__main__':
main()
``````

Usando type hints y la nueva feature de Python 3.10, el esperado match-case.

Fue lo unico que se me ocurrió para parar la devolucion infinita jajaja

``````def fibo_gen():
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux
if n2 == 5:
exit()
``````

Aca van mis soluciones al reto:

Solución 1:

``````import time
def fibo_gen(userlimit: int) -> int:
n1 = 0
n2 = 1
counter = 0
aux = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
if aux >= userlimit:
break
else:
yield aux

if __name__ == '__main__':

userlimit = int(input("Ingresa el numero limite de la secuencia: "))
fibonacci = fibo_gen(userlimit)
for element in fibonacci:
print(element)
time.sleep(.05)
``````

solución 2

``````import time

def fibo_gen():
n1 = 0
n2 = 1
counter = 0
while True:
if counter == 0:
counter += 1
yield n1
elif counter == 1:
counter += 1
yield n2
else:
aux = n1 + n2
n1, n2 = n2, aux
counter += 1
yield aux

if __name__ == '__main__':

fibonacci = fibo_gen()
userlimit = int(input("Ingrese el numero limite de la secuencia: "))
for element in fibonacci:
if element >= userlimit:
break
else:
print(element)
time.sleep(.05)
``````

my code

``````import time

def fibo_gen(maxim):  # se crea el generador
maximum = maxim
n1 = 0
n2 = 1
counter = 0
while True:
if counter < maximum:
if counter == 0:
counter += 1
yield n1    #yield es una especie de return que no termina la function, sino que pausa el estado de la funtion
elif counter == 1:
counter += 1
yield n2    #delvuleve el valor y pausa el estado de la funtion
else:
aux = n1 + n2
n1, n2 = n2, aux # hago un intercambio suave
counter += 1
yield aux
else:
raise StopIteration

if __name__ =='__main__':
maxim = int(input("enter number limite to fibonacci: "))
fibonacci = fibo_gen(maxim)
for element in fibonacci:
print(element)
time.sleep(0.5)
``````

Facundo es el maestro sayayin del Python