Advertisement
Guest User

Untitled

a guest
Jun 15th, 2021
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. import math
  2. from tabulate import tabulate
  3.  
  4. def nth_combination(seq, n):
  5. #make a copy of seq so we can destroy it without hurting the original
  6. seq = seq[:]
  7.  
  8. result = []
  9. while seq:
  10. n, i = divmod(n, len(seq))
  11. result.append(seq[i])
  12. del seq[i]
  13. return result
  14.  
  15. def nth_product_of_combinations(table, n):
  16. result = []
  17. for row in table:
  18. n, combination_idx = divmod(n, math.factorial(len(row)))
  19. result.append(nth_combination(row, combination_idx))
  20. return result
  21.  
  22. def total_possible_prodcombs(table):
  23. total = 1
  24. for row in table:
  25. total *= math.factorial(len(row))
  26. return total
  27.  
  28. table = [[ 'alex', 'smith', 'john'], ['sharlote', 'oliver', 'liam'], ['jasper', 'aria', 'luna']]
  29. for i in range(total_possible_prodcombs(table)): #3! * 3! * 3! == 216
  30. rearranged_table = nth_product_of_combinations(table, i)
  31. print(i, rearranged_table)
  32. print(tabulate(rearranged_table, headers=["A","B", "C"], showindex=range(1,len(table)+1), tablefmt='grid'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement