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)

Reimagining perl5-porters email list for 2021 and beyond

Let's examine if in 2021 an email redistribution list, i.e. perl5-porters@ (p5p) is still the best model for collaborating on the perl language. This is a discussion so comment below!

Advantages of an email list:

  • Familiar interface, people can use their client of choice
  • Low resources to run and maintain
  • Easy to derive automation from as email is all well known protocols
  • Everything is email

Disadvantages of an email list:

  • Email addresses disclosed to all participants (can be changed)
  • UI experience for participants inconsistent, may require client side configuration to "get right"
  • Email "reply" text can lower the signal to noise ratio
  • No topic categorization of posts, its all dumped in to your inbox
  • Tricky to respond to missed emails
  • Moderation is crude, every email is reviewed and approved, or everything is approved
  • Once an email is relayed it can't be moderated further
  • Encourages side channel correspondence
  • Everything is email

Given the long list of disadvantages we can guess why email lists (and newsgroups) have largely fallen by the wayside.

Calculating EV battery charge pricing with Perl

Presently I have great interest in “EVs” Electric Vehicles but I haven’t seen any data on how much it would cost to charge an electric vehicle from 0 % to 100 % battery charge at home in NYC ( So I wrote a Perl script to do just that ) but before we dig in into it I explain a few things about Electric Vehicles.

Electric Vehicles will have a battery capacity that is represented by kilowatt-hour units or kWh for short.

An EV’s driving range is represented in miles units ( In the US ) and the average mileage is determined by the EPA battery range rating ( the bigger the battery capacity usually means the more driving range you will have in a car ) after conducting a few tests ( so in reality your mileage will vary ).

Monthly Report - March

Celebration time ...

The month of March is very special to me. It was in this month 2 years ago, I started my dream project The Weekly Challenge (a.k.a. Perl Weekly Challenge).

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.

Kent Fredric's CPAN distributions are available for adoption

As most of you are probably aware, Kent Fredric sadly passed away earlier this year: notice from his family, on Facebook.

Kent was a prolific contributor to CPAN and Perl. He released more than 150 distributions of his own to CPAN, but also helped countless other authors and distributions, with bug reports, puil requests, and more.

When a CPAN author dies, their indexing permissions are dropped from PAUSE, and where they had the first-come permission, that will be passed to the pseudo-user ADOPTME. This flags the distribution as being available for adoption.

So as of now, all of Kent's distributions are available for adoption.

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.

Perl Weekly Challenge 164: Prime Palindromes and Happy Numbers

These are some answers to the Week 164 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 15, 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: Prime Palindromes

Write a script to find all prime numbers less than 1000, which are also palindromes in base 10. Palindromic numbers are numbers whose digits are the same in reverse. For example, 313 is a palindromic prime, but 337 is not, even though 733 (337 reversed) is also prime.

Prime Palindromes in Raku

We use a data pipeline (chained method invocations) with two grep statements, one to keep palindromes and one to keep prime numbers. This leads to a fairly concise one-line solution:

say (1..^1000).grep({ $_ == .flip }).grep({.is-prime});

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

Perl Weekly Challenge 163: Sum Bitwise Operator and Summations

These are some answers to the Week 163 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 May 8, 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: Sum Bitwise Operator

You are given list positive numbers, @n.

Write script to calculate the sum of bitwise & operator for all unique pairs.

Example 1:

Input: @n = (1, 2, 3)
Output: 3

Since (1 & 2) + (2 & 3) + (1 & 3) => 0 + 2 + 1 =>  3.

Example 2:

"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 162: ISBN-13 and Wheatstone-Playfair

These are some answers to the Week 162 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 May 1st, 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: ISBN-13

Write a script to generate the check digit of given ISBN-13 code. Please refer wikipedia for more information.

Example

ISBN-13 check digit for '978-0-306-40615-7' is 7.

This how Wikipedia describes the calculation of the ISBN-13 check digit:

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

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.