Perl Weekly Challenge 160: Four is Magic and Equilibrium Index

These are some answers to the Week 160 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 April 17, 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: Four is Magic

You are given a positive number, $n < 10.

Write a script to generate English text sequence starting with the English cardinal representation of the given number, the word ‘is’ and then the English cardinal representation of the count of characters that made up the first word, followed by a comma. Continue until you reach four.

Example 1:

Input: $n = 5
Output: Five is four, four is magic.

Example 2:

Input: $n = 7
Output: Seven is five, five is four, four is magic.

Perl weekly challenge 100

We are finally here - we have hit week 100 of Manwar's Perl Weekly Challenges, and here are solutions to this weeks challenges from the Perl Weekly Challenge.

You can find my full code on Github

Task 1: Fun time

You are given a time (12 hour / 24 hour). Write a script to convert the given time from 12 hour format to 24 hour format and vice versa. Ideally we expect a one-liner.
Example 1: Input: 05:15 pm or 05:15pm -> Output: 17:15
Example 2: Input: 19:15               -> Output: 07:15 pm or 07:15pm

The solution

Firstly I have to thank Manwar for asking for a one line solution as this is my modus operandi .... So we will look at this and see how we can get a simple yet compact solution... well here goes.... {code is 110 bytes, within the functions curly braces there are just 102 bytes of code}

berrybrew version 1.33 released!

I've released berrybrew version 1.33. This version has significant enhancements, along with some bug fixes and handling of some uncaught exceptions. The changes reflect versions 1.30 to 1.33.

Major changes include:

UI:

  • Allows you to install, switch to, remove and use Strawberry Perls directly
  • Can now spawn a CLI window for any Perl you have installed
  • Allows you to spawn a CLI window for the currently active Perl
  • Provides access to modify several of the core configuration options (debug, file association etc)
  • Allows you to disable all berrybrew Perls and restore to system default

Installer:

  • Performs an upgrade on any previous berrybrew install
  • Adds any new configuration options, while preserving the values of any previously set existing ones
  • Provides facility to install the most recent version of Strawberry Perl
  • Allows you to have berrybrew manage the .pl file association
  • Allows you to have the UI run at system startup
  • Aborts if trying to install the same version that's already installed

Functionality:

  • You can now leave off the 32/64 bit prefix on a Perl name, and we'll default to _64
  • All execution paths return a proper exit code
  • Added new berrybrew hidden command, lists all, well, hidden commands

For all other changes, please refer to the Changes file.

Cheers!

-stevieb

Perl 7: A Modest Proposal

I've written a new blog post on Perl 7 (prev: Perl 7: A Risk-Benefit Analysis and Perl 7 By Default). You can find it, and likely my future posts, on dev.to#perl, for similar reasons as mentioned here.

Perl 7: A Modest Proposal

Perl Weekly Challenge 159: Farey Sequence and Möbius Number

These are some answers to the Week 159 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 April 10, 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: Farey Sequence

Write a script to compute Farey Sequence of the order $n.

Example 1:

Input: $n = 5
Output: 0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1.

Example 2:

Input: $n = 7
Output: 0/1, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 2/5, 3/7, 1/2, 4/7, 3/5, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 1/1.

Example 3:

Input: $n = 4
Output: 0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1.

You can now use the spvmcc command to generate an executable file from the SPVM source code.

You can now use the spvmcc command to generate an executable file from the SPVM source code.

SPVM is a module that can convert Perl-like source code into C language and execute it.

I have succeeded in generating an executable in a very stable way.

SPVM source code

Who you gonna call? Perl client and website for Google Civic Information API

I recently became aware of a very cool service provided by the Google. The Civic Information API provides contact information for all elected representatives (from head of state down to municipal official) for any US address.

I wrote the Perl client for the API, published as Net::Google::CivicInformation. Get a free API token and you're up and running.

This was a satisfying project because I can imagine people finding actual value in the product. I'm all for being vocal with our government! I decided to take it a step further and created a webservice interfacing to the API. It's written in Dancer2 and the source code is on Github (at the urging of ++GabSzab who pointed out that there are few examples of Dancer2 apps for developers to study.)

