In this post, I am going to demonstrate my solutions to the Perl Weekly Challenge in the Raku programming language.
List all Christmas days which fall on Sunday in 2019–2100.
(.say if .day-of-week == 7 given Date.new(year => $_, month => 12, day => 25) ) for 2019 .. 2100;
In the loop, this program generates the
Date objects for every possible Christmas for the selected years and then checks the value of the
day-of-week attribute of it. Notice how the
given keyword is utilised so that we can use a dot syntax to call different methods on the same object. See another example in one of the previous posts.
Run the program and it should print the following list:
2022-12-25 2033-12-25 2039-12-25 2044-12-25 2050-12-25 2061-12-25 2067-12-25 2072-12-25 2078-12-25 2089-12-25 2095-12-25
Find all combinations of three numbers which give 12 in total and at least one of which is an even number.
Here, first of all, you can limit yourself with checking the numbers below 12, but actually, there is a bigger simplification if you realise that if you have three numbers at hand, and their sum is 12, then it is not possible for them all to be odd. So we can simplify the task to:
Find all combinations of three numbers which give 12 in total.
A separate note is that there is no restriction for the numbers to be sequential. So, we can generate all possible combinations using the
for (1 .. 12).combinations(3). The rest is simple: add them up with
[+] and print if the sum is 12.
.put if 12 == [+] $_ for (1 .. 12).combinations(3);
Note that if you swap the parts around the equals operator, then you will need parentheses, otherwise
[+] will be applied to the comparison
$_ == 12:
.put if ([+] $_) == 12 for (1 .. 12).combinations(3);
Both programs print the same output:
$ raku ch-2.raku 1 2 9 1 3 8 1 4 7 1 5 6 2 3 7 2 4 6 3 4 5