Next highest int from digits: Difference between revisions
Content added Content deleted
m (used gold section header for E.g., added whitespace, added whitespace before the TOC, corrected a misspelling.) |
|||
Line 4: | Line 4: | ||
integer using only the given digits<sup>*1</sup>. |
integer using only the given digits<sup>*1</sup>. |
||
* Numbers will not be padded to the left with zeroes. |
* Numbers will not be padded to the left with zeroes. |
||
* Use all given digits, with their given multiplicity. (If a digit appears twice in the input number, it should appear twice in the result). |
* Use all given digits, with their given multiplicity. (If a digit appears twice in the input number, it should appear twice in the result). |
||
* If there is no next |
* If there is no next highest integer return zero. |
||
⚫ | |||
⚫ | |||
;Algorithm 1: |
;Algorithm 1: |
||
# Generate all the permutations of the digits and sort into numeric order. |
# Generate all the permutations of the digits and sort into numeric order. |
||
# Find the number in the list. |
# Find the number in the list. |
||
# Return the next highest number from the list. |
# Return the next highest number from the list. |
||
The above could prove slow and memory hungry for numbers with large numbers of |
The above could prove slow and memory hungry for numbers with large numbers of |
||
Line 20: | Line 22: | ||
;Algorithm 2: |
;Algorithm 2: |
||
# Scan right-to-left through the digits of the number until you find a digit with a larger digit somewhere to the right of it. |
# Scan right-to-left through the digits of the number until you find a digit with a larger digit somewhere to the right of it. |
||
# Exchange that digit with the digit on the right that is ''both'' more than it, and closest to it. |
# Exchange that digit with the digit on the right that is ''both'' more than it, and closest to it. |
||
# Order the digits to the right of this position, after the swap; lowest-to-highest, left-to-right. (I.e. so they form the lowest numerical representation) |
# Order the digits to the right of this position, after the swap; lowest-to-highest, left-to-right. (I.e. so they form the lowest numerical representation) |
||
E.g: |
|||
'''E.g.:''' |
|||
<pre> |
<pre> |
||
n = 12453 |
n = 12453 |
||
Line 38: | Line 41: | ||
This second algorithm is faster and more memory efficient, but implementations |
This second algorithm is faster and more memory efficient, but implementations |
||
may be harder to test. |
may be harder to test. |
||
is to compare results from both algorithms for random numbers generated from a |
|||
One method of testing, (as used in developing the task), is to compare results from both |
|||
range that the first algorithm can handle. |
algorithms for random numbers generated from a range that the first algorithm can handle. |
||
;Task requirements: |
;Task requirements: |
||
Calculate the next highest int from the digits of the following numbers: |
Calculate the next highest int from the digits of the following numbers: |
||
* 0 |
:* 0 |
||
* 9 |
:* 9 |
||
* 12 |
:* 12 |
||
* 21 |
:* 21 |
||
* 12453 |
:* 12453 |
||
* 738440 |
:* 738440 |
||
* 45072010 |
:* 45072010 |
||
* 95322020 |
:* 95322020 |
||
;Optional stretch goal: |
;Optional stretch goal: |
||
* 9589776899767587796600 |
:* 9589776899767587796600 |
||
<br><br> |
|||
=={{header|C}}== |
=={{header|C}}== |