How to resolve the algorithm Sort disjoint sublist step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sort disjoint sublist step by step in the zkl programming language

Table of Contents

Problem Statement

Given a list of values and a set of integer indices into that value list, the task is to sort the values at the given indices, while preserving the values at indices outside the set of those to be sorted. Make your example work with the following list of values and set of indices: Where the correct result would be: In case of one-based indexing, rather than the zero-based indexing above, you would use the indices {7, 2, 8} instead. The indices are described as a set rather than a list but any collection-type of those indices without duplication may be used as long as the example is insensitive to the order of indices given.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sort disjoint sublist step by step in the zkl programming language

Source code in the zkl programming language

values :=T(7, 6, 5, 4, 3, 2, 1, 0);
indices:=T(6, 1, 7);

indices.apply(values.get).sort() // a.get(0) == a[0]
  .zip(indices.sort()) //-->(v,i) == L(L(0,1),L(1,6),L(6,7))
  .reduce(fcn(newList,[(v,i)]){ newList[i]=v; newList },values.copy())
  .println();  // new list

values :=L(7, 6, 5, 4, 3, 2, 1, 0);

indices.apply(values.get).sort() // a.get(0) == a[0]
  .zip(indices.sort()) //-->(v,i) == L(L(0,1),L(1,6),L(6,7))
  .apply2(fcn([(v,i)],list){ list[i]=v },values);

values.println();  // modified list

  

You may also check:How to resolve the algorithm Higher-order functions step by step in the REBOL programming language
You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the Java programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the E programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Vim Script programming language
You may also check:How to resolve the algorithm Polyspiral step by step in the Yabasic programming language