# A couple of tasks solved in Raku

Two tasks from the Weekly Challenge 231 solved in the Raku programming language.

On this page, I’ll briefly cover the solutions to the tasks for this week’s Weekly Challenge #231.

You are given an array of distinct integers.

Write a script to find all elements that is neither minimum nor maximum. Return -1 if you can’t.

Example 1

``````Input: @ints = (3, 2, 1, 4)
Output: (3, 2)

The minimum is 1 and maximum is 4 in the given array. So (3, 2) is neither min nor max.
``````

Example 2

``````Input: @ints = (3, 1)
Output: -1
``````

Example 3

``````Input: @ints = (2, 1, 3)
Output: (2)

The minimum is 1 and maximum is 3 in the given array. So 2 is neither min nor max.``````

Here is my original solution in the Raku programming language.

```sub solve(@data) {
@data.grep: * != (@data.min, @data.max).any
}```

As the tasks requires that we print `-1` when there are no elements in the output, let us add an update to satisfy this requirement:

```sub solve(@data) {
(@data.grep: * != (@data.min, @data.max).any) || -1
}```

The `*` in this code will actually replace the `\$_` variable. Would you prefer it, you may use `\$_`, but you’ll need parentheses in this case. So, instead of `@data.grep: * != ...`, you need `@data.grep({\$_ != ...})`, which may be a less clear code for some people.

Finally, let us use some math notation and replace calling the `.any` method with a ‘contains’ operator:

```sub solve(@data) {
(@data.grep: * ∉ (@data.min, @data.max)) || -1
}```

Well, actually, ‘does not contain’. And this is my final solution.

Note that you may want to use the `.minmax` method instead of two calls to `.min` and `.max`, but `.minmax` returns a range, which is not that suitable for this task.

Adding some test cases and passing them to the `solve` function:

```my @tests = (3, 2, 1, 4), (3, 1), (2, 1, 3);
say solve(\$_) for @tests;```

The program prints the expected output:

```\$ raku ch-1.raku
(3 2)
-1
(2)```

You are given a list of passenger details in the form “9999999999A1122”, where 9 denotes the phone number, A the sex, 1 the age and 2 the seat number.

Write a script to return the count of all senior citizens (age >= 60).

#### Example 1

``````Input: @list = ("7868190130M7522","5303914400F9211","9273338290F4010")
Ouput: 2

The age of the passengers in the given list are 75, 92 and 40.
So we have only 2 senior citizens.
``````

#### Example 2

``````Input: @list = ("1313579440F2036","2921522980M5644")
Ouput: 0``````

Apparently, the solution requires extracting information from a string in a specific format. It is not quite clear from the description whether the strings always contains the same number of characters, and thus the age and seat number are always two-digit values. But let’s use this assumption.

As we do not need any other information from the ticket code, no need to properly parse it, so I preferred anchoring around the only letter in the string and consider the next two digits as the age. Of course, you may make it simpler and just extract the two digits counting from the end of the string.

```sub is-sinior(\$ticket) {
~(\$ticket ~~ / <alpha> (\d\d) /)[0] >= 75
}```

Unlike Perl 5, Raku ignores spaces in regexes by default, so I added some air to it. On the other hand, extracting matches may seem a bit more complicated.

For the first given example (see task’s description), the Match object contains the following information:

```｢M75｣
alpha => ｢M｣
0 => ｢75｣```

So, I am taking the 0th element using `[0]` and stringily it with the `~` prefix operator.

In essence, the task has been solved. Let’s add the test cases and run them:

```my @tests = ('7868190130M7522', '5303914400F9211', '9273338290F4010'),
('1313579440F2036', '2921522980M5644');

for @tests -> @tickets {
say +@tickets.grep({is-sinior(\$_)});
}```

The program prints:

```\$ raku ch-2.raku
2
0```

* * *