Perl Weekly Challenge 243: Reverse Pairs

These are some answers to the Week 243, 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 November 19, 2023 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: Reverse Pairs

You are given an array of integers.

Write a script to return the number of reverse pairs in the given array.

A reverse pair is a pair (i, j) where: a) 0 <= i < j < nums.length and b) nums[i] > 2 * nums[j].

Example 1

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

(1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1
(3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1

Example 2

Perl Testing in 2023

With my open source work, I've historically taken an approach which relies more on integration testing than unit testing, but with some of my newer projects, I've tried adopting principles from $paidwork and applying them to my free software.

This is a quick run-down of how I'm structuring my test suite in newer projects. It's likely that many of my existing projects will never adopt this structure, but some may.

Using Perl to prepare sequencing files to submit to NCBI's GEO

In the middle of a manuscript submission that requires sequencing data to be uploaded to NCBI's Gene Expression Omnibus.
This is a fairly standardized and (painful!) process that requires one to assemble their sequencing data (a collection of hundreds or thousands of files in the FASTQ format), put them in a single (very large) folder, compress them, generate md5 hashes and then upload them to GEO's FTP site.
There are a couple of tutorials available e.g. here and there that mostly cover the use case of one having assembled the files into a single fastq.
Our project used Oxford's Nanopore platform which store's its data as a series of fastq files, each holding a user defined number of sequences (in our case 2,000). Some of the experiments had generated an excess of 10M reads, so we are talking about a serious number of files to process:


  1. uncompress (if compressed)

  2. concatanate

  3. compress

  4. hash using md5sum

This week in PSC (094) | 2023-01-20

PSC met today, all three of us attended.

We discussed:

  • HAARG’s map my $x RFC. Overall thoughts are good, with one or two minor questions we’ll add as comments.
  • Whether the additions to join() as part of RFC0013 should be gated by some sort of opt-in flag, so as to avoid surprises. Either a feature flag for the caller of join(), or a use overload import option.
  • Maybe renaming the RFC process itself (because of the ambiguity with IETF’s RFC) and improving the numbering system. More discussion needed.

Perl Weekly Challenge 242: Flip Matrix

These are some answers to the Week 242, 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 November 12, 2023, 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 2: Flip Matrix

You are given n x n binary matrix.

Write a script to flip the given matrix as below.

OTOBO supports the German Perl/Raku-Workshop

We are happy to announce that Rother OSS again supports the German Perl/Raku-Workshop as a sponsor.

Since 2011, Rother OSS GmbH, based in southern Germany and with a team throughout Germany, has relied on the combination of an open source ticket system and business services from experts. Specifically: consulting, development and support for the free OTRS versions.

Util::H2O and More, during Ordinary Times

Background

During the 2022 Perl Advent, in particular the entry for December 06; Perl Advent fans were introduced to a little module called Util::H2O.

A lot has already been said about Util::H2O, and this author uses it a lot in client and production code because it helps produce very clean and maintainable HASH reference heavy code. So much so, that he created the Util::H2O::More module to encapsulate some common tasks and additional capabilities for working between pure Perl data structures and blessed objects that have real data accessors, in a natural and idiomatic way.

Support for Generic Perl Data Structures

h2o is perfect for dealing with data structures that are made up of strictly HASH references, but it is often the case that useful data structures contain a mix of HASH and ARRAY references. For example, when using databases or web API calls returning JSON, it is often a list of records that is returned. This was the case of the example call that was in the December 06 Perl Advent 2022 article.

My Family and Other Fish (PerlayStation Part 2)

Faced with any problem, there are many potential approaches. This, I have realised, is amply illustrated by the members of my own family, further reinforcing the educational value of having one. I shall anonymise them for my own protection, and as a disclaimer also state that everything I say about them or anything at all is probably wrong. In fact I have already issued my daily, pre-emptive, unspecified apology to my wife. For me, game coding in Perl is no different.

Identify Problem

Perl Weekly Challenge 242: Missing Members

These are some answers to the Week 242, 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 November 12, 2023, 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: Missing Members

You are given two arrays of integers.

Write a script to find out the missing members in each other arrays.

Example 1

Input: @arr1 = (1, 2, 3)
       @arr2 = (2, 4, 6)
Output: ([1, 3], [4, 6])

(1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6).
(2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3).

Example 2

Regexp Delimiters

Perl lets you use almost anything as a regular expression delimiter. It is usual to use punctuation of some sort, but characters that match /\w/ can be used provided there is white space between the operator and the delimiter: m X foo Xsmx compiles and matches 'foobar'. In the presence of use utf8; you can go wild.

A query on the Perl 5 Porters Mailing List (a.k.a. 'p5p') a few days ago asked for opinions about appropriating the colon (':') as a delimiter for modifiers to the regular expression operators. This got me wondering about what regular expression delimiters were actually in use.

I scratched that itch by plowing through my local Mini CPAN, running everything that looked like Perl through PPI, and checking anything that parsed to an object of one of the relevant classes. A summary of the results is appended.

How to prevent an infinite loop

This loop (assuming you have an /etc/passwd and may read it) runs forever:

while () {
  open my $fh, '<:unix', '/etc/passwd' or die $!;
}

This loop terminates:

while () {
  open my $fh, '<', '/etc/passwd' or die $!;
  binmode $fh, ':unix';
}

Note that you will have to live with some extraneous output:
Too many open files at t.pl line 2.

Btw, you can make it loop forever again this way:

require POSIX;
while () {
  open my $fh, '<', '/etc/passwd' or die $!;
  binmode $fh, ':unix';
  POSIX::close fileno $fh;
}

The Perl Toolchain Summit is back in 2023!

After a three years pandemic-induced hiatus, it is my pleasure to announce that the Perl Toolchain Summit is happening again!

This year, for the thirteenth edition, we will be gathering again in Lyon, from Wednesday April 27 to Sunday April 30 2023, in the hotel Campanile Lyon Centre Part-Dieu. Participants will stay at the hotel, and work in the meeting rooms dedicated for the event.

The first rounds of invitations have been sent. We plan on having about thirty participants. We are always looking for sponsors (ask for our sponsor prospectus!).

Perl Weekly Challenge 238: Running Sum

These are some answers to the Week 238, 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 October 15, 2023 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: Running Sum

You are given an array of integers.

Write a script to return the running sum of the given array. The running sum can be calculated as sum[i] = num[0] + num[1] + …. + num[i].

Example 1

Input: @int = (1, 2, 3, 4, 5)
Output: (1, 3, 6, 10, 15)

Example 2

Input: @int = (1, 1, 1, 1, 1)
Output: (1, 2, 3, 4, 5)

Example 3

This week in PSC (093) | 2023-01-13

Paul, Philippe, and Rik had our regular call today to talk about perl5.

We put together some notes (at long last) for The Perl Foundation to use in their annual prospectus.

We talked about Math-Complex, and moving its issues into Perl5 GitHub. (A nice bit of work for anybody to do, but not groundshaking.)

We got back to the question of making Perl do SSL out of the box. Paul’s question was, “Can we bundle the CPAN dists for doing this and basically CPAN-install them at the end of normal install?” Audacious, but plausible!

We talked about the upcoming release of v5.38. Sure, it’s months away, but the various code freezes are approaching. What stuff needs work? One thing we did especially was review what experiments might be able to be deëxperimentalized. We talked about the refaliasing feature, which remains broken on closures, a total showstopper. Paul wonders if we can reduce the scope of how the feature can be used. For example: what if you must also use declared_refs?

Geizhals Preisvergleich sponsors the German Perl/Raku Workshop

In 2023, the German Perl/Raku Workshop will take place in Frankfurt am Main.
We are very happy to announce that long time Perl supporter Geizhals Preisvergleich sponsor the workshop.

Fosdem mini grants for 2023

One of the goals TPRF would like to achieve, now that conferences are becoming increasingly available in person, is to spread awareness of current Perl and Raku projects.

In support of this goal, TPRF will be issuing a limited number of mini grants of up to $300 to participants interested in holding Perl/Raku based talks in FOSDEM 2023 dev rooms. TPRF has made an intentional decision to not apply for a dev room, but to encourage talks to be given in other, non-language specific dev rooms instead. This will allow Perl and Raku to be shared with new audiences.

In addition, TPRF will have a 2 day stand at FOSDEM and are in need of volunteers to staff it. Stand volunteers are also eligible to apply for a mini grant of up to USD$300 to assist with the cost of attending.

To apply for a FOSDEM mini grant, please send a completed application to fosdem2023-grants@perlfoundation.org.

If you have questions, please join our fosdem slack channel, leave a comment below, or send a message to hello@perlfoundation.org

(Also on TPF news)

Perl Weekly Challenge 236: Array Loops

These are some answers to the Week 236, 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 October 1, 2023, 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 2: Array Loops

You are given an array of unique integers.

Write a script to determine how many loops are in the given array.

To determine a loop: Start at an index and take the number at array[index] and then proceed to that index and continue this until you end up at the starting index.

Example 1

Benchmarking Rakudo releases. Is Raku Still slow?

Benchmarking Rakudo releases. Is Raku Still slow?

Context

Around the first 'public' release of Perl 6 (The x-mas release) I wrote a module that uses the libgumbo from google to parse html5 webpages.

It was faster and more robust than the existing HTML::Parser module written in pure Perl 6. To be fair to the module, the full html5 norm is rather lenghtly to implement.

I was using this to parse my list of favorite fan fiction on fimfiction.net and try to make some stats around them. It was still not super fast, like 0.5+ sec to parse one page (I have like 20+ pages of favorites). So each run of the script was rather slow.

At the time Perl 6 was still in stabilization phase and performance improvements were not really important. But after a while, it started to creep its ways out into Moar and Rakudo. So I wanted to see the improvement over the Rakudo release for my Gumbo module.

Building all Rakudo release

Creating a Simple DSL in Perl

Let's look at the XSPF playlist format. It's a pretty simple XML-based file format.

  <?xml version="1.0" encoding="UTF-8"?>
  <playlist version="1" xmlns="http://xspf.org/ns/0/">
    <title>80's Music</title>
    <trackList>
      <track>
        <title>Take On Me</title>
        <creator>A-ha</creator>
        <location>https://example.com/music/01.mp3</location>
      </track>
      <track>
        <title>Tainted Love</title>
        <creator>Soft Cell</creator>
        <location>https://example.com/music/02.mp3</location>
      </track>
      <track>
        <title>Livin' on a Prayer</title>
        <creator>Bon Jovi</creator>
        <location>https://example.com/music/03.mp3</location>
      </track>
      </track>
    </trackList>
  </playlist>

The full specification has a lot more details, but for now, we'll just use those elements.

If we are building a Perl application that needs to allow less experienced users to write playlists in Perl, it might be useful to define a domain-specific dialect of Perl for writing playlists.

This week in PSC (092) | 2023-01-06

Having been off for two weeks, we spent a while just catching up with the state of the world. Not much of note to report this week.

  • We decided we should write up a list of “what’s expected in 5.38” to ensure we’re on track
  • We briefly chatted about :void, :scalar and :list as possible subroutine attributes for setting the return context

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.