Perl Weekly Challenge 196: Pattern 132 and Range List

These are some answers to the Week 196 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 December 25, 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: Pattern 132

You are given a list of integers, @list.

Write a script to find out subsequence that respect Pattern 132. Return empty array if none found.

Pattern 132 in a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j].

Example 1

Input:  @list = (3, 1, 4, 2)
Output: (1, 4, 2) respect the Pattern 132.

Example 2

Input: @list = (1, 2, 3, 4)
Output: () since no susbsequence can be found.

Example 3

TWC 147: Prime without Left, and Pent without Quad

In which we bravely overcome ambiguity, and dodge two approaches in the face of (O³).

Do-It-Yourself warnings categories

One of the reasons I have not "moved on" from Perl to some other more "modern" language is that Perl gives me such great access to its inner workings. The Do-It-Yourself Lexical Pragmas post from a couple weeks ago is an example of this. Another example is that Perl lets you tie your own code into its warnings system.

Tying into the warnings machinery requires a module. That is, the interface assumes you are reporting problems relative to another name space that invoked your code. Your module can either add diagnostics to existing Perl warning categories or actually create new categories. In either case your diagnostics are sensitive to the enablement or disablement of the category, as well as its fatalization.

In addition to enabling or disabling warning categories, use warnings ... and no warnings ...; make some subroutines available which can be used to issue your own diagnostics. These are reported relative to the file and line that called into your module (sort of like carp()). A useful subset of the warnings:: subroutines is:

Perl is not dead

Came across an interesting video from one of the users of Perl: Is Perl dead? @Randal L. Schwartz on Dart and Flutter @Code Maven

Perl Weekly Challenge 195: Special Integers and Most Frequent Even

These are some answers to the Week 195 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 December 18, 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: Special Integers

You are given a positive integer, $n > 0.

Write a script to print the count of all special integers between 1 and $n.

An integer is special when all of its digits are unique.

Example 1:

Input: $n = 15
Output: 14 as except 11 all other integers between 1 and 15 are spcial.

Example 2:

Input: $n = 35
Output: 32 as except 11, 22, 33 all others are special.

Special Integers in Raku

TWC 146: 10K Prime and CW Trees

In which we leap tall primes in a single bound, mis-take a tree, test percussion, and find the limits of a Curious Module.

Task 1: 10001st Prime Number - One-liners (expanded) in Raku and Perl.

Task 2: Curious Fraction Tree - Solutions in Raku and Perl (with 200+ tests), and another Perl solution using a CPAN module.

To whom this MySQL UTF-8 news may concern

Strictly speaking not news exactly, given that it dates from early 2018, but it was news to me, and since I haven’t seen it make the rounds I still find it worth disseminating. From the MySQL 8.0.11 release notes:

The utf8mb3 character set will be replaced by utf8mb4 in a future MySQL version. The utf8 character set is currently an alias for utf8mb3, but will at that point become a reference to utf8mb4. To avoid ambiguity about the meaning of utf8, consider specifying utf8mb4 explicitly for character set references instead of utf8.

This a declaration of intent rather than a change that has happened… but still. It’s been a very long September

(To prepare for this you’ll want to note what you need to take into account regarding the way utf8mb4 and indexes interact.)

Eertree: Palindromic Tree

The Weekly Challenge 145

The Task 2 of the Weekly Challenge #145 asked us to build a Palindromic Tree. It also linked to a blog post explaining the "Eertree" data structure.

Maybe it was just me, but I found the blog post confusing. Fortunately, it further linked to a scientific paper that introduced the structure around 2014.

Perl Weekly Challenge 194: Digital Clock and Frequency Equalizer

These are some answers to the Week 194 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 December 11, 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: Digital Clock

You are given time in the format hh:mm with one missing digit.

Write a script to find the highest digit between 0-9 that makes it valid time.

Example 1

Input: $time = '?5:00'
Output: 1

Since 05:00 and 15:00 are valid time and no other digits can fit in the missing place.

Example 2

Input: $time = '?3:00'
Output: 2

Example 3

My Favorite Warnings: closure

In the context of Perl, a closure is a piece of code that captures a specific instance of a lexical variable. A blog entry a month or so ago explores this in greater detail. If you review this blog entry, though, note that it does not cover lexical subroutines, which act more like anonymous subroutines even though they are named.

This blog entry, though, covers the closure warnings category.

This category has been around since warnings were introduced in Perl 5.6. I find four diagnostics in this category as of Perl 5.34.0; two related to lexical variables and two related to lexical subroutines. The "Variable" and "Subroutine" versions of these have similar causes, so I will not distinguish them in my discussion.

All of these diagnostics seem to arise from the fact that named, non-lexical subroutines are created at compile time, whereas lexical and anonymous subroutines are created at run time.

Opening Files Quickly from Inside vim

