Un concepto matemático modelado con recursión
def factorial(n):
if n <= 1:
return 1
return n * factorial(n-1)
def how_many_combinations(n, r):
return factorial(n) / (factorial(n -r)*factorial(r))
def calc_combination(items, r, len, start_from):
take = r - 1
for 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
def combination(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}")
while True:
try:
print(next(gen))
except StopIteration:
break```
Curso de Complejidad Algorítmica con Python
0 Comentarios
para escribir tu comentario