Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

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:

  1. Find largest index i such that array[i − 1] < array[i].

    Find largest index i such that array[i − 1] < array[i].

  2. Find largest index j such that j ≥ i and array[j] > array[i − 1].

  3. Swap array[j] and array[i − 1].

  4. Reverse the suffix starting at array[i].

  1. Find largest index j such that j ≥ i and array[j] > array[i − 1].
  1. Swap array[j] and array[i − 1].
  2. 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:

  1. Find largest index i such that array[i − 1] < array[i].
  1. Find largest index j such that j ≥ i and array[j] > array[i − 1].
  1. Swap array[j] and array[i − 1].
  2. 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:

  1. Find largest index i such that array[i − 1] < array[i].

  2. Find largest index j such that j ≥ i and array[j] > array[i − 1].

  3. Swap array[j] and array[i − 1].

  4. 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))
Source Link
Caridorc
  • 28k
  • 7
  • 54
  • 137

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:

  1. Find largest index i such that array[i − 1] < array[i].
  1. Find largest index j such that j ≥ i and array[j] > array[i − 1].
  1. Swap array[j] and array[i − 1].
  2. 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))
lang-py

AltStyle によって変換されたページ (->オリジナル) /