Advertisement
here2share

# recursion_combinations.py

Sep 16th, 2020
1,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. # recursion_combinations.py
  2.  
  3. t = [1,2,3,4,5]
  4.  
  5. def combinations(array, tuple_length, prev_array=[]):
  6.     if len(prev_array) == tuple_length:
  7.         return [prev_array]
  8.     combs = []
  9.     for i, val in enumerate(array):
  10.         prev_array_extended = prev_array[:]
  11.         prev_array_extended.append(val)
  12.         combs += combinations(array[i+1:], tuple_length, prev_array_extended)
  13.     return combs
  14.  
  15. ttt = combinations(t, 3)
  16.  
  17. ttt.sort()
  18. for z in ttt:
  19.     print(z)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement