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

Source code in the zkl programming language

fcn nextHightest(N){	// N is int, BigInt or string -->String.  Algorithm 2
//   ds:=N.split().copy();	// mutable, int
   ds:=N.toString().split("").apply("toInt").copy(); // handle "234" or BigInt
   if(ds.len()<2) return(0);
   m:=ds[-1];
   foreach i in ([ds.len()-1 .. 0,-1]){ 
      d:=ds[i];
      if(d
         dz,j,z := ds[i,*], dz.sort().filter1n('>(d)), dz[j];
	 dz.del(j);
//	 return( ds[0,i].extend( z, dz.sort() ).concat().toInt() );
	 return( ds[0,i].extend( z, dz.sort() ).concat() );
      }
      m=m.max(d);
   }
   "0"
}

ns:=T(0, 9, 12, 21, 12453, 738440, 45072010, 95322020);
foreach n in (ns){ println("%,d --> %,d".fmt(n,nextHightest(n))) }

n:="9589776899767587796600";	// or BigInt(n)
println("%s --> %s".fmt(n,nextHightest(n)));

  

You may also check:How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the AWK programming language
You may also check:How to resolve the algorithm Text processing/2 step by step in the Erlang programming language
You may also check:How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Gambas programming language
You may also check:How to resolve the algorithm 100 doors step by step in the ARM Assembly programming language