-
-
Notifications
You must be signed in to change notification settings - Fork 46.5k
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
issue with permute_recursive #9014
Comments
def generate_permutations(nums): Recurse
Example usageinput_nums = [1, 2, 3] |
I think it's more readable to make the list copy using |
Hey. I have added a PR #9115 fixing this issue. As you mentioned, I have used nums.copy() to pass a copy of the nums list. Please review it. Thanks. |
…#9014 (TheAlgorithms#9161) * Fixes TheAlgorithms#9014 * Fixed permute_recursive() by passing nums.copy()
What would you like to share?
Your code looks mostly correct, but there's one issue in the
permute_recursive
function due to the modification of thenums
list. Lists in Python are mutable, and when you usenums.pop(0)
, it modifies the originalnums
list. This can lead to incorrect results and even an infinite loop.To fix this, you should pass a copy of the
nums
list to the recursive function. Here's the correctedpermute_recursive
function:def permute_recursive(nums: list[int]) -> list[list[int]]:
"""
Return all permutations.
The text was updated successfully, but these errors were encountered: