Skip to content

Conversation

@KirilBangachev
Copy link
Contributor

Function that computes the kth lexicographic permtation of 0,1,2,...,n-1 in O(n^2) time

Function that computes the kth lexicographic permtation of 0,1,2,...,n-1 in O(n^2) time
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me... I added a couple of minor observations.

for i in range(2, n):
factorials.append(factorials[-1] * i)
assert (
0 <= k and k < factorials[-1] * n
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k is only needed once.
assert 0 <= k < factorials[-1] * n, "k out of bounds"

# Find permutation
while factorials:
factorial = factorials.pop()
number = k // factorial
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

number, k = divmod(factorial) # and delete line 34

Addressed requested changes
@cclauss cclauss merged commit 47d1795 into TheAlgorithms:master Sep 13, 2019
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Add Kth lexicographic permutation

Function that computes the kth lexicographic permtation of 0,1,2,...,n-1 in O(n^2) time

* Update kth_lexicographic_permutation.py

Addressed requested changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants