Next highest int from digits: Difference between revisions

add RPL
(added Arturo)
(add RPL)
Line 2,242:
45072010 => 45072100
95322020 => 95322200
</pre>
 
=={{header|RPL}}==
{{works with|HP|48G}}
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
≪ '''IF''' DUP SIZE 1 == '''THEN''' DROP 1 GET '''ELSE''' STREAM '''END'''
≫ '<span style="color:blue">REDUCE</span>' STO
'''IF''' DUP 9 > '''THEN'''
DUP MANT IP SWAP →STR DUP SIZE 0 → digit num size pos
≪ { } digit +
2 size '''FOR''' j
num j DUP SUB STR→
'''IF''' DUP digit > '''THEN''' j 1 - ‘pos’ STO '''END'''
DUP ‘digit’ STO +
'''NEXT'''
'''IF''' pos '''THEN'''
DUP pos GET ‘digit’ STO
DUP pos 1 + size SUB
DUP digit - DUP 0 ≤ 100 * ADD
≪ MIN ≫ <span style="color:blue">REDUCE</span> digit + POS pos +
DUP2 GET ROT pos ROT PUT SWAP digit PUT
DUP ‘pos’ INCR size SUB SORT pos SWAP REPL
≪ SWAP 10 * + ≫ <span style="color:blue">REDUCE</span>
'''ELSE''' DROP num STR→ '''END'''
≫ '''END'''
≫ '<span style="color:blue">NEXTHI</span>' STO
|
<span style="color:blue">REDUCE</span> ''( { list } ≪ func ≫ → func(list) ) ''
<span style="color:blue">NEXTHI</span> ''( int → next_highest_int ) ''
if int > 9 then
initialize variables
initialize list of digits with digit #1
for j = 2 to last digit index
get digit
if higher than previous digit, store position
store digit as previous and append to list
end loop
if there is an highest int
get d1 = first digit to swap
take the rest of list
look for d2 = lowest digit being greater than d1
swap d1 and d2
sort all digits at right of d1
turn the list of digits into a number
else return the initial number
|}
{0 9 12 21 12453 738440 45072010 95322020} 1 ≪ <span style="color:blue">NEXTHI</span> ≫ DOLIST
{{out}}
<pre>
1: { 0 9 21 21 12534 740348 45072100 95322200 }
</pre>
 
1,151

edits