Next highest int from digits: Difference between revisions

Content added Content deleted
(→‎{{header|Lua}}: added Lua solution)
m (syntax highlighting fixup automation)
Line 68: Line 68:
{{trans|Python: Algorithm 2}}
{{trans|Python: Algorithm 2}}


<lang 11l>F closest_more_than(n, lst)
<syntaxhighlight lang="11l">F closest_more_than(n, lst)
V large = max(lst) + 1
V large = max(lst) + 1
R lst.index(min(lst, key' x -> (I x <= @n {@large} E x)))
R lst.index(min(lst, key' x -> (I x <= @n {@large} E x)))
Line 88: Line 88:
L(x) [‘0’, ‘9’, ‘12’, ‘21’, ‘12453’, ‘738440’, ‘45072010’, ‘95322020’,
L(x) [‘0’, ‘9’, ‘12’, ‘21’, ‘12453’, ‘738440’, ‘45072010’, ‘95322020’,
‘9589776899767587796600’]
‘9589776899767587796600’]
print(‘#12 -> #12’.format(x, nexthigh(x)))</lang>
print(‘#12 -> #12’.format(x, nexthigh(x)))</syntaxhighlight>


{{out}}
{{out}}
Line 105: Line 105:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
===Permutation Version===
===Permutation Version===
<lang AutoHotkey>Next_highest_int(num){
<syntaxhighlight lang="autohotkey">Next_highest_int(num){
Arr := []
Arr := []
for i, v in permute(num)
for i, v in permute(num)
Line 138: Line 138:
res .= x[A_Index]
res .= x[A_Index]
return res
return res
}</lang>
}</syntaxhighlight>
Examples:<lang AutoHotkey>MsgBox % "" Next_highest_int(0)
Examples:<syntaxhighlight lang="autohotkey">MsgBox % "" Next_highest_int(0)
. "`n" Next_highest_int(9)
. "`n" Next_highest_int(9)
. "`n" Next_highest_int(12)
. "`n" Next_highest_int(12)
Line 146: Line 146:
. "`n" Next_highest_int(738440)
. "`n" Next_highest_int(738440)
. "`n" Next_highest_int(45072010)
. "`n" Next_highest_int(45072010)
. "`n" Next_highest_int(95322020)</lang>
. "`n" Next_highest_int(95322020)</syntaxhighlight>
{{out}}
{{out}}
<pre>0
<pre>0
Line 157: Line 157:
95322200</pre>
95322200</pre>
===Scanning Version===
===Scanning Version===
<lang AutoHotkey>Next_highest_int(num){
<syntaxhighlight lang="autohotkey">Next_highest_int(num){
Loop % StrLen(num){
Loop % StrLen(num){
i := A_Index
i := A_Index
Line 184: Line 184:
res .= v
res .= v
return res
return res
}</lang>
}</syntaxhighlight>
Examples:<lang AutoHotkey>MsgBox % "" Next_highest_int(0)
Examples:<syntaxhighlight lang="autohotkey">MsgBox % "" Next_highest_int(0)
. "`n" Next_highest_int(9)
. "`n" Next_highest_int(9)
. "`n" Next_highest_int(12)
. "`n" Next_highest_int(12)
Line 193: Line 193:
. "`n" Next_highest_int(45072010)
. "`n" Next_highest_int(45072010)
. "`n" Next_highest_int(95322020)
. "`n" Next_highest_int(95322020)
. "`n" Next_highest_int("9589776899767587796600") ; pass long numbers as text (between quotes)</lang>
. "`n" Next_highest_int("9589776899767587796600") ; pass long numbers as text (between quotes)</syntaxhighlight>
{{out}}
{{out}}
<pre>0
<pre>0
Line 206: Line 206:


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdbool.h>
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdio.h>
#include <stdint.h>
#include <stdint.h>
Line 260: Line 260:
printf("%s -> %s\n", big, next);
printf("%s -> %s\n", big, next);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 279: Line 279:
{{libheader|GMP}}
{{libheader|GMP}}
This solution makes use of std::next_permutation, which is essentially the same as Algorithm 2.
This solution makes use of std::next_permutation, which is essentially the same as Algorithm 2.
<lang cpp>#include <algorithm>
<syntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <iostream>
#include <sstream>
#include <sstream>
Line 306: Line 306:
std::cout << big << " -> " << next_highest(big) << '\n';
std::cout << big << " -> " << next_highest(big) << '\n';
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 323: Line 323:
=={{header|D}}==
=={{header|D}}==
{{trans|Java}}
{{trans|Java}}
<lang d>import std.algorithm;
<syntaxhighlight lang="d">import std.algorithm;
import std.array;
import std.array;
import std.conv;
import std.conv;
Line 446: Line 446:
testAll("12345");
testAll("12345");
testAll("11122");
testAll("11122");
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>0 -> 0
<pre>0 -> 0
Line 472: Line 472:
{{libheader| System.Generics.Collections}}
{{libheader| System.Generics.Collections}}
{{Trans|Go}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Next_highest_int_from_digits;
program Next_highest_int_from_digits;


Line 644: Line 644:
{$IFNDEF UNIX}
{$IFNDEF UNIX}
readln; {$ENDIF}
readln; {$ENDIF}
end.</lang>
end.</syntaxhighlight>


=={{header|Factor}}==
=={{header|Factor}}==
This uses the <code>next-permutation</code> word from the <code>math.combinatorics</code> vocabulary. <code>next-permutation</code> wraps around and returns the smallest lexicographic permutation after the largest one, so additionally we must check if we're at the largest permutation and return zero if so. See the implementation of <code>next-permutation</code> [https://docs.factorcode.org/content/word-next-permutation%2Cmath.combinatorics.html here].
This uses the <code>next-permutation</code> word from the <code>math.combinatorics</code> vocabulary. <code>next-permutation</code> wraps around and returns the smallest lexicographic permutation after the largest one, so additionally we must check if we're at the largest permutation and return zero if so. See the implementation of <code>next-permutation</code> [https://docs.factorcode.org/content/word-next-permutation%2Cmath.combinatorics.html here].
{{works with|Factor|0.99 2020-01-23}}
{{works with|Factor|0.99 2020-01-23}}
<lang factor>USING: formatting grouping kernel math math.combinatorics
<syntaxhighlight lang="factor">USING: formatting grouping kernel math math.combinatorics
math.parser sequences ;
math.parser sequences ;


Line 660: Line 660:
9589776899767587796600
9589776899767587796600
}
}
[ dup next-highest "%d -> %d\n" printf ] each</lang>
[ dup next-highest "%d -> %d\n" printf ] each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 676: Line 676:
=={{header|Go}}==
=={{header|Go}}==
This uses a modified version of the recursive code in the [[https://rosettacode.org/wiki/Permutations#Go Permutations#Go]] task.
This uses a modified version of the recursive code in the [[https://rosettacode.org/wiki/Permutations#Go Permutations#Go]] task.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 790: Line 790:
algorithm1(nums[:len(nums)-1]) // exclude the last one
algorithm1(nums[:len(nums)-1]) // exclude the last one
algorithm2(nums) // include the last one
algorithm2(nums) // include the last one
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 824: Line 824:
{{Trans|Python}}
{{Trans|Python}}
(Generator version)
(Generator version)
<lang haskell>import Data.List (nub, permutations, sort)
<syntaxhighlight lang="haskell">import Data.List (nub, permutations, sort)


digitShuffleSuccessors :: Integer -> [Integer]
digitShuffleSuccessors :: Integer -> [Integer]
Line 862: Line 862:


rjust :: Int -> Char -> String -> String
rjust :: Int -> Char -> String -> String
rjust n c = drop . length <*> (replicate n c <>)</lang>
rjust n c = drop . length <*> (replicate n c <>)</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Taking up to 5 digit-shuffle successors of a positive integer:
<pre>Taking up to 5 digit-shuffle successors of a positive integer:
Line 880: Line 880:
(The digit-swap approach makes it feasible to obtain successors of this kind for much larger numbers)
(The digit-swap approach makes it feasible to obtain successors of this kind for much larger numbers)


<lang haskell>import Data.List (unfoldr)
<syntaxhighlight lang="haskell">import Data.List (unfoldr)


------------------- MINIMAL DIGIT-SWAPS ------------------
------------------- MINIMAL DIGIT-SWAPS ------------------
Line 972: Line 972:


rjust :: Int -> Char -> String -> String
rjust :: Int -> Char -> String -> String
rjust n c = drop . length <*> (replicate n c <>)</lang>
rjust n c = drop . length <*> (replicate n c <>)</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Taking up to 5 digit-shuffle successors of a positive integer:
<pre>Taking up to 5 digit-shuffle successors of a positive integer:
Line 1,018: Line 1,018:
=={{header|Java}}==
=={{header|Java}}==
Additional testing is performed, including a number with all unique digits and a number with duplicate digits. Included test of all permutations, that the order and correct number of permutations is achieved, and that each permutation is different than all others. If a library is not used, then this testing will provide a better proof of correctness.
Additional testing is performed, including a number with all unique digits and a number with duplicate digits. Included test of all permutations, that the order and correct number of permutations is achieved, and that each permutation is different than all others. If a library is not used, then this testing will provide a better proof of correctness.
<lang java>
<syntaxhighlight lang="java">
import java.math.BigInteger;
import java.math.BigInteger;
import java.text.NumberFormat;
import java.text.NumberFormat;
Line 1,144: Line 1,144:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,171: Line 1,171:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>const compose = (...fn) => (...x) => fn.reduce((a, b) => c => a(b(c)))(...x);
<syntaxhighlight lang="javascript">const compose = (...fn) => (...x) => fn.reduce((a, b) => c => a(b(c)))(...x);
const toString = x => x + '';
const toString = x => x + '';
const reverse = x => Array.from(x).reduce((p, c) => [c, ...p], []);
const reverse = x => Array.from(x).reduce((p, c) => [c, ...p], []);
Line 1,214: Line 1,214:
test(95322020);
test(95322020);
test('9589776899767587796600');
test('9589776899767587796600');
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>0 => 0
<pre>0 => 0
Line 1,227: Line 1,227:


=={{header|jq}}==
=={{header|jq}}==
<lang jq># Generate a stream of all the permutations of the input array
<syntaxhighlight lang="jq"># Generate a stream of all the permutations of the input array
def permutations:
def permutations:
# Given an array as input, generate a stream by inserting $x at different positions
# Given an array as input, generate a stream by inserting $x at different positions
Line 1,258: Line 1,258:
95322020;
95322020;


task | "\(.) => \(next_highest)"</lang>
task | "\(.) => \(next_highest)"</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,271: Line 1,271:
</pre>
</pre>
=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Combinatorics, BenchmarkTools
<syntaxhighlight lang="julia">using Combinatorics, BenchmarkTools


asint(dig) = foldl((i, j) -> 10i + Int128(j), dig)
asint(dig) = foldl((i, j) -> 10i + Int128(j), dig)
Line 1,361: Line 1,361:
@btime nexthighest_2(n)
@btime nexthighest_2(n)
println(" for method 2 and n $n.")
println(" for method 2 and n $n.")
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
N 1A 1B 2
N 1A 1B 2
Line 1,387: Line 1,387:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>import java.math.BigInteger
<syntaxhighlight lang="scala">import java.math.BigInteger
import java.text.NumberFormat
import java.text.NumberFormat


Line 1,512: Line 1,512:
}
}
return sb.toString()
return sb.toString()
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>0 -> 0
<pre>0 -> 0
Line 1,537: Line 1,537:
=={{header|Lua}}==
=={{header|Lua}}==
Algorithm 2 with a reverse index of digit positions.
Algorithm 2 with a reverse index of digit positions.
<lang lua>unpack = unpack or table.unpack -- <=5.2 vs >=5.3 polyfill
<syntaxhighlight lang="lua">unpack = unpack or table.unpack -- <=5.2 vs >=5.3 polyfill
function nexthighestint(n)
function nexthighestint(n)
Line 1,569: Line 1,569:
for _,n in ipairs(tests) do
for _,n in ipairs(tests) do
print(n .. " -> " .. (nexthighestint(n) or "(none)"))
print(n .. " -> " .. (nexthighestint(n) or "(none)"))
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>0 -> (none)
<pre>0 -> (none)
Line 1,584: Line 1,584:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[NextHighestIntFromDigits]
<syntaxhighlight lang="mathematica">ClearAll[NextHighestIntFromDigits]
NextHighestIntFromDigits[n_Integer?NonNegative]:=Module[{digs},
NextHighestIntFromDigits[n_Integer?NonNegative]:=Module[{digs},
digs=IntegerDigits[n];
digs=IntegerDigits[n];
Line 1,591: Line 1,591:
If[Length[digs]==1,First[digs],RankedMin[digs,2]]
If[Length[digs]==1,First[digs],RankedMin[digs,2]]
]
]
NextHighestIntFromDigits/@{0,9,12,21,12453,738440,45072010,95322020}</lang>
NextHighestIntFromDigits/@{0,9,12,21,12453,738440,45072010,95322020}</syntaxhighlight>
{{out}}
{{out}}
<pre>{0, 9, 21, 21, 12534, 740348, 45072100, 95322200}</pre>
<pre>{0, 9, 21, 21, 12534, 740348, 45072100, 95322200}</pre>
Line 1,597: Line 1,597:
=={{header|Nim}}==
=={{header|Nim}}==
Using the scanning algorithm.
Using the scanning algorithm.
<lang Nim>import algorithm
<syntaxhighlight lang="nim">import algorithm


type Digit = range[0..9]
type Digit = range[0..9]
Line 1,639: Line 1,639:
when isMainModule:
when isMainModule:
for n in [0, 9, 12, 21, 12453, 738440, 45072010, 95322020]:
for n in [0, 9, 12, 21, 12453, 738440, 45072010, 95322020]:
echo n, " → ", nextHighest(n)</lang>
echo n, " → ", nextHighest(n)</syntaxhighlight>


{{out}}
{{out}}
Line 1,653: Line 1,653:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 1,683: Line 1,683:
for (0, 9, 12, 21, 12453, 738440, 45072010, 95322020, 9589776899767587796600, 3345333) {
for (0, 9, 12, 21, 12453, 738440, 45072010, 95322020, 9589776899767587796600, 3345333) {
printf "%30s -> %s\n", comma($_), comma next_greatest_integer $_;
printf "%30s -> %s\n", comma($_), comma next_greatest_integer $_;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 -> 0
<pre> 0 -> 0
Line 1,698: Line 1,698:
=={{header|Phix}}==
=={{header|Phix}}==
===algorithm 1===
===algorithm 1===
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">nigh</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">nigh</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)))</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)))</span>
Line 1,718: Line 1,718:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,732: Line 1,732:
</pre>
</pre>
===algorithm 2===
===algorithm 2===
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">nigh</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">nigh</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">hi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">[$]</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">hi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">[$]</span>
Line 1,759: Line 1,759:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,777: Line 1,777:
===Python: Algorithm 2===
===Python: Algorithm 2===
Like Algorithm 2, but digit order is reversed for easier indexing, then reversed on return.
Like Algorithm 2, but digit order is reversed for easier indexing, then reversed on return.
<lang python>def closest_more_than(n, lst):
<syntaxhighlight lang="python">def closest_more_than(n, lst):
"(index of) closest int from lst, to n that is also > n"
"(index of) closest int from lst, to n that is also > n"
large = max(lst) + 1
large = max(lst) + 1
Line 1,801: Line 1,801:
for x in [0, 9, 12, 21, 12453, 738440, 45072010, 95322020,
for x in [0, 9, 12, 21, 12453, 738440, 45072010, 95322020,
9589776899767587796600]:
9589776899767587796600]:
print(f"{x:>12_d} -> {nexthigh(x):>12_d}")</lang>
print(f"{x:>12_d} -> {nexthigh(x):>12_d}")</syntaxhighlight>


{{out}}
{{out}}
Line 1,817: Line 1,817:
===Python: Algorithm 1===
===Python: Algorithm 1===
I would not try it on the stretch goal, otherwise results as above.
I would not try it on the stretch goal, otherwise results as above.
<lang python>from itertools import permutations
<syntaxhighlight lang="python">from itertools import permutations




Line 1,828: Line 1,828:
if perm != this:
if perm != this:
return int(''.join(perm))
return int(''.join(perm))
return 0</lang>
return 0</syntaxhighlight>


===Python: Generator===
===Python: Generator===
Line 1,834: Line 1,834:
A variant which defines (in terms of a concatMap over permutations), a generator of '''all''' digit-shuffle successors for a given integer:
A variant which defines (in terms of a concatMap over permutations), a generator of '''all''' digit-shuffle successors for a given integer:


<lang python>'''Next highest int from digits'''
<syntaxhighlight lang="python">'''Next highest int from digits'''


from itertools import chain, islice, permutations, tee
from itertools import chain, islice, permutations, tee
Line 1,956: Line 1,956:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Taking up to 5 digit-shuffle successors for each:
<pre>Taking up to 5 digit-shuffle successors for each:
Line 1,974: Line 1,974:
Minimal error trapping. Assumes that the passed number is an integer. Handles positive or negative integers, always returns next largest regardless (if possible).
Minimal error trapping. Assumes that the passed number is an integer. Handles positive or negative integers, always returns next largest regardless (if possible).


<lang perl6>use Lingua::EN::Numbers;
<syntaxhighlight lang="raku" line>use Lingua::EN::Numbers;


sub next-greatest-index ($str, &op = &infix:«<» ) {
sub next-greatest-index ($str, &op = &infix:«<» ) {
Line 2,006: Line 2,006:
printf "%30s -> %s%s\n", .&comma, .&next-greatest-integer < 0 ?? '' !! ' ', .&next-greatest-integer.&comma for
printf "%30s -> %s%s\n", .&comma, .&next-greatest-integer < 0 ?? '' !! ' ', .&next-greatest-integer.&comma for
flat 0, (9, 12, 21, 12453, 738440, 45072010, 95322020, 9589776899767587796600, 3345333,
flat 0, (9, 12, 21, 12453, 738440, 45072010, 95322020, 9589776899767587796600, 3345333,
95897768997675877966000000000000000000000000000000000000000000000000000000000000000000).map: { $_, -$_ };</lang>
95897768997675877966000000000000000000000000000000000000000000000000000000000000000000).map: { $_, -$_ };</syntaxhighlight>
{{out}}
{{out}}
<pre>Next largest integer able to be made from these digits, or zero if no larger exists:
<pre>Next largest integer able to be made from these digits, or zero if no larger exists:
Line 2,032: Line 2,032:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program finds the next highest positive integer from a list of decimal digits. */
<syntaxhighlight lang="rexx">/*REXX program finds the next highest positive integer from a list of decimal digits. */
parse arg n /*obtain optional arguments from the CL*/
parse arg n /*obtain optional arguments from the CL*/
if n='' | n="," then n= 0 9 12 21 12453 738440 45072010 95322020 /*use the defaults?*/
if n='' | n="," then n= 0 9 12 21 12453 738440 45072010 95322020 /*use the defaults?*/
Line 2,056: Line 2,056:
end /*k*/
end /*k*/
do m=0 for 10; if @.m==0 then iterate; $= $ || copies(m, @.m)
do m=0 for 10; if @.m==0 then iterate; $= $ || copies(m, @.m)
end /*m*/; return $ /* [↑] build a sorted digit mask.*/</lang>
end /*m*/; return $ /* [↑] build a sorted digit mask.*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 2,070: Line 2,070:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"


Line 2,169: Line 2,169:
last -= 1
last -= 1
end
end
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,183: Line 2,183:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>fn next_permutation<T: PartialOrd>(array: &mut [T]) -> bool {
<syntaxhighlight lang="rust">fn next_permutation<T: PartialOrd>(array: &mut [T]) -> bool {
let len = array.len();
let len = array.len();
if len < 2 {
if len < 2 {
Line 2,218: Line 2,218:
println!("{} -> {}", n, next_highest_int(*n));
println!("{} -> {}", n, next_highest_int(*n));
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,234: Line 2,234:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func next_from_digits(n, b = 10) {
<syntaxhighlight lang="ruby">func next_from_digits(n, b = 10) {


var a = n.digits(b).flip
var a = n.digits(b).flip
Line 2,254: Line 2,254:
) {
) {
printf("%30s -> %s\n", n, next_from_digits(n))
printf("%30s -> %s\n", n, next_from_digits(n))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,276: Line 2,276:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
{{libheader|Wren-str}}
{{libheader|Wren-str}}
<lang ecmascript>import "/sort" for Sort, Find
<syntaxhighlight lang="ecmascript">import "/sort" for Sort, Find
import "/fmt" for Fmt
import "/fmt" for Fmt
import "/str" for Str
import "/str" for Str
Line 2,383: Line 2,383:
var nums = ["0", "9", "12", "21", "12453", "738440", "45072010", "95322020", "9589776899767587796600"]
var nums = ["0", "9", "12", "21", "12453", "738440", "45072010", "95322020", "9589776899767587796600"]
algorithm1.call(nums[0...-1]) // exclude the last one
algorithm1.call(nums[0...-1]) // exclude the last one
algorithm2.call(nums) // include the last one</lang>
algorithm2.call(nums) // include the last one</syntaxhighlight>


{{out}}
{{out}}
Line 2,412: Line 2,412:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fcn nextHightest(N){ // N is int, BigInt or string -->String. Algorithm 2
<syntaxhighlight lang="zkl">fcn nextHightest(N){ // N is int, BigInt or string -->String. Algorithm 2
// ds:=N.split().copy(); // mutable, int
// ds:=N.split().copy(); // mutable, int
ds:=N.toString().split("").apply("toInt").copy(); // handle "234" or BigInt
ds:=N.toString().split("").apply("toInt").copy(); // handle "234" or BigInt
Line 2,428: Line 2,428:
}
}
"0"
"0"
}</lang>
}</syntaxhighlight>
<lang zkl>ns:=T(0, 9, 12, 21, 12453, 738440, 45072010, 95322020);
<syntaxhighlight lang="zkl">ns:=T(0, 9, 12, 21, 12453, 738440, 45072010, 95322020);
foreach n in (ns){ println("%,d --> %,d".fmt(n,nextHightest(n))) }
foreach n in (ns){ println("%,d --> %,d".fmt(n,nextHightest(n))) }


n:="9589776899767587796600"; // or BigInt(n)
n:="9589776899767587796600"; // or BigInt(n)
println("%s --> %s".fmt(n,nextHightest(n)));</lang>
println("%s --> %s".fmt(n,nextHightest(n)));</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>