Perl Weekly Challenge #211

A couple very very last-minute solutions to the Weekly Challenge #211. I was crammed for time, so I didn't get to these until the last minute.

Challenge #1

For challenge number 1 I had an idea of the method I would use, but since I've been experimenting with it anyway, I asked ChatGPT for its ideas as well. Because of my lack of time, I wanted to get some help with the design process. ChatGPT is amazing at both developing and describing an algorithm in simple terms to make it understandable. I based my solution somewhat off the AI's algorithm, but I did write it entirely by hand. It's pretty simple, it just iterates across the matrix and makes sure everything matches its diagonal neighbor prior to it.

Another thing you might notice this week is that I actually put my solutions into functions, not just a basic script. Anyways, here it is:

Perl Weekly Challenge 253: Weakest Row and Schwartzian Transform

These are some answers to the Week 253, Task 2, 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 January 28, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 2: Weakest Row

You are given an m x n binary matrix i.e. only 0 and 1 where 1 always appear before 0.

A row i is weaker than a row j if one of the following is true:

a) The number of 1s in row i is less than the number of 1s in row j.

b) Both rows have the same number of 1 and i < j.

Write a script to return the order of rows from weakest to strongest.

Example 1

Experiments in Overloading

Let's play with overloading a little.

A simple class:

  package Local::Overloaded {
    use Moo;
    
    has number => ( is => 'ro' );
    
    use overload '0+' => sub {
      my $self = shift;
      return $self->number;
    };
  }

And let's test it:

  use Test2::V0;
  my $obj = Local::Overloaded->new( number => 42 );
  is( 0+$obj, 42 );
  done_testing;

This test fails.

Why?

How to change charset=ISO-8859-1 to charset=UTF8? Rocky 9.1 server

Hi ! Everyone, I am back here to ask environmental issue.
When I ran the following CGI.pm testing script before placing at /var/www/cgi-bin, but it is still at /home/mkido/bin. Perl script ran successfully at the Terminal, and replied back the OUTPUT below there.

Perl Script, check.pl
#!/usr/bin/env perl
use strict;
use warnings;
use CGI;

my $q = CGI->new;
print $q->header();
print "OK";

===================
OUTPUT
mkido@localhost$check.pl [Enter to run it.]
Content-Type: text/html; charset=ISO-8859-1

OK
mkido@localhost$


This output result evoked my recognition. Is this Rocky 9.1 Web Server running to provide charset=ISO-8859-1 ?? I want to set all replies from the Server to charset=UTF-8. The httpd.conf is already set to charset=UTF-8. Is there any other setting adjustment somewhere in environmental configuration on my server to change from charset=ISO-8859-1 to charset=UTF-8 ?? Thanks, thanks.

Weather::WeatherKit and Weather::Astro7Timer

Today, the popular Dark Sky weather API is shutting down. I did a little write-up for non-Perl devs on DEV.to, but I thought I'd post here a couple of potentially useful modules I released to CPAN recently. 

Weather::WeatherKit accesses the WeatherKit REST API, which is Apple's official Dark Sky replacement. The module uses Crypt::JWT to create tokens, so accessing WeatherKit then is as simple as:

use Weather::WeatherKit;

my $wk = Weather::WeatherKit->new(
    team_id    => $apple_team_id,
    service_id => $weatherkit_service_id,
    key_id     => $key_id,
    key        => $private_key
);

my %report = $wk->get(
    lat      => 51.2,
    lon      => -1.8,
    dataSets => 'currentWeather'
);

Of course, this API is sort of free, as it requires an Apple developer account. If you don't have one and don't want to get one, there are some alternative APIs, but for the purposes of this post I'll stick to 7Timer, via Weather::Astro7Timer. Even simpler, as it does not need authentication:

Perl Weekly Challenge 253: Split Strings

These are some answers to the Week 253, Task 1, 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 January 28, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 1: Split Strings

You are given an array of strings and a character separator.

Write a script to return all words separated by the given character excluding empty string.

Example 1

Input: @words = ("one.two.three","four.five","six")
       $separator = "."
Output: "one","two","three","four","five","six"

Example 2

