How to resolve the algorithm Next highest int from digits step by step in the 11l programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Next highest int from digits step by step in the 11l programming language
Table of Contents
Problem Statement
Given a zero or positive integer, the task is to generate the next largest integer using only the given digits*1.
The above could prove slow and memory hungry for numbers with large numbers of digits, but should be easy to reason about its correctness.
E.g.: This second algorithm is faster and more memory efficient, but implementations may be harder to test. One method of testing, (as used in developing the task), is to compare results from both algorithms for random numbers generated from a range that the first algorithm can handle.
Calculate the next highest int from the digits of the following numbers:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Next highest int from digits step by step in the 11l programming language
Source code in the 11l programming language
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)))
You may also check:How to resolve the algorithm Sierpinski pentagon step by step in the Raku programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the KonsolScript programming language
You may also check:How to resolve the algorithm Amicable pairs step by step in the Julia programming language
You may also check:How to resolve the algorithm Shoelace formula for polygonal area step by step in the zkl programming language
You may also check:How to resolve the algorithm Modular exponentiation step by step in the PARI/GP programming language