The site is online now at https://contactmyreps.com. Please give it a try. Feedback welcome!

cmr.png

Perl weekly challenge 98

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

You can find my full code on Github

Challenge 1

You are given file $FILE.

Create subroutine readN($FILE, $number) returns the first n-characters and moves the pointer to the (n+1)th character.

Solution

Rather than turning this into an object which was the first idea - I decided to keep the code clean by making it a function call, and to also make it work with multiple file handles open simultaneously.

To achieve this without an object - we will need to use a global hash to contain the opened file handles - so that when we re-call the function we don't re-open the file.

Perl Weekly Challenge 158: Additive Primes and Cuban Primes

These are some answers to the Week 158 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 April 3, 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.

Additive Primes

Write a script to find out all Additive Primes <= 100.

Additive primes are prime numbers for which the sum of their decimal digits are also primes.

Output

2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89

Additive Primes in Raku

Using the is-prime (used twice), comb, and sum methods, this task can be solved in Raku with a simple one-liner:

say join ", ",  grep { .is-prime and .comb.sum.is-prime }, 1..100;

This script displays the following output:

$ raku ./add-prime.raku
2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89

Monthly Report - January

Let's look at the brighter side ...

Did you have chance to read my annual report?

I have been doing monthly report for many years now.

Why? What is the point?

Well, let me answer the first question, Why? To be honest with you, I do it to keep myself motivated. I need some kind of (self) motivation to carry on what I do on a daily basis. Now to answer the second question, What is the point? It helps me to keep track and follow the progress.

I found the truth about GameStop, Qanon, the Biden adminstration, and the British Royal Family

The British Royal Family and GameStop have been in a secret war with each other for hundreds of years. Nobody knows why. GameStop has included it in their manifesto and the Royals is always untrustworthy. They leave clues to mock us! GAMESTOP HAS INCLUDED IT IN THEIR MANIFESTO! MOCK! MOCK!

There is more:

Q found out that reincarnation is true and he has been imprisoned in The Pentagon. Qanon are protecting this secret. The Biden administration knows the truth but Qanon have paid them off with a warehouse full of holy water. The Wikipedia entry for The Pentagon keeps getting edited by the Biden administration and Qanon even admit it. They leave clues to mock us.

You too can find out the truth about what's REALLY going on here or here

Perl weekly challenge 97

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

You can find my full code on Github

Challenge 1

You are given string $S containing alphabets A .. Z only and a number $N . Write a script to encrypt the given string $S using Caesar Cipher with left shift of size $N .

Solution


sub caesar {
  return $_[0] =~ s{([A-Z])}{chr 65+(-65-$_[1]+ord$1)%26}regex;
}

This is a simple one liner - but has some neat features - other than using "regex" for the switches, although most are important...

  • r - return value rather than substitute in original string
  • e - evaluate replace rather than use string
  • g - repeat over all characters
  • x - not needed (comments in match) - but looks good!

In the evaled replacement code - there is some clever ordering of values to reduce the need for brackets...

Perl Weekly Challenge 157: Pythagorean Means and Brazilian Number

These are some answers to the Week 157 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 March 27, 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: Pythagorean Numbers

You are given a set of integers.

Write a script to compute all three Pythagorean Means i.e Arithmetic Mean, Geometric Mean and Harmonic Mean of the given set of integers. Please refer to wikipedia page for more informations.

Example 1:

Input: @n = (1,3,5,6,9)
Output: AM = 4.8, GM = 3.9, HM = 2.8

Example 2:

Input: @n = (2,4,6,8,10)
Output: AM = 6.0, GM = 5.2, HM = 4.4

Example 3:

Dancer2 0.300005 Released

Well, it’s been a hot minute since the last release, hasn’t it? Dancer2 0.300005 has landed on CPAN and features a number of bug fixes, enhancements, and doc patches:

Mood Lighting

The lighting in my bedroom uses Philips Hue bulbs — specifically, the coloured ones. Last night, I decided it would be nice to set the three lights in my bedroom to cycle slowly through a set of warm colours using a script.

