Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def M(n, k): # n down, k across in Mahonian number triangle
- if n == 1 and k == 0:
- return 1
- elif n < 0 or k < 0 or k > n*(n-1)/2:
- return 0
- else:
- return M(n, k-1) + M(n-1, k) - M(n-1, k-n)
- def index_to_code(index, length, weight):
- n = length
- k = weight
- code = [0]*length
- total = 0
- if index > M(n+1, k): # index too high
- raise ValueError('Index is too high for the given amount of digits and digital sum.')
- while k:
- m = M(n, k)
- if m + total <= index:
- total += m
- code[n-1] += 1
- k -= 1
- elif m + total > index:
- n -= 1
- return code
- print(*reversed(index_to_code(69, 5, 5)), sep='')
Advertisement
Add Comment
Please, Sign In to add comment