Aún no tienes acceso a esta clase

Crea una cuenta y continúa viendo este curso

Curso Profesional de Python

Curso Profesional de Python

Facundo García Martoni

Facundo García Martoni

Programando closures

9/21
Recursos

Aportes 413

Preguntas 10

Ordenar por:

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

Hola,

Encontré que se puede hacer el reto usando una función lambda. El resultado sería el siguiente.

Reto

Podemos usar lambda functions aquí:

reto ampliado:

Pyrhon 3.9

def tabla_multiplicar(x: int) -> int:
    def multiplicaciones(n: int) -> int:
        my_tabla = {i: i*x for i in range(0,n)}
        return my_tabla
    return multiplicaciones


def run():
    tabla_del_5 = tabla_multiplicar(5)
    tabla_del_5_hasta_330 = tabla_del_5(330)
    cinco_multiplicado_por = tabla_del_5_hasta_330
    print(cinco_multiplicado_por[221])
    
if __name__ == "__main__":
    run()

La manera más absurda de multiplicar, aunque se puede sacar toda la tabla en un diccionario, lo cual no está mal jeje muy potente esto de las closure, bravísimo curso 😃

Mi aporte saludos

def make_division_by(n):
    def division(x: int) -> float:
        assert n > 0, "El valor ingresado en el divisor debe ser mayor a cero"
        return round(x/n, 2)
    return division

def run():
    division_by_3 = make_division_by(3)
    division_by_5 = make_division_by(5)
    division_by_18 = make_division_by(18)
    print(division_by_3(18))
    print(division_by_5(100))
    print(division_by_18(54))
    print(division_by_3(division_by_5(200)))

if __name__ == "__main__":
    run()

Humilde aporte

def make_division_by(n: int): 
    def division(x: int):
        assert type(x) == int, 'You have to type an int number'
        return x/n
    return division


def run():
    division_by_3 = make_division_by(3)
    print(int(division_by_3(18)))

    division_by_5 = make_division_by(5)
    print(int(division_by_5(100)))

    division_by_18 = make_division_by(18)
    print(int(division_by_18(54)))


if __name__ == '__main__':
    run()

Hola a todos, como andan?

Programe e siguiente codigo

<def divisor(number):

    def dividendo(n):

        return n//number
    
    return number

def run():

    divided_by_2= divisor(3)
    divided_by_1point5= divisor(5)
    divided_by_1000= divisor(18)

    number= int(input('Please insert your number: '))
    divided_by_2(number)
    divided_by_1point5(number)
    divided_by_1000(number)

    print(f'''Your number was {number}.
              Divided by 3 is: {divided_by_2(10)},
              Divided by 5 is: {divided_by_1point5(10)},
              Divided by 18 is: {divided_by_1000(10)} ''')

if __name__== '__main__':

    run()>

Probe con el numero 10, y me arroja :

Traceback (most recent call last):
File “c:/Users/adrie/Desktop/Cursos/Curso-profesional-python/make_division_by.py”, line 27, in <module>
run()
File “c:/Users/adrie/Desktop/Cursos/Curso-profesional-python/make_division_by.py”, line 16, in run
divided_by_2(number)
TypeError: ‘int’ object is not callable

No puedo encotnrar el error, alguine podria decirme com ose repara?

Mi aporte

def make_division_by(n: int):
    def division(d: int) -> float:
        assert n != 0, "No se puede dividir entre cero"
        return d / n
    return division



def run():
    division_by_3 = make_division_by(0)
    print(division_by_3(18))

    division_by_5 = make_division_by(5)
    print(division_by_5(100))

    division_by_18 = make_division_by(18)
    print(division_by_18(54))

if __name__ == "__main__":
    run()
def make_division_by(n):
    def divisioner(x):
        assert type(x) == int, "Solo puedes ingresar numeros positivos"
        return int(x / n)
    return divisioner


def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))
    division_by_5 = make_division_by(5)
    print(division_by_5(100))
    division_by_18 = make_division_by(18)
    print(division_by_18(54))

if __name__ == '__main__':
    run()

Mi código

def divisor(d):
    def make_division(D):
        return D/d
    return make_division

def run():
    div_5 = divisor(5)
    print(div_5(50))
    
if __name__ == '__main__':
    run()
def make_division_by(n:int)->int:
    def division(x:int)->int:
        assert type(x)==int, "solo se pueden ingresar numeros enteros"
        return x/n
    return division
def run():
    division_by_3 = make_division_by(3)
    print(int(division_by_3(18)))

    division_by_5 = make_division_by(5)
    print(int(division_by_5(100)))

    division_by_18 = make_division_by(18)
    print(int(division_by_18(54)))

    
    
    
if __name__=="__main__":
    run()
def make_division_by(n):
    def division(x):
        return x/n
    return division


def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18)) #6

    division_by_5 = make_division_by(5)
    print(division_by_5(100)) #20

    division_by_18 = make_division_by(18)
    print(division_by_18(54)) #3

if __name__ == "__main__":
    run()

Lo hice siguiendo el curso y con capacidad de dividir decimales y negativos.

Reto cumplido:

