by Arne Sommer

# Zero Shuffle with Raku

 Published 18. July 2023.

This is my response to The Weekly Challenge #226.

## Challenge #226.1: Shuffle String

You are given a string and an array of indices of same length as string.

Write a script to return the string after re-arranging the indices in the correct order.

Example 1: ```Input: \$string = 'lacelengh', @indices = (3,2,0,5,4,8,6,7,1) Output: 'challenge' ``` Example 2: ```Input: \$string = 'rulepark', @indices = (4,7,3,1,0,5,2,6) Output: 'perlraku' ```

This surely is a task for an array slice?

Let us have a go, and find out...

File: shuffle-string-slice ```#! /usr/bin/env raku unit sub MAIN (\$string where \$string.chars > 0, #  *@indices where @indices.elems == \$string.chars); #  my @string = \$string.comb; #  say @string[@indices].join; #  ```

 A string, with at least 1 character.

 A slurpy array with the same number of elements as characters in .

Note the missing check for non-negative integers, and that the indices cover all the positions (without duplicates and holes).

 Get the string as an array of individual characters.

 Get the characters in the correct order, with an array slice, and join that array to form a string that we print.

Running it:

```\$ ./shuffle-string-slice lacelengh 3 2 0 5 4 8 6 7 1 eclelhnga ```

Oops.

We have actually translated the indices the wrong way round. That is fixable, with a little more code.

And let us add the missing error checking this time.

File: shuffle-string ```#! /usr/bin/env raku unit sub MAIN (\$string where \$string.chars > 0, #  *@indices where @indices.elems == @indices.unique.elems == \$string.chars #  && all(@indices) ~~ UInt #  && all(@indices) < \$string.chars); #  my @string = \$string.comb; #  my @out; #  for ^@string.elems -> \$i #  { @out[@indices[\$i]] = @string[\$i] #  } say @out.join; #  ```

 As before.

 Note the added check for the same length after removing duplicate indices with `unique`.

See docs.raku.org/routine/unique for more information about `unique`.

 Ensure that the indices are non-negative with the `Uint` (Unsigned Int) type.

See docs.raku.org/type/UInt for more information about the `Uint` type.

 Check that all the values are within the upper index limit. This and  and  ensures that we have all the indices we should have.

 As before.

 The unshuffled characters will end up here.

 Iterate over the indices.

 Copy the character from the input array, with the index found at the current position.

 Print the result, as a string.

Running it gives the expected result:

```\$ ./shuffle-string lacelengh 3 2 0 5 4 8 6 7 1 challenge \$ ./shuffle-string rulepark 4 7 3 1 0 5 2 6 perlraku ```

## Challenge #226.2: Zero Array

You are given an array of non-negative integers, `@ints`.

Write a script to return the minimum number of operations to make every element equal zero.

In each operation, you are required to pick a positive number less than or equal to the smallest element in the array, then subtract that from each positive element in the array.

Example 1: ```Input: @ints = (1, 5, 0, 3, 5) Output: 3 operation 1: pick 1 => (0, 4, 0, 2, 4) operation 2: pick 2 => (0, 2, 0, 0, 2) operation 3: pick 2 => (0, 0, 0, 0, 0) ``` Example 2: ```Input: @ints = (0) Output: 0 ``` Example 3: ```Input: @ints = (2, 1, 4, 0, 3) Output: 4 operation 1: pick 1 => (1, 0, 3, 0, 2) operation 2: pick 1 => (0, 0, 2, 0, 1) operation 3: pick 1 => (0, 0, 1, 0, 0) operation 4: pick 1 => (0, 0, 0, 0, 0) ```
File: zero-array ```#! /usr/bin/env raku unit sub MAIN (*@ints where all(@ints) ~~ UInt, #  :v(:\$verbose)); my \$operations = 0; #  while @ints.sum > 0 #  { \$operations++; #  my \$smallest = @ints.grep( * > 0).min; #  my @new = @ints.map({ \$_ > 0 ?? \$_ - \$smallest !! 0 }); #  say ":Ints @ints[] -> smallest: \$smallest -> @new[]" if \$verbose; @ints = @new; #  } say \$operations; #  ```

 Ensure that all the values (if any, as none is ok) are of the `UInt` (Unsigned Int) type.

 The number of operations will end up here.

 As long as we have non-zero values in the array, using `sum` to add them all up.

 One operation.

 Find the smallest number (with `min`), which is not zero (with `grep`).

 Subtract the smalelst number (from ) from each non-zero value.

 Copy the new array over the old one. (This extra step is used by verbose mode.)

 Print the number of steps.

Running it:

```\$ ./zero-array 1 5 0 3 5 3 \$ ./zero-array 1 1 \$ ./zero-array 2 1 4 0 3 4 ```

Looking good.

With verbose mode:

```\$ ./zero-array -v 1 5 0 3 5 :Ints 1 5 0 3 5 -> smallest: 1 -> 0 4 0 2 4 :Ints 0 4 0 2 4 -> smallest: 2 -> 0 2 0 0 2 :Ints 0 2 0 0 2 -> smallest: 2 -> 0 0 0 0 0 3 \$ ./zero-array -v 1 :Ints 1 -> smallest: 1 -> 0 1 \$ ./zero-array -v 2 1 4 0 3 :Ints 2 1 4 0 3 -> smallest: 1 -> 1 0 3 0 2 :Ints 1 0 3 0 2 -> smallest: 1 -> 0 0 2 0 1 :Ints 0 0 2 0 1 -> smallest: 1 -> 0 0 1 0 0 :Ints 0 0 1 0 0 -> smallest: 1 -> 0 0 0 0 0 4 ```

But...

All that special code to handle zeroes surely can be removed, if we remove the zeroes themselves? Let us find out...

File: zero-array-sans-zero ```#! /usr/bin/env raku unit sub MAIN (*@ints where all(@ints) ~~ UInt, :v(:\$verbose)); my \$operations = 0; @ints = @ints.grep( * > 0 ); #  while @ints.elems > 0 #  { my \$smallest = @ints.min; #  my @new = @ints.map({ \$_ - \$smallest }); # [3a] say ":Ints @ints[] - smallest: \$smallest -> @new[]" if \$verbose; \$operations++; @ints = @new.grep( * > 0 ); # [1a] } say \$operations; ```

 We have to get rid of the zeroes twice, initially - and in the loop. This is not neat, but the simplifications of  and [3a] may be worth it.

 This is a quicker test than the first one (using `sum`).

 Simpler code than before, as the zeroes have been removed before we come here.

Running it gives the expected result:

```\$ ./zero-array-sans-zero 1 5 0 3 5 3 \$ ./zero-array-sans-zero 0 0 \$ ./zero-array-sans-zero 2 1 4 0 3 4 ```

You may have noticed that I have ignored the «pick a positive number less than or equal to the smallest element in the array» part, and gone for «equal to». This seems right at first glance, and actually pass muster. (As the number of operations required is the number of unique non-zero values in the input.)

This insight leads to the realisation that the program is highly overengineered:

File: zero-array-short ```#! /usr/bin/env raku unit sub MAIN (*@ints where all(@ints) ~~ UInt); say @ints.grep( * > 0).unique.elems; #  ```

 get rid of zeroes (if any) and duplicates (if any), and count the result (i.e. unique non-zero values).

Running it gives the correct result:

```\$ ./zero-array-short 1 5 0 3 5 3 \$ ./zero-array-short 0 0 \$ ./zero-array-short 2 1 4 0 3 4 ```

And that's it.