Input: @words = ("$perl$$", "$$raku$")
       $separator = "$"
Output: "perl","raku"

Split Strings in Raku

Graph - Weekly Travelling in CPAN

Destination: Graph
Date of Latest Release: Feb 12, 2023
Distribution: Graph
Module version: 0.9726
Main Contributors: Jarkko Hietaniemi (JHI)
Current Maintainer: Neil Bowers (NEILB)
License: [perl_5]

an_undirected_graph.png

Long time ago I claimed in front of a friend I would write a short introduction to graph theory, but I had been not able to figure out where I should start. Neither I would try today. The mathematical objects graphs, or the abstract data structures graphs, are full of interesting behaviors being studied in the discrete math subdiscipline graph theory. The CPAN module Graph is designed to empower Perl programmers doodle with undirected graphs and directed graphs (and also multigraphs and hypergraphs - not going to visit these functionalities here).

Perl Weekly Challenge #210

I'm back this week with PWC #210. Last week I was very busy and spent a long time reviewing other peoples' far more efficient solutions to #208, so I didn't get to doing any solutions for #209. The usual disclaimer about this could contain spoilers, so if you're trying to solve the challenge yourself you may want to skip this post for now. So let's get right into this.

Kill And Win

For this challenge I decided to use some of the tools I learned about in the solutions other people submitted for #208, especially the ways hashes can make the process more efficient. The goal is to find the number in the list where you can delete the most points by deleting the number and its adjacent numbers plus and minus one. You get to count each number however many times it appears in the list.

Introducing Exporter::Almighty

Consider a simple module like this:

Perl Weekly Challenge 252: Unique Sum Zero

These are some answers to the Week 252, Task 2, 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 January 21, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 2: Unique Sum Zero

You are given an integer, $n.

Write a script to find an array containing $n unique integers such that they add up to zero.

Example 1

Input: $n = 5
Output: (-7, -1, 1, 3, 4)

Two other possible solutions could be as below:
(-5, -1, 1, 2, 3) and (-3, -1, 2, -2, 4).

Example 2

This week in PSC (102) | 2023-03-24

Topics discussed:

  • Bug reporting by email: we commit to nothing as an organisation. Some would-be submitters of issues don’t want to use GitHub. They can just send an email to perl5-porters and hope that some helpful soul will copy-paste to GitHub. We encourage the conventional use of GitHub.
  • On the topic email bug reports, it’s likely time to deprecate perlbug as a way to send bug reports. It could be updated to point the user to GitHub, and to print perl -V output (etc.) to copy and paste. We should also have a better set of templates for issues on GitHub.
  • The segfaults on feature-class and refaliasing won’t be fixed before 5.38, and should just be documented as “known bugs”
  • After we discussed renaming RFC to PPC, the general sentiment seemed to be “in favor”, so we’re going to make the rename
  • Rik volunteered to release 5.38, we still need a release manager for 5.37.11
  • Some discussions about preparing the Perl Toolchain Summit plans (which will host the first ever in-person PSC meeting)

Text::Extract::Word, MsOffice::Word::Surgeon - Weekly Travelling in CPAN

Destination: Text::Extract::Word
Date of Latest Release: Mar 09, 2012
Distribution: Text::Extract::Word
Module version: 0.02
Main Contributors: Stuart Watt (SNKWATT)
License: The Artistic License 2.0
Date of Latest Release: Jan 26, 2023
Distribution: MsOffice::Word::Surgeon
Module version: 2.01
Main Contributors: Laurent Dami (DAMI)
License: The Artistic License 2.0

Notice

Perl Weekly Challenge #208

First of all, a greeting. I posted an introduction with a notification of intent to take over a module on CPAN, but the maintainer responded to me. I'm Avery, I'm developing SeekMIDI, a small graphical MIDI sequencer. I started it in 2016 and I took a long break from programming entirely, and I've just restarted developing my programming skills again. For starters, I'm working on Perl Weekly Challenges and bug fixes to modules.

Without further ado, here are my solutions to the PWC #208. All solutions are about to be posted, but this could be a spoiler if you're trying to solve it too. I was very pleased this week that I got it down to about 15-25 minutes for each task, so I'm definitely getting more comfortable in Perl again.

