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```
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