Skip to content

Add Kth lexicographic permutation #1179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2019
Merged

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