First, task 1:

Perl Weekly Challenge 252: Special Numbers

These are some answers to the Week 252, Task 1, 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 January 21, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 1: Special Numbers

You are given an array of integers, @ints.

Write a script to find the sum of the squares of all special elements of the given array.

An element $int[i] of @ints is called special if i divides n, i.e. n % i == 0, where n is the length of the given array. Also the array is 1-indexed for the task.

Example 1

The first line of Perl_CGI script, env perl vs perl only, how different?

Hi ! Everyone there ! How are you ?

Until recently I runs all of my Perl scripts as well as Perl_CGI scripts by starting the folowing salutation,

#! /usr/bin/perl -w

The script with this beginning runs well at BASH shell at (/home/mkido/bin) LINUX such as Fedora, Ubuntu, Rocky (Alma-derivative). However, almost right now I noticed some of Perl example around has the different first line as below,

#! /usr/bin/env perl

And it doesn't seem to run at HOME BASH shell (/home/mkido/bin) by simple way of executing it by-itself by the command line. Will someone explain me about what is this [env perl] stuff? Thank you so much.

Mitsuru Kido

This week in PSC (101) | 2023-03-17

Porters,

We had an abbreviated PSC call today, largely due to an unexpected delay.

We discussed offering split-up deprecation categories, so you can no warnings 'deprecated::.xyz' and re-affirmed that we want to do this.

We talked about improving the backcompat of strict-vs-version behavior for use vX where X is older than v5.36 and agreed we’d bring that back, but wanted to discuss more about other related changes to the use-vX code.

Template Toolkit’s DEFAULT is not too useful

Quoth the fine manual for Template Toolkit:

The DEFAULT directive is similar to SET but only updates variables that are currently undefined or have no "true" value (in the Perl sense).

Nice. Basically, where SET is like the = operator in Perl, DEFAULT is like the ||= operator. Quite useful! If it were, that is. Because the analogy is only superficially true.

Perl Weekly Challenge 251: Lucky Number

These are some answers to the Week 251, Task 2, 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 January 14, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 2: Lucky Number

You are given a m x n matrix of distinct numbers.

Write a script to return the lucky number, if there is one, or -1 if not.

A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.

Example 1

Quiq - Weekly Travelling in CPAN

Destination: Quiq

Date of Latest Release: Feb 04, 2023
Distribution: Quiq
Module version: 1.207
Main Contributors: Frank Seitz(FSEITZ)
License: The Artistic License
Source: https://github.com/s31tz/Quiq

Quiq is a "Perl class library for rapid development". The modules are "designed according to uniform principles" (translated from German).

Quiq contains 234 (Ooooops!!!) classes at the time this post is being written and their descriptions are mostly written in German.

It contains several text-based document/markup language code writers, namely Quiq::Tag (for XML), Quiq::Html::MODULES, Quiq::MediaWiki::Markup, Quiq::Css, Quiq::LaTex::MODULES; some network functionalities including Quiq::Http::MODULES, Quiq::Ssh and Quiq::Socket ; some tiny tools like Quiq::Color and Quiq::Stopwatch; some system tools like Quiq::Path, Quiq::TempFile and Quiq::Process; some modules for meta-development including Quiq::Hash and Quiq::Object; some modules for general use like Quiq::Converter, Quiq::Math and Quiq::Epoch. ...


For CSS, we have Quiq::Css :

ChatGPT for Perl Learning

How to learn Perl has been an eternal problem for the Perl community. Compared to many other languages that place an emphasis on teaching the basics and using education as a tool for evangelism we've had sporadic efforts along those lines. Given the size of our community and the fact that the best programmers are often in great demand, we have a hard time pumping out the needed docs and examples.

I've been using ChatGPT quite a lot lately and find that although it makes some mistakes it actually churns out pretty decent Perl if given good instructions. For example here's one I did this AM. We just had the dreaded hour shift here in the US and I'm just not up to thinking so I asked Chat GPT:

"write a perl subroutine that accepts two hashrefs and a list. For each item in the item merge the hashrefs by combining the values into an array ref. "

Here's what it pumped out:

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.