Perl Weekly Challenge 168: Perrin Primes

These are some answers to the Week 168 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 June 12, 2022 at 23:59). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

The Perrin sequence is defined to start with [3, 0, 2]; after that, term N is the sum of terms N-2 and N-3. (So it continues 3, 2, 5, 5, 7, ….)

A Perrin prime is a number in the Perrin sequence which is also a prime number.

Calculate the first 13 Perrin Primes.

f(13) = [2, 3, 5, 7, 17, 29, 277, 367, 853, 14197, 43721, 1442968193, 792606555396977]

On my first implementation, I originally obtained the following sequence:

2 3 2 5 5 7 17 29 277 367 853 14197 43721...

Switch lots of things on at once

Many people already have codes like

use strict;
use warnings;

and so on at the top of each script they write. For scripts which I don't intend to publish anywhere, I have a module (which I accidentally called Z not knowing there was already a module of the same name on CPAN), which switches on lots of things at once just by saying

use Z;

The top bit goes like this:

[Personal Review] Codes from The Weekly Challenge Week 095-105

If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email).

-------------------------------------

My coding momentum is a bit low.

Reflections on the codes I have written:

#095

Task 1: Palindrome Number
TMTOWTDI. On this seemly and actually simple task, I chose to compare the digit one by one.

Task 2: Demo Stack
A bit smell of laziness. I did not provide functions when stack is empty and pop() or min() is called.

#096

Task 1: Reverse Words
A lesson on extra-white space. Oppositely but as lack of caution as a sin, this morning (GMT+8) I found I have forgotten a newline for my code for #105 Task 1.

Task 2: Edit Distance
That was a standard computer science exercise. I was astounded by reading Mr Abigail's blog on the approach on saving memory space.

#097

Task 1: Caesar Cipher
Trivial.

In defence of OOP

During the last years it became fashionable to rag on object oriented programming and a decade ago I would join the choir. Hack, when I started with Perl I despised the bloat and inefficiency of many corporate smelling *coughjava* systems and preached the light weight and foreward thinking way that real hackers travel. In this miniseries I want to write why I changed my tune [part one], the best way (IMO) to use OOP [part two] and why inheritance (incl. roles and templates) and delegation or not helpful features (in contrast to polymorphism) [part three]. Maybe there will be more about rating Perl OO features and modules.

Perl Weekly Challenge 167: Circular Primes

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

Spoiler Alert: This weekly challenge deadline is due in a few of days from now (on June 5, 2022 at 23:59). 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: Circular Primes

Write a script to find out first 10 circular primes having at least 3 digits (base 10).

Please checkout wikipedia for more information.

A circular prime is a prime number with the property that the number generated at each intermediate step when cyclically permuting its (base 10) digits will also be prime.

Output:

113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939, 199933

Given the task specification, I think that the suggested output is wrong. But I’ll come back to that later on.

Circular Primes in Raku

Perl weekly challenge 105

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

You can find my full code on Github

Nth root

You are given positive numbers $N and $k.

Write a script to find out the $Nth root of $k.

The solution

I decided that I would not go the easy way this week and just use the power function ** i.e. return $k**(1/$N).

Instead for integer values of $N, solve this with only using the simple mathematical operators +, /, *, -, <, >

To do this we will use a divide and conquer solution, starting at the two ends of the interval we calculate the values of x^N, and then iterate reducing the interval in half - choosing the interval where the value of x^N is less than k at the left hand end & x^N is greater than k.

To do this we store the value of the ends of the interval as l and r respectively and computer the Nth power of each (ln & rn).

Subject Verb Object notation; declarative Perl without the framework

If you’ve read Curtis “Ovid” Poe’s articles on the declarative framework for Tau Station Link and Link, you are undoubtedly aware of many of the benefits this style of programming can bring. It decouples the “what” from the “how”, encourages discrete functions and prevents the OO trap of “god objects”. The result is software that is easy to test, robust and very flexible. Inserting steps, reording steps etc… are done much easier and more clearly than trying to figure this out in 300 lines of imperative code with four to six level deep if-else chains with for loops mixed in for good fun. However, the framework is tightly coupled to the game in spots and has a few other issues that make it not ready for general use.

"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 166: Hexadecimal Words and K-Directory Diff

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

Task 1: Hexadecimal Words

As an old systems programmer, whenever I needed to come up with a 32-bit number, I would reach for the tired old examples like 0xDeadBeef and 0xC0dedBad. I want more!

Write a program that will read from a dictionary and find 2- to 8-letter words that can be “spelled” in hexadecimal, with the addition of the following letter substitutions:

  • o ⟶ 0 (e.g., 0xf00d = “food”)
  • l ⟶ 1
  • i ⟶ 1
  • s ⟶ 5
  • t ⟶ 7

You can use your own dictionary or you can simply open ../../../data/dictionary.txt (relative to your script’s location in our GitHub repository) to access the dictionary of common words from Week #161.

Optional Extras (for an 0xAddedFee, of course! * Limit the number of “special” letter substitutions in any one result to keep that result at least somewhat comprehensible. (0x51105010 is an actual example from my sample solution you may wish to avoid!)*

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:

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.

Perl Weekly Challenge 165: Scalable Vector Graphics

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

This week, Task 1 and part of Task 2 relate to Scalable Vector Graphics (SVG). I’d been using SVG a very long time ago and certainly didn’t remember any of the details. So, in my first blog relating to PWC 165, I stated that I didn’t have time for that and covered only the part of the challenge not related to SVG. I also said that, in the event that I find some time over the weekend, I might come back and fulfill the SVG part. I thought at the time that this was rather unlikely, but I was finally able to cover the SVG part, at least in Raku.

Task 1: Scalable Vector Graphics (SVG)

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 ];
}

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

Perl Weekly Challenge 165: Line of Best Fit

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

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

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 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.

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.