I use ot a lot when it comes to opening files. I wanted to be able to use this tool from inside vim as well. It turns out, the solution is quite simple.

Full post: https://www.olafalders.com/2022/01/05/open-this-file-from-inside-vim/

Writing a SNES assembler compiler/disassembler - Day 4

Testing

It's time to test what we have written so far. If you look at the asar project, there are already some test files and they come with their own test syntax. It's actually pretty neat since it's embedded in the ASM files comments, so you don't need to write specific tests files.

The format is documented here https://github.com/RPGHacker/asar#test-format but for now, we will just keep the offset and byte value part.

Testing grammar

We could write a loop with like files.IO.slurp.lines and be done to handle this. But let use again a grammar. No action associated with it since it's very basic.

Perl Weekly Challenge 193: Binary String and Odd String

These are some answers to the Week 193 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 December 4, 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: Binary String

You are given an integer, $n > 0.

Write a script to find all possible binary numbers of size $n.

Example 1

Input: $n = 2
Output: 00, 11, 01, 10

Example 2

Input: $n = 3
Output: 000, 001, 010, 100, 111, 110, 101, 011

For this task, all we need to do is to print all the integers between 0 and (2 ** $n) -1, and to display the output in binary format.

Binary String in Raku

Do-It-Yourself Lexical Pragmas

The phrase "Lexical Pragmas" is probably both redundant and ungrammatical (the correct plural of "pragma" being "pragmata", I believe). But the use of "pragma" to mean "Perl module with an all-lower-case name" is fairly common, and I wanted to make clear that this was not what I was talking about. This blog entry is about writing Perl code whose configuration changes are limited to a lexical scope, just like built-in pragmata such as strict or warnings.

There are two pieces to the puzzle: storing the configuration (at compile time) and reading the configuration.

My new modules in 2021

Perl

I had a reasonably productive year, releasing several modules that I think/hope are useful for the wider ecosystem.

Crypt::Passphrase

This module manages the passwords in a cryptographically agile manner. That means that it can not only verify passwords using different ciphers, but it also aids in gradually upgrading passwords hashed with an outdated cipher (or outdated settings) to the current one; for example when you want to upgrade from bcrypt to argon2. Password hashing is both a rather common form of cryptography, and one that is more subject to change than others; you should probably reevaluate your password handling every couple of years. With this module, you can initiate such a transition with a simple configuration change.

This also includes a number of extension distributions (e.g. Crypt::Passphrase::Argon2, Crypt::Passphrase::Bcrypt, etc…), and one new backend module (Crypt::Bcrypt)

Thread::Csp

Draft: Dancer2 Deprecation Policy

Hello fellow Dancers!

I have posted a draft deprecation policy for the Dancer2 code base for public review.

The Dancer Core Team has done our best to look at this every which way and cover all potential issues and use cases, but we're sure to have missed something here or there. So take a look and let us know. Your feedback is welcome - please add comments, feedback, and suggestions on the issue.

Thanks in advance! :-)

Perl Weekly Challenge 192: Binary Flip and Equal Distribution

These are some answers to the Week 192 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, 27, 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: Binary Flip

You are given a positive integer, $n.

Write a script to find the binary flip.

Example 1

Input: $n = 5
Output: 2

First find the binary equivalent of the given integer, 101.
Then flip the binary digits 0 -> 1 and 1 -> 0 and we get 010.
So Binary 010 => Decimal 2.

Example 2

TWC 146: 10K Prime and CW Trees (redirect)

Please go here instead.

Monthly Report - November

Time to relax before ...

What a shame, I delayed the Monthy Report once again. Although I did manage to publish last month on time i.e. 4th Nov.

Do I have reason to justify the delay?

Well, December, is always the busiest month. It is the month when I spend more time with family than ususal. Also I was busy with the Advent Calendar 2021.

As you must have guessed, today I am done with the final gift. Hence, the monthly report is getting time. There was another reason for the delay as I started Live Coding again. I have done few, which I will talk about in the end of year report in a week time, insha-ALLAH.

So, what was the highlight of last month?

My Favorite Warnings: ambiguous

... computer language design is just like a stroll in the park. Jurassic Park, that is. -- Larry Wall

Perl's grammar is inherently ambiguous. That is, it is possible for a syntactically correct chunk of Perl to have more than one valid interpretation. Maybe this is because Larry Wall is a linguist? After all, natural languages are full of ambiguity.

The ambiguous warning is part of the group syntax; that is to say, use warning 'syntax'; enables ambiguous, as well as other warnings in that group. Of course, if appropriate you can just use warning 'ambiguous'; if more precision is justified. Both warnings go back to Perl 5.6, when the warnings pragma itself was introduced.

As has become habitual, I present a random selection of instances of this warning. Normal text is from perldiag. Italicized text is mine.

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.