Guest User

Untitled

a guest
Mar 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. import pandas as pd
  2. import itertools
  3. import numpy as np
  4.  
  5. def combinations_permuted(n_elm):
  6. items = list(range(0,n_elm+1,1))
  7. combinations = pd.DataFrame(list(filter(lambda x: np.sum(x)==n_elm,list(itertools.combinations_with_replacement(items, n_elm)))))
  8. comb_permuted = pd.DataFrame()
  9. for index, combination in combinations.iterrows():
  10. comb_permuted=comb_permuted.append(list(filter(lambda x: np.sum(x)==n_elm,list(itertools.permutations(combination.tolist(), n_elm)))))
  11.  
  12. return(comb_permuted.drop_duplicates().as_matrix())
  13.  
  14. array([[0, 0, 3],
  15. [0, 3, 0],
  16. [3, 0, 0],
  17. [0, 1, 2],
  18. [0, 2, 1],
  19. [1, 0, 2],
  20. [1, 2, 0],
  21. [2, 0, 1],
  22. [2, 1, 0],
  23. [1, 1, 1]], dtype=int64)
  24.  
  25. def combinations_permuted(n_elm):
  26.  
  27. items = list(range(0,n_elm+1,1))
  28.  
  29. combinations = pd.DataFrame(list(filter(lambda x: np.sum(x)==n_elm,list(itertools.combinations_with_replacement(items, n_elm)))))
  30. comb_permuted = pd.DataFrame()
  31. for index, combination in combinations.iterrows():
  32. comb_permuted=comb_permuted.append(list(filter(lambda x: np.sum(x)==n_elm,list(itertools.permutations(combination.tolist(), n_elm)))))
  33.  
  34. return(comb_permuted.drop_duplicates().as_matrix())
  35.  
  36. def combinations_recursive_inner(n, buf, gaps, sum, accum):
  37. if gaps == 0:
  38. accum.append(list(buf))
  39. else:
  40. for x in range(0, n+1):
  41. if sum + x + (gaps - 1) * n < n: continue
  42. if sum + x > n: break
  43. combinations_recursive_inner(n, buf + [x], gaps - 1, sum + x, accum)
  44.  
  45. def combinations_recursive(n):
  46. accum = []
  47. combinations_recursive_inner(n, [], n, 0, accum)
  48. return pd.DataFrame(accum).as_matrix()
Add Comment
Please, Sign In to add comment