def make_division_by(n):
    """This closure returns a functions that returns the division 
    of an x number by n"""
    # You have to code here!
    def divided_by(x):
        assert type(x) == int, "Solo puedes usar numeros"
        result = x/n
        return result
    return divided_by    

division_by_3 = make_division_by(3)
print(division_by_3(18)) # The expected output is 6

division_by_5 = make_division_by(5)
print(division_by_5(100)) # The expected output is 20

division_by_18 = make_division_by(18)
print(division_by_18(54)) # The expected output is 3
def make_division_by(n):
  def division(m):
    return m/n
  return division

def run():
  division_by_3 = make_division_by(3) 
  print(division_by_3(18))
  division_by_5 = make_division_by(5) 
  print(division_by_5(100))
  division_by_18 = make_division_by(18) 
  print(division_by_18(54))

if __name__== '__main__':
  run()

Aquí va el mío!

def makeDivisionBy(n: int):
    def numerator(x: int) -> float:
        try:
            if type(n) != int or type(x) != int:
                raise ValueError("Solo puedes ingresar numeros enteros")
            return x/n
        except ValueError as ve:
            print(ve)
            return "Operación cancelada..."

    return numerator

def main():
    divisonBy3 = makeDivisionBy("A")
    print(divisonBy3(18))

    divisonBy5 = makeDivisionBy(5)
    print(divisonBy5(100))

    divisonBy18 = makeDivisionBy(18)
    print(divisonBy18(54))


if __name__ == "__main__":
    main()

Saludos ✌🤞

def make_division_by(n):
    """This closure returns a function that returns the division of an x number by n"""
    def division(x):
        return x//n
    return division

def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))

    division_by_3 = make_division_by(5)
    print(division_by_3(100))

    division_by_3 = make_division_by(18)
    print(division_by_3(54))

if __name__ == "__main__":
    run()
def make_division_by(n):
    def divi(d):
        return d/n
    return divi

def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))

    division_by_3 = make_division_by(5)
    print(division_by_3(100))

    division_by_3 = make_division_by(18)
    print(division_by_3(54))

if __name__=='__main__':
    run()

Hice alreves la clase xD pero bueno si funciona

def multiplier(name: str) -> str:
    # Tener una nested function
    def mult_name(quantity: int) -> str:

        # assertstatement <condicion>, <"mensaje">
        assert type(name) == str, "Solo puedes ingresar texto"

        # referenciar un valor de un scope superior
        return name * quantity

    # La función que envuelve la nested debe retornarse
    return mult_name


def run():
    name_to_mult = multiplier("Alejandro")
    print(f" {name_to_mult(4)} ")


if __name__ == "__main__":
    run()

Tarea

def make_division_by(n: int) -> float:
    """This closure returns a function that returns the division of an x number by n"""

    def division(x: int) -> float:
        assert type(x) == int, "Error: Solo ingresar numeros"
        return x / n

    return division


def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))

    division_by_5 = make_division_by(5)
    print(division_by_5(100))

    division_by_18 = make_division_by(18)
    print(division_by_18(55))

    division_by_18 = make_division_by(9)
    print(division_by_18(85))


if __name__ == "__main__":
    run()

Aquí mi resultado:

def make_repeater_of(n):
    def repeater(string):
        assert type(string) == str, "Sólo puedes utilizar cadenas"
        return string*n
    return repeater

def divisor(coc):
    assert type(coc) == int, "Sólo puedes utilizar enteros"
    def dividendo(div):
        assert type(div) == int, "Sólo puedes utilizar enteros"
        return div/coc
    return dividendo

def run():
    repeat_5 = make_repeater_of(5)
    print(repeat_5("Hola "))
    div_by_5 = divisor(5)
    print(div_by_5(100))

if __name__=="__main__":
    run()

Reto

def make_division_by(n):
    def dividir(z: int) -> float:
        assert n > 0, "El Divisor debe ser mayor a cero"
        return z/n
    return dividir

def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))
    division_by_5 = make_division_by(5)
    print(division_by_5(100))
    division_by_18 = make_division_by(18)
    print(division_by_18(54))
  
if __name__ == "__main__":
    run()

My exercise.!

def make_division_by(n):
    def division(divisor):
        return divisor // n
    return division

def run():
    division_by_3 = make_division_by(3)
    division_by_5 = make_division_by(5)
    division_by_18 = make_division_by(18)

    print(division_by_3(18))
    print(division_by_5(100))
    print(division_by_18(54))

if __name__ == '__main__':
    run()
def make_division_by(n) -> int:

    def division(number: int) -> int:
        return number / n
    return division


if __name__ == '__main__':
    divider_3 = make_division_by(3)
    divider_5 = make_division_by(5)
    divider_18 = make_division_by(18)
    
    print(divider_3(18))
    assert divider_3(18) == 6, 'Is Incorrect'

    print(divider_5(100))
    assert divider_5(100) == 20, 'Is Incorrect'
    
    print(divider_18(54))
    assert divider_18(54) == 3, 'Is Incorrect'

Reto

def make_division_by(n):
    def num_by(x):
        assert type(x) != str, "Debems ingresar un entero"
        return x / n
    return num_by
   

