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}

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

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

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

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.

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

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

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 155: Fortunate Numbers and Pisano Periods

These are some answers to the Week 155 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 13, 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: Fortunate Numbers

Write a script to produce first 8 Fortunate Numbers (unique and sorted).

According to Wikipedia:

A Fortunate number, named after Reo Fortune, is the smallest integer m > 1 such that, for a given positive integer n, pn# + m is a prime number, where the primorial pn# is the product of the first n prime numbers.

Expected Output:

3, 5, 7, 13, 17, 19, 23, 37

Fortunate Numbers in Raku

We first create an infinite (lazy) list (@primes) of prime numbers. Then, we use it to create a list of primordials (@primorials). And then, we use it to find fortunate 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.

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 154: Missing Permutations and Padovan Primes

These are some answers to the Week 154 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 6, 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: Missing Permutations

You are given possible permutations of the string ‘PERL’.

PELR, PREL, PERL, PRLE, PLER, PLRE, EPRL, EPLR, ERPL,
ERLP, ELPR, ELRP, RPEL, RPLE, REPL, RELP, RLPE, RLEP,
LPER, LPRE, LEPR, LRPE, LREP

Write a script to find any permutations missing from the list.

We’ll assume that the permutations provided are correct, but that at least one is missing. In fact, if one of the permutation is wrong, this would not alter the result. For this task, we’ll generate all permutations and compare the result with the given list of permutations.

Missing Permutations in Raku

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';

Roles, h'uh, what are they good for?

What is a role? Put simply, roles are a form of code reuse. Often, the term shared behavior is used. Roles are said to be consumed and the methods ( including attribute accessors ) are flattened into the consuming class.

One of the major benefits of roles is they attempt to solve the diamond problem encountered in multi-inheritance by requiring developers to resolve name collisions manually that arise in multi-inheritance. Don't be fooled however, roles are a form of multi-inheritance.

I often see roles being used in ways they shouldn’t be. Let’s look at the mis-use of roles, then see an example of shared behavior.

I’m using that word inheritance a lot for a reason, one of the two ways I see roles most often misused is to hide an inheritance nightmare.

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.