Sadly you use an extremely complicated algorithm about which I have not even the slightest clue.
I was lucky enough to follow this link given by @Jaime that suggest a trivially easy algorithm:
- Find largest index i such that array[i − 1] < array[i].
Find largest index i such that array[i − 1] < array[i].
Find largest index j such that j ≥ i and array[j] > array[i − 1].
Swap array[j] and array[i − 1].
Reverse the suffix starting at array[i].
- Find largest index j such that j ≥ i and array[j] > array[i − 1].
- Swap array[j] and array[i − 1].
- Reverse the suffix starting at array[i].
Converting this into code gives:
def _next_permutation(array):
i = max(i for i in range(1, len(array)) if array[i - 1] < array[i])
j = max(j for j in range(i, len(array)) if array[j] > array[i - 1])
array[j], array[i - 1] = array[i - 1], array[j]
array[i:] = reversed(array[i:])
Just 4 lines, one for each line in the pseudo-code. This surely is more readable and maintainable than the huge quantity of code you wrote.
Then finishing the problem is just writing a thin wrapper around this that converts numbers to lists of digits and back:
def next_permutation(n):
array = list(str(n))
_next_permutation(array)
return int(''.join(array))
Sadly you use an extremely complicated algorithm about which I have not even the slightest clue.
I was lucky enough to follow this link given by @Jaime that suggest a trivially easy algorithm:
- Find largest index i such that array[i − 1] < array[i].
- Find largest index j such that j ≥ i and array[j] > array[i − 1].
- Swap array[j] and array[i − 1].
- Reverse the suffix starting at array[i].
Converting this into code gives:
def _next_permutation(array):
i = max(i for i in range(1, len(array)) if array[i - 1] < array[i])
j = max(j for j in range(i, len(array)) if array[j] > array[i - 1])
array[j], array[i - 1] = array[i - 1], array[j]
array[i:] = reversed(array[i:])
Just 4 lines, one for each line in the pseudo-code. This surely is more readable and maintainable than the huge quantity of code you wrote.
Then finishing the problem is just writing a thin wrapper around this that converts numbers to lists of digits and back:
def next_permutation(n):
array = list(str(n))
_next_permutation(array)
return int(''.join(array))
Sadly you use an extremely complicated algorithm about which I have not even the slightest clue.
I was lucky enough to follow this link given by @Jaime that suggest a trivially easy algorithm:
Find largest index i such that array[i − 1] < array[i].
Find largest index j such that j ≥ i and array[j] > array[i − 1].
Swap array[j] and array[i − 1].
Reverse the suffix starting at array[i].
Converting this into code gives:
def _next_permutation(array):
i = max(i for i in range(1, len(array)) if array[i - 1] < array[i])
j = max(j for j in range(i, len(array)) if array[j] > array[i - 1])
array[j], array[i - 1] = array[i - 1], array[j]
array[i:] = reversed(array[i:])
Just 4 lines, one for each line in the pseudo-code. This surely is more readable and maintainable than the huge quantity of code you wrote.
Then finishing the problem is just writing a thin wrapper around this that converts numbers to lists of digits and back:
def next_permutation(n):
array = list(str(n))
_next_permutation(array)
return int(''.join(array))
Sadly you use an extremely complicated algorithm about which I have not even the slightest clue.
I was lucky enough to follow this link given by @Jaime that suggest a trivially easy algorithm:
- Find largest index i such that array[i − 1] < array[i].
- Find largest index j such that j ≥ i and array[j] > array[i − 1].
- Swap array[j] and array[i − 1].
- Reverse the suffix starting at array[i].
Converting this into code gives:
def _next_permutation(array):
i = max(i for i in range(1, len(array)) if array[i - 1] < array[i])
j = max(j for j in range(i, len(array)) if array[j] > array[i - 1])
array[j], array[i - 1] = array[i - 1], array[j]
array[i:] = reversed(array[i:])
Just 4 lines, one for each line in the pseudo-code. This surely is more readable and maintainable than the huge quantity of code you wrote.
Then finishing the problem is just writing a thin wrapper around this that converts numbers to lists of digits and back:
def next_permutation(n):
array = list(str(n))
_next_permutation(array)
return int(''.join(array))