def run():
    dividir = make_division_by(4)
    print(dividir(8))

if __name__ == "__main__":
    run()

make_division_by

def make_division_by(divisor):
    def division(numerador):
        return numerador / divisor
    return division

def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))

    division_by_5 = make_division_by(5)
    print(division_by_5(100))

    division_by_18 = make_division_by(18)
    print(division_by_18(54))


if __name__ == "__main__":
    run()

Mi solucion del reto con tipado estatico

def make_division_by(n: int) -> int:
    def division(x: int) -> int:
        return x // n
    return division

def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))
    division_by_5 = make_division_by(5)
    print(division_by_5(100))
    division_by_18 = make_division_by(18)
    print(division_by_18(54))

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

Mi solución al reto propuesto en clase:

from typing import Callable

def make_division_by(n: int) -> Callable[[int], float]:
    def division(x: int) -> float:
        assert type(x) == int, "Solo se pueden ingresar números"
        return x/n
    return division

def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))
    
    division_by_5 = make_division_by(5)
    print(division_by_5(100))

    division_by_18 = make_division_by(18)
    print(division_by_18(54))

if __name__ == '__main__':
    run()

Comparto mi solución al reto:


def make_division_by(n):
    def number_2be_divided(number):
        return number / n
    return number_2be_divided

def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))
    division_by_5 = make_division_by(5)
    print(division_by_5(100))
    division_by_18 = make_division_by(18)
    print(division_by_18(54))

if __name__ == '__main__':
    run()


def make_division_by(n):
def division(div):
assert type(div) == int, "Solo puedes utilizar numeros enteros"
return div / n
return division

def run():
division_by_3 = make_division_by(3)
print(division_by_3(18))
division_by_3 = make_division_by(5)
print(division_by_3(100))
division_by_3 = make_division_by(18)
print(division_by_3(54))

if name == ‘main’:
run()

reto:

def make_devision(n: float):
    """
        This closure returns a function that returns the devision of an x number by n
    """
    def division(x: float) -> float:
        return x / n
    return division

def run():
    n = float(input('Insert n: '))
    devision_n = make_devision(n)
    x = float(input('Insert x: '))
    print(devision_n(x))

if __name__ == '__main__':
    run()
 
from typing import Callable
def make_division_by(divisor: int) -> Callable[[int], float]:
    if divisor == 0:
        raise ZeroDivisionError
    def number(dividend: int) -> float:
        return dividend / divisor
    return number

def run():
    try:
        division_by = make_division_by(10)
        print(division_by(100))
    except ZeroDivisionError:
        print('The divisor cannot be 0')
    except TypeError:
        print('Only numbers are allowed')
    
if __name__ == '__main__':
    run()

Listo el reto utilizando el metodo K.I.S.S. 😃

# Define clousure function
def make_division_by(n):
    ''' This clousure return a function as with the division results
        of the x number by n
    '''
    # Define nested function for the clousure
    def divide(x):
        return x // n
    return divide


# Principal function
def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))

    division_by_5 = make_division_by(5)
    print(division_by_5(100))

    division_by_18 = make_division_by(18)
    print(division_by_18(54))


# Entry point
if __name__ == '__main__':
    run()

Reto cumplido 😀. También le agregué tipado estático y casos de error al reto y al código original del profe.
.
Espero que les pueda servir de ayuda. ✌🏻
.

Mi resultado del reto:

def make_division_by(n):
    assert n != 0 , "no puede ser dividido por cero" 

    def division(m):
        return m/n

    return division

def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))
    division_by_5 = make_division_by(5)
    print(division_by_5(100))
    division_by_18 = make_division_by(18)
    print(division_by_18(54))

if __name__ == '__main__':
    run()
def make_repeater_off(n):
    def repeater(string:str):
        assert type(string) == str, "Solo puedes utilizar cadenas"
        return string*n
    return repeater

def make_division_by(n:int):
    def divisor(x:int)->float: 
        assert n!=0, 'No puedes dividir para cero'
        return x/n
    return divisor

def main():


    repeat_5 = make_repeater_off(5)
    #La funcion make_repeater_off es guardada en repeat
    # 5 y esta recuerda el valor 5.
    divide_by5 = make_division_by(5)
    print(divide_by5(25))

    print(repeat_5("Hola!`"))



if __name__ == '__main__': 
    main()
def make_repeater_of(n):
    def repeater(string):
        assert type(string) == str, "Solo puedes usar string"
        return string * n
    return repeater

def make_division_by(n):
    def division(div):
        return div // n
        if type(div) != int and div < 0:
            assert "Ingrese un NUMERO por favor y que sea mayor a uno"
    return division

def run():
    repeat_5 = make_repeater_of(5)
    division_by_3 = make_division_by(3)
    division_by_5 = make_division_by(5)
    division_by_18 = make_division_by(18)
    print(division_by_3(18))
    print(division_by_5(100))
    print(division_by_18(54))

if __name__ == '__main__':
    run()
    

The challenge: (Viva el Platzi day)

closure_exercise.py

