Invierte en tu educación con el precio especial

Antes:$249

Currency
$209

Paga en 4 cuotas sin intereses

Paga en 4 cuotas sin intereses
Suscríbete

Termina en:

12d

10h

47m

08s

2

Calcular las combinaciones de una lista

Un concepto matemático modelado con recursión

deffactorial(n):if n <= 1:
        return1return n * factorial(n-1)

defhow_many_combinations(n, r):return factorial(n) / (factorial(n -r)*factorial(r))

defcalc_combination(items, r, len, start_from):    
    take = r - 1for i in range(start_from, len - take): 
        if r == 1:
            yield [items[i]]
        else:
            item = items[i]
            for j in calc_combination(items, r-1, len, i + 1):
                yield [item] + j

defcombination(items, r):return calc_combination(items, r, len(items), 0)

if __name__ == '__main__':
    print(a, b)
    data = ['A', 'E', 'I', 'O', 'U']
    group = 3
    gen = combination(data, group)
    n = how_many_combinations(len(data), group)
    print(f"The number of combinations are: {n}")
    whileTrue:
        try:
            print(next(gen))
        except StopIteration:
            break```
Escribe tu comentario
+ 2
2
39924Puntos
4 años

En el post anterior se me paso borrar un print. Lo subo de nuevo

deffactorial(n):if n <= 1:
        return1return n * factorial(n-1)

defhow_many_combinations(n, r):return factorial(n) / (factorial(n -r)*factorial(r))

defcalc_combination(items, r, len, start_from):    
    take = r - 1for i in range(start_from, len - take): 
        if r == 1:
            yield [items[i]]
        else:
            item = items[i]
            for j in calc_combination(items, r-1, len, i + 1):
                yield [item] + j

defcombination(items, r):return calc_combination(items, r, len(items), 0)

if __name__ == '__main__':
    print(a, b)
    data = ['A', 'E', 'I', 'O', 'U']
    group = 3
    gen = combination(data, group)
    n = how_many_combinations(len(data), group)
    print(f"The number of combinations are: {n}")
    whileTrue:
        try:
            print(next(gen))
        except StopIteration:
            break