combinations




Pay Notebook Creator: Salah Ahmed0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0

Binomial Coefficient

Choose k from n items

{items_text: items} {k_int: k}

In [1]:
# Crosscompute
items_text_path = './colors.txt'
k_int = 2

target_folder = './results'
In [2]:
from os.path import join
k, items = k_int, open(items_text_path, 'r').readlines()
target_path = join(target_folder, 'options.csv')
In [3]:
items = [x.strip() for x in items if x.strip()]
In [4]:
n = len(items)
In [5]:
from math import factorial
In [6]:
def binomial_coef(n, k):
    num = factorial(n)
    denom = factorial(k) * (factorial(n - k))
    return int(num / denom)
In [7]:
sol = binomial_coef(n, k)
print('binomial_coefficient = %d' % sol)
binomial_coefficient = 21
In [8]:
from itertools import combinations
possible_choices = (combinations(items, k))
In [10]:
with open(target_path, 'w') as f:
    for choice in possible_choices:
        f.write(','.join(choice) + '\n')
In [11]:
print('choices_table_path = %s' % target_path)
choices_table_path = ./results/options.csv