def make_division_by(n: int) -> float:
    """
    this closure return a function that returns the division
    of an x number by n
    """
    assert n != 0, "n no debe ser cero"
    assert type(n) in [int, float], "debe es un numero entero o flotante"

    def division(number: int) -> float:
        assert type(number) in [int, float], "debe ser un numero entero o flotante"
        return number/n
    
    return division

closure_test.py

from closure_exercise import make_division_by
from unittest import TestCase
import unittest

class TestingFunctions(TestCase):
    """Tests to know if the methods works well"""
    
    def test_is_palindrome(self):
        """Testing is_palindrome method"""
        division_by_3 = make_division_by(3)
        division_by_5 = make_division_by(5)
        division_by_18 = make_division_by(18)

        self.assertEqual(division_by_3(18), 6.0)
        self.assertEqual(division_by_5(100), 20.0)
        self.assertEqual(division_by_18(54), 3.0)

if __name__ == '__main__':
    unittest.main()
[Running] python -u "c:\Users\HP\Documents\GitHub\2__Script-Cursos_Platzi\Data Science\24 Curso Profesional de Python\9_closure_test.py"
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

[Done] exited with code=0 in 0.534 seconds

he hecho solo 2 scripts básicos para aportar a la comunidad
solo paso por aqui porque es el platzi day y estoy refrescando mis conocimientos
lo cual me da mucha alegria 😃

con funcion lambda:

def make_division_by(n):
    return lambda x: x / n 

Con una funcion normal:

def make_division_by(n: int) -> callable:
    assert type(n) == int, "Ingresa un numero entero"

    def divided_number(x: int) -> int:
        assert type(n) == int, "Ingresa un numero entero"
        return x / n
    return divided_number 
def make_division_by(n):
    def dividend(x):
        assert type(x) == int, 'Solo puedes ingresar enteros'
        return x / n
    return dividend


def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))

    division_by_5 = make_division_by(5)
    print(division_by_5(100))

    division_by_18 = make_division_by(18)
    print(division_by_18('54'))



if __name__ == '__main__':
    run()

Mi solución:

def make_division_by(n):
    """This clousure returns a function that returns the division of an x number by n"""

    assert type(n) == int and n != 0, 'Valor inválido: ha de ser un número diferente a 0'

    def division(x):
        assert type(x) == int, 'Valor inválido: ha de ser un número' 
        return x / n
    return division


def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))

    division_by_5 = make_division_by(5)
    print(division_by_5(100))

    division_by_18 = make_division_by(18)
    print(division_by_18(54))


if __name__ == '__main__':
    run()

Resolucion del reto, pero tengo una duda, como se haria para no repetir la funcion de division?

def make_division(n: int):
    try:
        assert n != 0, "n debe ser mayor a cero"
        def division(x: int) -> int:
            try:
                assert type(x) == int, "x debe ser un entero"
                assert type(n) == int, "n debe ser un entero"
                return int(x / n)
            except Exception as err:
                print(err)
                return 0
        return division
    except Exception as err:
        print(err)
        n = 1
        def division(x: int) -> int:
            try:
                assert type(x) == int, "x debe ser un entero"
                assert type(n) == int, "n debe ser un entero"
                return int(x / n)
            except Exception as err:
                print(err)
                return 0
        return division


def run():
    division_by_3 = make_division(3)
    print(division_by_3(18))

    division_by_5 = make_division(5)
    print(division_by_5(100))

    division_by_18 = make_division(18)
    print(division_by_18(54))

    division_by_9 = make_division(9)
    print(division_by_9("asd"))

    division_by_0 = make_division(0)
    print(division_by_0(10))

if __name__ == '__main__':
    run()

reto

def make_division_by(n):
    def dividend(x):
        return x / n
    return dividend
def run():
    #
    divisor_by_3 = make_division_by(3)
    print(divisor_by_3(6))
    divisor_by_5 = make_division_by(3)
    print(divisor_by_3(15))
    


if __name__ == '__main__':
    run() 
def make_division_by(n:int):
    assert type(n)==int ,"Solo puedes ingresar numeros"
    def division(num:int):
        assert type(num)==int ,"Solo puedes ingresar numeros"
        return num/n
    return division
    
division_by_3 = make_division_by(3)
    print(division_by_3(18)) # 6

    division_by_5 = make_division_by(5)
    print(division_by_5(100)) # 20
    
    division_by_18 = make_division_by(18)
    print(division_by_18(54)) # 3
    
    division_by_other = make_division_by(82)
    print(division_by_other("4"))



def make_division_by(n: int):
""“This closure returns a function that returns the division of an x number by n”""
assert n > 0, "El valor debe ser mayor a cero"
def divisor(x: int) -> float:
return x/n
return divisor

def run():
division_by_3 = make_division_by(3)
print(division_by_3(18)) # The expected output is 6

division_by_5 = make_division_by(5)
print(division_by_5(100))    # The expected output is 20

division_by_18 = make_division_by(18)
print(division_by_18(54))    # The expected output is 3 

if name == ‘main’:
run()

el código de la clase

# hola 3 -> holaholahola
# carlos 2  -> carlos carlos carlos

#Closure, función inferior llama a superior

