Reply To: Programming
The mystery of the silver bullet › Forums › The Intelligence Room › Programming › Reply To: Programming
Looking at it, I realize that I called factorial() twice for each iteration, which is too inefficient. Here, I fixed it:
def nth_permutation(n,objects): perm = [] n -= 1 while len(objects) > 0: k = factorial(len(objects)-1) m = int(n/k) n -= m * k perm.append(objects[m]) objects.pop(m) return perm
@ByteInBits, you said
Normally I would smile and let it pass but I have to take issue with you
to come to some understanding.In post #98171 you state:
@ByteInBits, your MD5 sum is for the NEXT permutation. By my example, you should start with 1 and not 0.This make you incorrect with the entry number!!
(standard practice doese not start at 1, and you never stated the deviation)
But I did. When I said:
Notice that I listed them in a sort of numerical order: 123, 132, 213, 231, 312, 321. There are 3! = 6 of them. If I were to ask you what is the fourth permutation of 1, 2, 3, you should answer “2, 3, 1”. OK, got it?
I was very careful about making sure the list started at 1, so I said “fourth” instead of “third”. I even stopped to ask if everyone understood before I went on.
Never wager against a Sicilian when death is on the line.