Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- from tabulate import tabulate
- def nth_combination(seq, n):
- #make a copy of seq so we can destroy it without hurting the original
- seq = seq[:]
- result = []
- while seq:
- n, i = divmod(n, len(seq))
- result.append(seq[i])
- del seq[i]
- return result
- def nth_product_of_combinations(table, n):
- result = []
- for row in table:
- n, combination_idx = divmod(n, math.factorial(len(row)))
- result.append(nth_combination(row, combination_idx))
- return result
- def total_possible_prodcombs(table):
- total = 1
- for row in table:
- total *= math.factorial(len(row))
- return total
- table = [[ 'alex', 'smith', 'john'], ['sharlote', 'oliver', 'liam'], ['jasper', 'aria', 'luna']]
- for i in range(total_possible_prodcombs(table)): #3! * 3! * 3! == 216
- rearranged_table = nth_product_of_combinations(table, i)
- print(i, rearranged_table)
- 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