def make_repeater_of(n):
    def repeater(string):
        assert type(string) == str, "solo puedes usar cadenas"
        #Afirmamos que lo que nos llega a este nested function es un string
        
        return string * n
    return repeater

#función principal
def run():
    repeat_5 = make_repeater_of(5)
    #Se ejecuta nuestra función envolvente(5)
    #Crea una función interna repeter que recibe un string
    #retorna el resultado de multiplicar el string por 5
    # retorna a repeater
    print(repeat_5("hola"))
    repeat_10 = make_repeater_of(10)
    print(repeat_10("carlos"))
    

    
# holaholaholaholahola
# carloscarloscarloscarloscarloscarloscarloscarloscarloscarlos

Mi aporte

def make_division_by(n):
    def divider(x):
        assert type(x) == int, "solo puedes usar enteros"
        #Afirmamos que lo que nos llega a este nested function es un entero
        return x / n
    return divider
def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))
    division_by_5 = make_division_by(5)
    print(division_by_5(100))
    division_by_18 = make_division_by(18)
    print(division_by_18(54))
if __name__ == "__main__":
    run()
    
#     6.0
# 20.0
# 3.0

mi pequeño aporte

def make_division_by(n):
    def division(x):
        return x / n
    return division

def run():
    devision_by_number = make_division_by(float(input("ingresa un numero a dividr ")))
    print(devision_by_number(float(input("ingresa el otro numero a dividir "))))

if __name__ == "__main__":
    run()

Este es mi aporte 😅

``
def make_division_by(n):
    """ This clousure returns a function that returns the division
        of an x number by n
    """
    def division(x):
        return x / n
    return division

division_by_3 = make_division_by(3)
print(division_by_3(18)) #The expected output is 6.0

division_by_5 = make_division_by(5)
print(division_by_5(100)) #The expected output is 20.0

division_by_18 = make_division_by(18)
print(division_by_18(54)) #The expected output is 
```3.0
```

Asi quedo mi codigo amigos.

def make_division(n):
    def division(number):
        assert type(number) == int, "solo puedes ingresar numeros"
        return number/n
    return division    


def run():
    
    division_3 = make_division(3)
    print(division_3(18))
    division_5 = make_division(5)
    print(division_5(100))
    division_18 = make_division(18)
    print(division_18(54))


if __name__ == "__main__":
    run()

mi pequeño aporte:

def make_division_by(n):
    def division(x):
        return x / n
    return division

def run():
    division_3 = make_division_by(3)
    print(division_3(18))

    division_5 = make_division_by(5)
    print(division_5(100))

    division_18 = make_division_by(18)
    print(division_18(54))

if __name__ == '__main__':
    run()

Solución al reto:

def make_division(x: int):
    def division(y: int) -> int:
        assert type(y) == int, "You can only input intigers"
        return y / x
    return division


def run():

    division_by_3 = make_division(3)
    print(division_by_3(18))

    division_by_5 = make_division(5)
    print(division_by_5(100))

    division_by_18 = make_division(18)
    print(division_by_18(54))


if __name__ == "__main__":
    run()

Esta es mi forma de resolver el reto 😄

def make_division_of(n):
    return lambda x: x / n




def run():
	# otra forma de hacerlo
    # n = int(input('Enter a number: '))
    # p = int(input('Enter another number: '))
    # division = make_division_of(n)
    # print(int(division(p)))

    division_by_3 = make_division_of(3)
    print(int(division_by_3(18)))

    division_by_5 = make_division_of(5)
    print(int(division_by_5(100)))

    division_by_18 = make_division_of(18)
    print(int(division_by_18(54)))


if __name__ == '__main__':
    run()

Aporte

def make_division_by(n):
    def numerator(x):
        assert type(x) == int or type(x) == float, "You need to type a number!"
        return x/n
    return numerator

def run():
    division_by_3 = make_division_by(3)
    print(division_by_3("asdasd"))

    division_by_5 = make_division_by(5)
    print(division_by_5(100))

    division_by_18 = make_division_by(18)
    print(division_by_18(54))

if __name__=='__main__':
    run()

comparto el reto:

from typing import Callable

def make_division_by(n: float) -> Callable [[float], float]:
    def divider(number: float) -> float:
        # assert type(n) == int, "solo puedes usar numeros"
        assert n > 1, "solo puedes usar numero mayores a 1 "
        return number / n
    return divider

def run():
    divide_5 = make_division_by(2.8)
    print(divide_5(20))


if __name__ == '__main__':
    run()

con ayuda de ustedes agregue el flotante(decimales). Notaran que elimine un assert, no supe como elegir que aceptara numeros enteros y numeros decimales.

Intenté aplicarlo a tablas de multiplicar

def make_division_by(n):
    def divider(x):
        assert type(n) == int, "Solo puedes usar números enteros positivos"
        return x / n
    return divider


def run():

    division_by_3 = make_division_by(3)
    print(division_by_3(18)) # 6

    division_by_5 = make_division_by(5)
    print(division_by_5(100)) # 20

    division_by_18 = make_division_by(18)
    print(division_by_18(54)) # 3

if __name__ == '__main__':
    run()

Hola les comparto mi aporte para el reto

