Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import itertools
- import numpy as np
- def combinations_permuted(n_elm):
- items = list(range(0,n_elm+1,1))
- combinations = pd.DataFrame(list(filter(lambda x: np.sum(x)==n_elm,list(itertools.combinations_with_replacement(items, n_elm)))))
- comb_permuted = pd.DataFrame()
- for index, combination in combinations.iterrows():
- comb_permuted=comb_permuted.append(list(filter(lambda x: np.sum(x)==n_elm,list(itertools.permutations(combination.tolist(), n_elm)))))
- return(comb_permuted.drop_duplicates().as_matrix())
- array([[0, 0, 3],
- [0, 3, 0],
- [3, 0, 0],
- [0, 1, 2],
- [0, 2, 1],
- [1, 0, 2],
- [1, 2, 0],
- [2, 0, 1],
- [2, 1, 0],
- [1, 1, 1]], dtype=int64)
- def combinations_permuted(n_elm):
- items = list(range(0,n_elm+1,1))
- combinations = pd.DataFrame(list(filter(lambda x: np.sum(x)==n_elm,list(itertools.combinations_with_replacement(items, n_elm)))))
- comb_permuted = pd.DataFrame()
- for index, combination in combinations.iterrows():
- comb_permuted=comb_permuted.append(list(filter(lambda x: np.sum(x)==n_elm,list(itertools.permutations(combination.tolist(), n_elm)))))
- return(comb_permuted.drop_duplicates().as_matrix())
- def combinations_recursive_inner(n, buf, gaps, sum, accum):
- if gaps == 0:
- accum.append(list(buf))
- else:
- for x in range(0, n+1):
- if sum + x + (gaps - 1) * n < n: continue
- if sum + x > n: break
- combinations_recursive_inner(n, buf + [x], gaps - 1, sum + x, accum)
- def combinations_recursive(n):
- accum = []
- combinations_recursive_inner(n, [], n, 0, accum)
- return pd.DataFrame(accum).as_matrix()
Add Comment
Please, Sign In to add comment