Perl Weekly Challenge 161: Abecedarian Words and Pangrams

These are some answers to the Week 161 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a couple of days from now (on April 24, 2022 at 24:00). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Abecedarian Words

An abecedarian word is a word whose letters are arranged in alphabetical order. For example, “knotty” is an abecedarian word, but “knots” is not. Output or return a list of all abecedarian words in the dictionary, sorted in decreasing order of length.

Optionally, using only abecedarian words, leave a short comment in your code to make your reviewer smile.

Abecedarian Words in Raku

"My half-life with Perl" from OSCON 2013 live encore performance

I've been asked by a couple of Perl groups to give a virtual presentation. Writing new material that would only have been shown once is a lot of work for a small reward.

But, I just happened to be cleaning out my virtual junk drawer, and stumbled across my "half my life with Perl" slide deck that I had presented at OSCON 2013. Most of the stuff is timeless, as it describes Perl's first 25 years, and my second 25 years and how I influenced Perl, and Perl influenced me, and how my company (Stonehenge) was changed by all of this, and in some ways even changed all of this as well.

Please tune in at 6pm Pacific Time (currently UTC-7) on Monday the 22nd to watch it live. I will try to read the comments quickly at the end of the show and answer any questions as well. The video will remain permanently on Youtube at the address below.

Live link: https://www.youtube.com/watch?v=8VMz7GINc2E

Please share!

Perl weekly challenge 104

Here are solutions to this weeks challenges from the Perl Weekly Challenge.

You can find my full code on Github

Task 1: FUSC sequence

Write a script to generate first 50 members of FUSC Sequence. Please refer to OEIS for more information.

The sequence defined as below:

fusc(0) = 0
fusc(1) = 1
for n > 1:
when n is even: fusc(n) = fusc(n / 2),
when n is odd:  fusc(n) = fusc((n-1)/2) + fusc((n+1)/2)

Solution

I will show you 4 versions of the code below - they essentially non-cached/cached versions of recursive (naive) code to get an individual element, and a non-recursive version to compute the whole sequence.

Dancer2 0.301000 Released

On behalf of the Dancer Core Team, version 0.301000 is now available. This is not the release we envisioned; it is missing some things we'd like to have finished, but it does have a couple of new things worth pointing out:

  • A new keyword, request_data, to get the entire deserialized body of the request
  • A new Cookbook recipe for showing how to dynamically enable/disable modules and routes at runtime
  • Numerous doc and bug fixes.

Check out the changelog for a complete list of changes.

The big thing worth pointing out is App::Cmd, which is now not a requirement of Dancer2. A new version of App::Cmd was released with a minimum version requirement of Perl 5.20. We aim to support Dancer2 back to Perl 5.10, which was no longer possible with the current App::Cmd. We had several options to consider in moving forward, and the one we chose was this:

Perl Weekly Challenge 160: Four is Magic and Equilibrium Index

These are some answers to the Week 160 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a couple of days from now (on April 17, 2022 at 24:00). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Four is Magic

You are given a positive number, $n < 10.

Write a script to generate English text sequence starting with the English cardinal representation of the given number, the word ‘is’ and then the English cardinal representation of the count of characters that made up the first word, followed by a comma. Continue until you reach four.

Example 1:

Input: $n = 5
Output: Five is four, four is magic.

Example 2:

Input: $n = 7
Output: Seven is five, five is four, four is magic.

Relatively easy ways to catch memory errors

If you're using XS and C in your Perl module, you might have to worry about memory errors. There are tools like valgrind or memory sanitizer which you can use of course:

valgrind perl -I blib/lib -I blib/arch ./mytestscript

or if your problems are even more serious ones like segmentation fault errors you can run Perl under a debugger like gdb:

gdb perl
% run -I blib/lib -I blib/arch ./mytestscript

However, it might be worth noting some "easy" ways to catch memory errors which actually catch a lot of things.

The first thing is setting the variable to 0 after freeing:

 free (result);
 result = 0;

This prevents you from using the variable again accidentally after freeing, although free (0) is actually not an error, so it doesn't prevent you freeing it twice.

Twenty years ago...

Twenty years ago today I released my first CPAN module: Fork::Queue.

Then there was a group of people supervising module names, and they advised me to move it under the Proc namespace and so it became Proc::Queue.

A couple of weeks before, a bug on one of my scripts had fork-bombed a production box. I barely remember the details now... just that my manager had a serious conversation with me about the incident.

Anyway, that forced me to write the module.

Something I remember for sure is the proud I was of it. How I had been able to replace several Perl builtins playing with the CORE::GLOBAL namespace in a (IMO) clever way!

Perl weekly challenge 103

Here are solutions to this weeks challenges from the Perl Weekly Challenge.

You can find my full code on Github

Task 1: Chinese zodiac

You are given a year $year.

The animal cycle: Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Rooster, Dog, Pig.

The element cycle: Wood, Fire, Earth, Metal, Water.

Additionally there is a two year cycle between Yin & Yang

This challenge is a relatively simple challenge - and one perl is well suited:

sub year_name {
  return join q( ),
    qw( Yang   Yin                             )[  $_[0]    %  2 ],
    qw( Metal  Water   Wood   Fire  Earth      )[ ($_[0]/2) %  5 ],
    qw( Monkey Rooster Dog    Pig   Rat   Ox
        Tiger  Rabbit  Dragon Snake Horse Goat )[  $_[0]    % 12 ];
}

Perl Weekly Challenge 159: Farey Sequence and Möbius Number

These are some answers to the Week 159 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few days from now (on April 10, 2022 at 24:00). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Farey Sequence

Write a script to compute Farey Sequence of the order $n.

Example 1:

Input: $n = 5
Output: 0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1.

Example 2:

