RogerBW's Blog

Perl Weekly Challenge 30 15 October 2019

I've been doing the Perl Weekly Challenges. This one dealt with date calculations and exhaustive number searches.

Write a script to list dates for Sunday Christmas between 2019 and 2100. For example, 25 Dec 2022 is Sunday.

I was going to do this straight, but then it seemed like fun to make it a one-liner. Well, almost.

use Time::Local;

print map {"$_\n"} grep {(gmtime(timegm(0,0,12,25,11,$_)))[6]==0} (2019..2100);

Note that timegm matches gmtime's 0-based month numbering. (And it's a habit of mine to use timegm, and noon, to minimise the possibility of time-zone-related confusion, though in this case it shouldn't make any difference.)

In Perl6 this is just as straightforward:

map {say "$_"}, grep {Date.new($_,12,25).day-of-week==7}, (2019..2100);

I do like Perl6's built-in Date type, but I'm sure that having to load it (and all the rest) for every program is the reason startup is so much slower.

Write a script to print all possible series of 3 numbers, where in each series at least one of the number is even and sum of the three numbers is always 12. For example, 3,4,5.

Since this is clearly intended to be a finite list I choose not to consider negative numbers and non-integers. An optimisation: an even sum of three integers must by definition contain at least one even integer, so there's no need to check for it. I optimise further by generating the numbers in ascending order, so that each combination will only be displayed once.

my $target=12;
foreach my $a (0..$target) {
  foreach my $b ($a..$target) {
    my $c=$target-$a-$b;
    if ($c>=$b) {
      print "$a $b $c\n";
    }
  }
}

and converting this to Perl6 is just a matter of changing the loop syntax.

Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime cthulhu eternal cycling dead of winter doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2022 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow opera parody paul temple perl perl weekly challenge photography podcast politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1