This is my response to The Weekly Challenge #326.
YYYY-MM-DD
.
Input: $date = '2025-02-02'
Output: 33
The 2nd Feb, 2025 is the 33rd day of the year.
Example 2:
Input: $date = '2025-04-10'
Output: 100
Example 3:
Input: $date = '2025-09-07'
Output: 250
#! /usr/bin/env raku
unit sub MAIN ($date); # where Date.new($date) ~~ Date); # [1]
say Date.new($date).day-of-year; # [2]
[1] It may be tempting to validate the input, as done here in the
comment. But it does not really help, as the call in [2] will catch any errors anyway.
And we would end up calling Date.new
twice on legal dates.
See
docs.raku.org/type/Date
for information about the Date
class.
[2] Generate the Date
object, and use the day-of-year
method to get the day
of year.
Running it:
$ ./day-of-year 2025-02-02
33
$ ./day-of-year 2025-04-10
100
$ ./day-of-year 2025-09-07
250
Looking good.
Verbose mode did not make any sense in this program.
Input: @ints = (1, 3, 2, 4)
Output: (3, 4, 4)
Pair 1: (1, 3) => 3 one time => (3)
Pair 2: (2, 4) => 4 two times => (4, 4)
Example 2:
Input: @ints = (1, 1, 2, 2)
Output: (1, 2, 2)
Pair 1: (1, 1) => 1 one time => (1)
Pair 2: (2, 2) => 2 two times => (2, 2)
Example 3:
Input: @ints = (3, 1, 3, 2)
Output: (1, 1, 1, 2, 2, 2)
Pair 1: (3, 1) => 1 three times => (1, 1, 1)
Pair 2: (3, 2) => 2 three times => (2, 2, 2)
! /usr/bin/env raku
subset PosInt of Int where * >= 0; # [1]
unit sub MAIN (*@ints where @ints.elems %% 2 && all(@ints) ~~ PosInt, # [2]
:v(:$verbose));
my @output; # [3]
for @ints -> $i, $j # [4]
{
my @add = $j.Int xx $i; # [5]
say ": i:$i, j:$j -> @add[]" if $verbose;
@output.append: @add; # [6]
}
say "({ @output.join(", ") })"; # [7]
[1] We define a custom type for positive integers with
subset
.
See docs.raku.org/routine/subset for more information about subset
.
[2] A slurpy array of positive integers, courtesy of [1]. The number of elements must be even (divisible by two), and zero elements is ok.
[3] The result will end up here.
[4] Iterate over the input, two values at a time.
[5] Use the list repetition operator xx
to get the required values.
See docs.raku.org/routine/xx for more information about xx
.
[6]
Add the new list to the result, as new elements (courtsy of
append
), and not as a single sublist (which push
would have done).
See docs.raku.org/routine/append for more information about append
.
See docs.raku.org/routine/push for more information about push
.
[7] Pretty print the result.
Running it:
$ ./decompressed-list 1 3 2 4
(3, 4, 4)
$ ./decompressed-list 1 1 2 2
(1, 2, 2)
$ ./decompressed-list 3 1 3 2
(1, 1, 1, 2, 2, 2)
Looking good.
With verbose mode:
$ ./decompressed-list -v 1 3 2 4
: i:1, j:3 -> 3
: i:2, j:4 -> 4 4
(3, 4, 4)
$ ./decompressed-list -v 1 1 2 2
: i:1, j:1 -> 1
: i:2, j:2 -> 2 2
(1, 2, 2)
$ ./decompressed-list -v 3 1 3 2
: i:3, j:1 -> 1 1 1
: i:3, j:2 -> 2 2 2
(1, 1, 1, 2, 2, 2)
And that's it.