Input: $n = 7
Output: 0/1, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 2/5, 3/7, 1/2, 4/7, 3/5, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 1/1.

Example 3:

Input: $n = 4
Output: 0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1.

I failed to pause before blogging

I got this email from PAUSE just now:

Failed: PAUSE indexer report BKB/Go-Tokenize-0.01.tar.gz

     module : switch

It looks like it doesn't like this line of code containing Go keywords:

chan         else         goto         package      switch

A blog post about blog posts

Folk's in the world of Perl have been making amazing efforts to blog more and even to produce video content. That's awesome! Keep it up!

Risking sounding like a mandatory training video from HR, I want to remind budding authors of some high level criteria you should review before completing a post:

  • Does it welcome new people to Perl?
  • Does it welcome people back to Perl?
  • Does it lift, encourage and praise them?
  • Is it respectful to people who's use case, experiences and journey with Perl is different to yours?
  • Is it respectful of peoples lifestyle, politics, background, and differing lived experiences?
  • Does it grow the Perl community and celebrate our diversity?
  • Does it teach people something new about Perl?

The answer in all cases should be yes.

Here are some examples that aren't inspired by any specific post but are based on content or comments I have read.


Using system perl is stupid, it's not real perl

Monthly Report - February

Back to school ...

Apology for the delay in monthly report, usually I get it released on first day of the month.

Last month was the shortest month of the year as you all know, so getting things done became relatively harder.

What is in store this time?

Well, there were two things that took away all the attentions.

500th edition of Perl Weekly newsletter

I have been a regular reader of the weekly newsletter for many years now. If you want regular dose of Perl news directly in your inbox every Monday then please do subscribe the newsletter. I joined the elite group of editors in May 2018, thanks to Gabor Szabo for giving me the opportunity. I had the honour to edit the 500th edition of the Perl Weekly newsletter. It feels nice and gives me sense of achievments.

Perl Weekly Challenge 158: Additive Primes and Cuban Primes

These are some answers to the Week 158 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a couple of days from now (on April 3, 2022 at 24:00). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Additive Primes

Write a script to find out all Additive Primes <= 100.

Additive primes are prime numbers for which the sum of their decimal digits are also primes.

Output

2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89

Additive Primes in Raku

Using the is-prime (used twice), comb, and sum methods, this task can be solved in Raku with a simple one-liner:

say join ", ",  grep { .is-prime and .comb.sum.is-prime }, 1..100;

This script displays the following output:

$ raku ./add-prime.raku
2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89

Perl is dead ... when I'm dead!

Too fanboy/girl-ish, perhaps?

Yes, I'm well on-trend, by a couple of months. As you see, lockdown has made a hot mess of my blogging schedule. I count myself very fortunate that is the worst effect it's had on me, alongside the gaining of some mass.

WfH WARNING! Watch out for those caramel waffles! A single Stroopwaffle has enough calories to feed a hungry village for a day and are not a sustainable treatment for anxiety. Two kWh per packet, not a word of a lie.

Perl weekly challenge 102

Here are solutions to this weeks challenges from the Perl Weekly Challenge.

You can find my full code on Github

Task 1: Rare Numbers

You are given a positive integer $N. Write a script to generate all Rare numbers of size $N if exists. Please checkout the page for more information about it.

Examples:

  • 2 digits: 65
  • 6 digits: 621,770
  • 9 digits: 281,089,082

The solution

There is a very naive solution to this problem.

Gzip::Zopfli - another compression module

Following on from the Gzip::Libdeflate I mentioned before, I also made this: Gzip::Zopfli

It is based on the Zopfli gzip compression library from Google Research.

Both Zopfli and libdeflate seem to excel at compressing JSON files compared to the ordinary zlib compression, sometimes with an improvement of up to 30%. Zopfli usually wins by a small margin over libdeflate. Here are some results of this script on random JSON files:

Perl Weekly Challenge 157: Pythagorean Means and Brazilian Number

These are some answers to the Week 157 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a couple of days from now (on March 27, 2022 at 24:00). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Pythagorean Numbers

You are given a set of integers.

Write a script to compute all three Pythagorean Means i.e Arithmetic Mean, Geometric Mean and Harmonic Mean of the given set of integers. Please refer to wikipedia page for more informations.

Example 1:

Input: @n = (1,3,5,6,9)
Output: AM = 4.8, GM = 3.9, HM = 2.8

Example 2:

Input: @n = (2,4,6,8,10)
Output: AM = 6.0, GM = 5.2, HM = 4.4

Example 3:

rt.cpan.org to remain online

Despite rt.cpan.org still displaying the sunset message, it is in fact not going away forever on the 1st of March, but will have an 'extended downtime' while it is moved elsewhere. In future it'd be nice if communications of such things, and even allowing others to have a say on the matter, could be handled better.

See also:

Perl weekly challenge 101

Here are solutions to this weeks challenges from the Perl Weekly Challenge.

You can find my full code on Github

Task 1: Pack a Spiral

You are given an array @A of items (integers say, but they can be anything).

Your task is to pack that array into an MxN matrix spirally counterclockwise, as tightly as possible.

‘Tightly’ means the absolute value |M-N| of the difference has to be as small as possible.

New compression module Gzip::Libdeflate

I've turned the libdeflate compression library into a CPAN module:

Gzip::Libdeflate

This is the gzip compression method, but updated.

It's supposed to be much faster and better than libz (the original gzip library).

Sometimes I am getting compression of as much as 30% better on some files.

So far I haven't tested whether or not it is faster.

See the module above for links to the original library and so on.

Since this is an early release, it's very likely indeed that bugs in the module are my fault rather than any problem with libdeflate, so please report them to me.

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl with a graphic design donated by Six Apart, Ltd.