def division_by(x: int):
    def number_input(n: int) -> float:
        try:
            if x == 0:
                raise ValueError("No esta definido, division para 0")
            return n / x
        except ValueError as e:
            return print(e)

    return number_input

    
def run():
    num_div = int(input("Ingresa el numero para el dividendo: "))
    num_by = int(input("Ingresa el numero para el divisor: "))
    
    make_division = division_by(num_by)
    print(f"El resultado es: {make_division(num_div)}")


if __name__ == '__main__':
    run()
def make_division_by(n):
    def dividen(x):
        assert type(n)==int and n!=0,"you can´t divide by zero"
        return x / n
    return dividen


def run():
    divsion_by_3 = make_division_by(5)
    print(divsion_by_3(18))
    divsion_by_5 = make_division_by(5)
    print(divsion_by_5(100))
    divsion_by_10 = make_division_by(10)
    print(divsion_by_10(54))


if __name__ == '__main__':
    run()

Challenge 😎

def make_division_by(n):
    def division_num(x):
        assert type(x) == int, "Solo dividir numeros enteros"
        return x // n
    return division_num


def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))
    division_by_5 = make_division_by(5)
    print(division_by_5(100))
    division_by_18 = make_division_by(18)
    print(division_by_18(54))


if __name__ == '__main__':
    run()

Retillo

def makeDivisionBy(n):
    """
    This closure return a function that returns the division
    of an x number by n
    """
    def division(x):
        assert x != 0, "No se pueden dividir entre 0"
        return x / n
    return division

def main():
    divisionBy3 = makeDivisionBy(3)
    print(divisionBy3(18))
    
    divisionBy5 = makeDivisionBy(5)
    print(divisionBy5(100))
    
    divisionBy18 = makeDivisionBy(18)
    print(divisionBy18(54))
    

if __name__ == '__main__':
    main() 
def make_division_by(n):
    """This closure return a function that returns the division of an x number by n
    """
    def division(x):
        return x/n
    return division    

Dejo mi código del desafío:

def make_division_by(n):
    def division(x):
        return x / n
    return division

division_by_3 = make_division_by(3)
division_by_5 = make_division_by(5)
division_by_18 = make_division_by(18)

print(division_by_3(18))
print(division_by_5(100))
print(division_by_18(54))
def make_divison_by(n):
    def divison(x):
        return x / n
    return divison


def main():
    division_by_3 = make_divison_by(3)
    print(division_by_3(18))
    division_by_5 = make_divison_by(5)
    print(division_by_5(100))
    division_by_18 = make_divison_by(18)
    print(division_by_18(54))


if __name__ == "__main__":
    main()

def numerador(numerador:int) -> int:
def division(denominador):
assert type(denominador)==int, "Solo puedes ingresar números"
assert denominador!=0, "No puedes ingresar cero"
return numerador/denominador
return division

def run():
division_by_3=numerador(18)
print(division_by_3(3))
division_by_3=numerador(100)
print(division_by_3(5))
division_by_3=numerador(54)
print(division_by_3(18))
if name==“main”:
run()

mi codigo del reto

def make_division_by(x:int)-> float:
    def division(n: int) -> float:
        assert type(n) == int and n > 0 and x > 0 ,"solo se puede dividir números enteros"
        return round(n / x, 3)
    return division


def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))
    division_by_5 =make_division_by(5)
    print(division_by_5(100))
    division_by_18 = make_division_by(18)
    print(division_by_18(54))


if __name__ == '__main__':
    run()

Aquí mi aporte

def division_by(n: int) ->float:
  def division(x: int) ->float:
    assert type(n) == int and n > 0, "El  número deber ser entero y mayor a cero."
    return x / n

  return division    


def run():
  division_2 = division_by(2)
  print(division_2(20))

  division_4 = division_by(4)
  print(division_4(20))

  division_3 = division_by(0)
  print(division_3(18))


if __name__ == '__main__':
  run()
# def repeater_phrase(n):
#     def repeater(string):
#         assert type(string) == str ,'Only use strings'
#         return string * n
#     return repeater
      
# def run():
#     repeat5 = repeater_phrase(5)
#     print(repeat5('Hi '))
#     repeat7 = repeater_phrase(7)
#     print(repeat7('Galilea '))
# def make_division_by(n):
#     def number(x):
#         assert type(x) == int,'Only use integer numbers'
#         return x / n
#     return number
def make_division_by(n):
    return lambda x : x / n
def run():
    divided_by_3 = make_division_by(3)
    print(divided_by_3(int(input('Enter number : '))))
    divided_by_5 = make_division_by(5)
    print(divided_by_5(100))
    divided_by_18 = make_division_by(18)
    print(divided_by_18(54))
  
if __name__ == '__main__':
    run()

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

def make_division_by(n):
    def division(x):
        assert type(x) == int, 'Solo puedes utilizar números enteros'
        return x / n
    return division
def make_division_by(n):
    
    def division(x):
        return x // n

    return division


def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))

    division_by_5 = make_division_by(5)
    print(division_by_5(100))

    division_by_18 = make_division_by(18)
    print(division_by_18(54))


if __name__ == '__main__':
    run()

Reto completado

