How to resolve the algorithm Next highest int from digits step by step in the Raku 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 Raku 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 Raku programming language

Source code in the raku programming language

use Lingua::EN::Numbers;

sub next-greatest-index ($str, &op = &infix:«<» ) {
    my @i = $str.comb;
    (1..^@i).first: { &op(@i[$_ - 1], @i[$_]) }, :end, :k;
}

multi next-greatest-integer (Int $num where * >= 0) {
    return 0 if $num.chars < 2;
    return $num.flip > $num ?? $num.flip !! 0 if $num.chars == 2;
    return 0 unless my $i = next-greatest-index( $num ) // 0;
    my $digit = $num.substr($i, 1);
    my @rest  = (flat $num.substr($i).comb).sort(+*);
    my $next  = @rest.first: * > $digit, :k;
    $digit    = @rest.splice($next,1);
    join '', flat $num.substr(0,$i), $digit, @rest;
}

multi next-greatest-integer (Int $num where * < 0) {
    return 0 if $num.chars < 3;
    return $num.abs.flip < -$num ?? -$num.abs.flip !! 0 if $num.chars == 3;
    return 0 unless my $i = next-greatest-index( $num, &CORE::infix:«>» ) // 0;
    my $digit = $num.substr($i, 1);
    my @rest  = (flat $num.substr($i).comb).sort(-*);
    my $next  = @rest.first: * < $digit, :k;
    $digit    = @rest.splice($next,1);
    join '', flat $num.substr(0,$i), $digit, @rest;
}

say "Next largest integer able to be made from these digits, or zero if no larger exists:";
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,
    95897768997675877966000000000000000000000000000000000000000000000000000000000000000000).map: { $_, -$_ };


  

You may also check:How to resolve the algorithm Rename a file step by step in the Python programming language
You may also check:How to resolve the algorithm Averages/Mean time of day step by step in the ooRexx programming language
You may also check:How to resolve the algorithm Pinstripe/Display step by step in the Befunge programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Align columns step by step in the Clojure programming language