Course of Raku / Addendum 🆕 / Working with data / Lists, arrays, and hashes / Exercises / Split into even and odd
Solution: Split into even and odd
Here is a possible solution to the task.
Code
my @numbers = 1..10;
my @even = @numbers.grep(* %% 2);
my @odd = @numbers.grep(* % 2);
say "even: @even[]";
say "odd: @odd[]";🦋 You can find the source code in the file even-odd.raku.
Output
even: 2 4 6 8 10
odd: 1 3 5 7 9Comments
* %% 2is true for numbers divisible by two;* % 2is true when the remainder is non-zero, i.e. odd. Eachgrepkeeps one group.Interpolating
@even[]with the empty-bracket zen slice prints the elements separated by spaces inside the double-quoted string.