def make_division_by(n):
    def division(x):
        return x / n
    return division


def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))

if __name__ == '__main__':
    run()
def make_division_by(n:int):
    def division(number:int) -> int:
        assert number==0,"not posible division between 0"
        return number / n
    return division

def run():
    div_by_5 = make_division_by(3)
    print(div_by_5(0))
    div_by_5 = make_division_by(5)
    print(div_by_5(100))
    div_by_5 = make_division_by(18)
    print(div_by_5(54))

Reto cumplido:)

def haz_divisiones(num):
def dividendo(div):
assert type(div) == int, "\n***>> Error <<***\nIntroduzca sólo un número\n"
return div / num
return dividendo

def run():
dividir_por_3 = haz_divisiones(3)
dividir_por_5 = haz_divisiones(5)
dividir_por_10 = haz_divisiones(10)

print(dividir_por_3(18))
print(dividir_por_5(20))
print(dividir_por_10(80))
print(dividir_por_3("18")) # Aquí se provoca un error

if name == “main”:
run()

def make_division_by(n):
    def division_by(x):
        return x/n
    return division_by

def run():
    divi= make_division_by(3)
    print(divi(18))
    
    divi= make_division_by(5)
    print(divi(100))
    
    divi= make_division_by(18)
    print(divi(54))
    
if __name__=='__main__':   
    run()
def make_division_by(n):
    def division(number):
        assert type(number) == int, "Solo puedes pasar números enteros"
        return int(number / n)
    return division


def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18)) 

    division_by_5 = make_division_by(5)
    print(division_by_5(100)) 

    division_by_18 = make_division_by(18)
    print(division_by_18(54)) 


if __name__ == '__main__':
    run()

def make_division_by(n):
def repeater(numero):
assert type(numero) == int, "Solo puedes utilizar números"
return numero / n
return repeater

def run():
division_by_3 = make_division_by(3)
print(division_by_3(18))

division_by_5 = make_division_by(5)
print(division_by_5(100))

division_by_18 = make_division_by(18)
print(division_by_18(54))

if name == ‘main’:
run()

Facundo siempre dejando el tema sumamente claro c:

def make_division(n):
    def division(x):
        return x / n
    return division

Reto:

def make_division_by(n: int)-> int:
    '''This closure returns a function that returns the division of an x number by n'''
    assert type(n) == int, 'You have to enter an integer'

    def division(x: int)-> int:
        assert type(x) == int, 'You have to enter an integer'
        return x // n
    return division

def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18)) # The expected output is 6

    division_by_5 = make_division_by(5)
    print(division_by_5(100)) # The expected output is 20

    division_by_18 = make_division_by(18)
    print(division_by_18(54)) # The expected output is 3

if __name__ == '__main__':
    run()
def make_division_by(n):
    def division(x):
        assert type(x) == int, "Solo puedes utilizar enteros"
        return x / n
    
    return division    

def run():
    division_by_3 = make_division_by(3)    
    print(division_by_3(18))
    division_by_5 = make_division_by(5)
    print(division_by_5(100))
    division_by_18 = make_division_by(18)
    print(division_by_18(54))

if __name__ == "__main__":
    run()  

Obteniendo el cociente de dividir por 18 un número ingresado, que debe cumplir ( múltiplo de 3, entero positivo y diferente de cero):

def make_division(quotient):
    def division(div):        
         return div/quotient              
    return division    

def run():
    while True:
        try:
            input_number =int( input('Enter a positve number  multitpe of 3 different of zero = '))
            if input_number > 0 and (input_number%3==0):                         
                make_division_3=make_division(input_number)
                print('18 % ' + str(input_number) +  ' = ' + str(make_division_3(18)))
            else:
                print('Please, Must enter a nmber that is positve multiple of 3 and different of zero') 
        except ValueError: print('Must Enter a int number')

if __name__ == '__main__':
    run()   

Reto Cumplido, plus asserts de que ni el numerador y el divisor sean 0.

#!/usr/bin/python3

def make_division_by (divisor: int):
    assert divisor != 0,ZeroDivisionError

    def divide_by (num: int):
        assert num != 0, ZeroDivisionError
        return num / divisor

    return divide_by

if __name__ == "__main__":
    divide_by_3 = make_division_by(3)
    print(divide_by_3(21))

    divide_by_0 = make_division_by(0)
    print(divide_by_0(0))

Hice el código de la clase totalmente alrevez, pero aún así funciona.

Joe Hawley Joe Hawley
Yelwah Eoj Yelwah Eoj

#!/usr/bin/python3

def make_word_repeater (word: str):
    assert type(word) == str, "string only"
    def word_repeater (num_reps: int):
        final_word: str = word * num_reps

        return final_word

    return word_repeater

if __name__ == "__main__":
    joe_hawley_attacks = make_word_repeater("Joe Hawley ")
    print(joe_hawley_attacks(64))
def make_division_by(n:float):
    def number(m:float):
        d = m / n 
        return d
    return number

division_by_3 = make_division_by(3)
print(division_by_3(18))


division_by_5 = make_division_by(5)
print(division_by_5(100))

division_by_18 = make_division_by(18)        
print(division_by_18(54))


