Next highest int from digits: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added syntax colouring the hard way)
(Added 11l)
Line 64: Line 64:
:*   9589776899767587796600
:*   9589776899767587796600
<br><br>
<br><br>

=={{header|11l}}==
{{trans|Python: Algorithm 2}}

<lang 11l>F closest_more_than(n, lst)
V large = max(lst) + 1
R lst.index(min(lst, key' x -> (I x <= @n {@large} E x)))

F nexthigh(n)
V this = reversed(Array(n.map(digit -> Int(digit))))
V mx = this[0]
L(digit) this[1..]
V i = L.index + 1
I digit < mx
V mx_index = closest_more_than(digit, this[0 .< i + 1])
swap(&this[mx_index], &this[i])
this.sort_range(0 .< i, reverse' 1B)
R reversed(this).map(d -> String(d)).join(‘’)
E I digit > mx
mx = digit
R ‘0’

L(x) [‘0’, ‘9’, ‘12’, ‘21’, ‘12453’, ‘738440’, ‘45072010’, ‘95322020’,
‘9589776899767587796600’]
print(‘#12 -> #12’.format(x, nexthigh(x)))</lang>

{{out}}
<pre>
0 -> 0
9 -> 0
12 -> 21
21 -> 0
12453 -> 12534
738440 -> 740348
45072010 -> 45072100
95322020 -> 95322200
9589776899767587796600 -> 9589776899767587900667
</pre>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==