I didn't want harsh transitions from one colour to the next, but for the lighting to fade from one colour to the next in a smooth gradient. Also, I didn't want the three bulbs to all be the exact same colour, but wanted each bulb to be at different stage in the cycle, like they're "chasing" each other through the colours.

So I whipped up a quick script. It requires the command-line tool hueadm to be installed and set up before we start. You can run hueadm lights to get a list of available lights, and in particular, their ID numbers.

Perl weekly challenge 96

This week we had contrasting challenges.

Challenge 1 - Reverse Words

Take a string of words {with arbitrary white space around the words} and reverse the order of the words in the string and removing any redundant white space.

This is a classic example of a 1-liner....


   join q( ), reverse grep {$_} split m{\s+}, $_[0];

Challenge 2 - Edit Distance

I will provide 2 solutions here... one a less optimal solution which at the same time gives us a nice way of rendering the alignment - and then an more efficient "boiling down" of the first algorithm to just return the distance...

I'm just going to add "Another day job challenge!"

To be able to make "nicer" output - rather than just keeping track of the edit distance of substrings - we will actually keep the alignment of the two words as a string of "operations" whether they be Indels or SNPs.

Perl Weekly Challenge 156: Pernicious and Weird Numbers

These are some answers to the Week 156 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 March 20, 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: Pernicious Numbers

Write a script to permute first 10 Pernicious Numbers.

A pernicious number is a positive integer which has prime number of ones in its binary representation.

The first pernicious number is 3 since binary representation of 3 = (11) and 1 + 1 = 2, which is a prime.

Expected Output:

3, 5, 6, 7, 9, 10, 11, 12, 13, 14

I’m not sure why the task description speaks about permuting the first 10 pernicious numbers. It seems we’re simply requested to display them.

Pernicious Numbers in Raku

Regarding the closure of rt.cpan

Let me preface this short post with this, I don't have the solution to this problem. Perhaps there is someone in the wider Perl space who is well placed to pick this up, but there seems to be little going on in terms of community engagement.

In the first week of 2021 I noticed a link to this sunset message for rt.cpan behind displayed on the rt.cpan homepage. Firstly I believe the notification on the page could be highlighted better, grey on grey, on a page with lots of grey isn't exactly eye catching.

At the time the linked article didn't contain much information, besides a date. It has since been updated with links to resources to migrate tickets elsewhere.

Perl weekly challenge 096 - Raku

This is a Raku answer for the 096 Perl Weekly Challenge

.

Exercise 1

The first task consists in writing a script to reverse the order of words without leading/trailing spaces. These are the examples:

Example 1:
Input: $S = "The Weekly Challenge"
Output: "Challenge Weekly The"

Example 2:
Input: $S = " Perl and Raku are part of the same family "
Output: "family same the of part are Raku and Perl"

This is easy to implement in Raku due to the many routines and methods included by default:

sub challenge( $string ) {
  return $string.words.reverse;
}

Exercise 2

The second exercise is the implementation of the Wagner–Fischer algorithm to compute the edit distance between two strings of characters. Raku shows the muscle of its support for OOP, allowing to almost literally write down the algorithm as a class. This is the pseudocode algorithm on Wikipedia:

Perl weekly challenge 99

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

You can find my full code on Github

Challenge 1

You are given a string $S and a pattern $P.

Write a script to check if given pattern validate the entire string. Print 1 if pass otherwise 0.

The patterns can also have the following characters:

  • ? - Match any single character.
  • * - Match any sequence of characters.

Example 1:

Input:  $S = "abcde" $P = "a*e"
Output: 1

Solution

This challenge is relatively simple - converting "file name" wildcards into perl regular expressions. the "*" wildcard is the same as ".*" in perl, "?" is the same as "." in perl.

So we replace them in the regex (and remembering we are tied to the ends of the string).


  my $regex  = '\A' . ( $pattern =~ s{[*]}{.*}r =~ s{[.]}{?}r ).'\Z';

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.