def make_division_by(n):

    def division(x):
        assert type(x) == int, "You must use an integer"
        return x/n
    
    return division

def run():
    division_3 = make_division_by(3)
    print(division_3(18))

    division_5 = make_division_by(5)
    print(division_5(100))

    division_18 = make_division_by(18)
    print(division_18(54))
def make_division_by(n):
    def division(x):
        assert type(x) == int, "x must be an integer"
        return x / n
    return division

def run():
    divide_by_2 = make_division_by(2)
    print(divide_by_2(10)) # 5.0

    divide_by_3 = make_division_by(3)
    print(divide_by_3(10))  # 3.33

    divide_by_4 = make_division_by(4)
    print(divide_by_4(20))  # 5.0

if __name__ == "__main__":
    run()

CODIGO:

def make_division_by(divisor):
    def divider(dividend):
        assert type(dividend) == int, "Incorrect input"
        return dividend / divisor
    return divider

def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))
    division_by_5 = make_division_by(5)
    print(division_by_5(100))
    division_by_18 = make_division_by(18)
    print(division_by_5(54))

if __name__ == '__main__':
    run() 

RESULTADO:

6.0
20.0
10.8

Process finished with exit code 0 

Hola, aquí esta mi ejercicio:

def make_division_by(n):
    def division(x):        
        return x/n
    return division
  
def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))
    division_by_5 = make_division_by(5)
    print(division_by_5(100))
    division_by_18 = make_division_by(18)
    print(division_by_18(54))

if __name__ == "__main__":
    run()

Comunidad les comparto mi solución al reto.

def make_divisors_by(n):
    def divisors(integer):
        assert type(integer) == int, "Solo puedes utilizar numeros enteros"
        return integer / n
    return divisors

def run():
    divisions_by_3 = make_divisors_by (3)
    print(divisions_by_3(18)) 
    divisions_by_5 = make_divisors_by (5)
    print(divisions_by_5(100)) 
    divisions_by_18 = make_divisors_by (18)
    print(divisions_by_18(54)) 

if __name__== '__main__':
    run()
def make_division_by(n):
    '''This closures returns a function that returns the 
        divison of an x number by n 
    '''
    def divide(x):
        return x / n
    
    return divide

def run():
    divide_by2 = make_division_by(2)
    print(divide_by2(18))

    divide_by5 = make_division_by(5)
    print(divide_by5(40))

if __name__ == '__main__':
    run()

mi ejercicio:

def make_division_by(n: int):
    
    def division(x: int) -> float:
        return x/n

    return division


def main():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))

    division_by_5 = make_division_by(5)
    print(division_by_5(100))

    division_by_18 = make_division_by(18)
    print(division_by_18(54))



if __name__ == '__main__':
    main()
    

Aquí humildemente mi resolución del ejercicio 😁

def make_division_by(n):
    assert type(n) == int, "Solo puedes usuar números enteros"
    def divisor(d):
        assert type(d) == int, "Solo puedes usuar números enteros"
        return d/n
    return divisor

division_by_3 = make_division_by(3)
print(division_by_3(18))

division_by_5 = make_division_by(5)
print(division_by_5(100))

division_by_18 = make_division_by(18)
print(division_by_18(54))

Reto:

Mi solución al reto

Reto 🚀

from typing import Callable


def make_division_by(n: int) -> Callable[[int], float]:

    def division(x: int) -> float:
        # Testing
        assert x != 0, "You cannot divide by zero"
        return x/n

    return division


def run():
    division_5 = make_division_by(5)
    print(division_5(100))
    # Expected behavior
    # 20


if __name__ == "__main__":
    run()

Comparto mi versión.

El reto

def make_division_by(n: int):
    assert ((type(n) == int) & (n != 0)), "Sólo puedes utilizar enteros distintos de 0"
    def division(i: int) -> float:
        assert type(i) == int, "Sólo puedes utilizar enteros"
        return i / n
    return division

def run():
    division_by_3 = make_division_by(3)
    print(division_by_3(18))
    division_by_5 = make_division_by(5)
    print(division_by_5(100))
    division_by_18 = make_division_by(18)
    print(division_by_18(54))

if __name__ == "__main__":
    run()

Les comparto mi resultado usando un typing más amplio de enteros 😃

from decimal import Decimal
from typing import Callable, Union

NumericType = Union[int, float, Decimal]

def make_repeater_of(times: int) -> Callable:
    def repeater(word: str) -> str:
        assert type(word) == str, 'Sólo puedes usar cadenas'
        return word * times

    return repeater


def make_division_by(divider: NumericType) -> Callable:
    def division(dividend: NumericType) -> NumericType:
        return dividend / divider

    return division


def run() -> None:
    repeat_5: Callable = make_repeater_of(5)
    print(repeat_5('Hola'))

    repeat_10: Callable = make_repeater_of(10)
    print(repeat_10('Foo'))

    division_by_3: Callable = make_division_by(3)
    print(division_by_3(18))

    division_by_5: Callable = make_division_by(5)
    print(division_by_5(100))

    division_by_18: Callable = make_division_by(18)
    print(division_by_18(54))


if __name__ == '__main__':
    run()