Perl Weekly Challenge 019: Five Weekends and Paragraph Wrapping

This week’s challenge was a bit easier than the recent ones, but I was glad for that. The Perl Conference in Riga is coming and I still don’t have my slides ready!

Five Weekends

Write a script to display months from the year 1900 to 2019 where you find 5 weekends, i.e. 5 Fridays, 5 Saturdays, and 5 Sundays.

I started by running the cal utility (part of the util-linux package) to see how such months might look. For example, this is the output of cal 1904 (5 weekends highlighted manually be me):

Monthly Report - July

Chopping substrings

The first task of the 18th Weekly Challenge was to find the longest common substring(s) in a set of strings. That is, given a set of strings like “ABABC”, “BABCA” and “ABCBA”, print out “ABC”.

The “best practice” solution is a technique known as suffix trees, which requires some moderately complex coding. However, we can get very reasonable performance for strings up to hundreds of thousands of characters long using a much simpler approach.

Let’s start with a very concise, but suboptimal, solution. If we has a list of all the possible substrings for each string:

ABABC: ABABC, ABAB, BABC, ABA, BAB, ABC, AB, BA, BC, A, B, C
BABCA: BABCA, BABC, ABCA, BAB, ABC, BCA, BA, AB, BC, CA, B, A, C
ABCBA: ABCBA, ABCB, BCBA, ABC, BCB, CBA, AB, BC, CB, BA, A, B, C

...then we could treat each list as a set, take the intersection of those sets:

ABC, AB, BA, BC, A, B, C

...then simply find the longest element: ABC

Perl Weekly Challenge # 19: Weekends and Wrapping Lines

These are some answers to the Week 19 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in several days from now (August 4, 2019). This blog post offers some solutions to this challenge, please don't read on if you intend to complete the challenge on your own.

Challenge # 1: Five Weekends in a Month

Write a script to display months from the year 1900 to 2019 where you find 5 weekends i.e. 5 Friday, 5 Saturday and 5 Sunday.

This time, I'll start with Perl 6, because the built-in Date type seems to make it easier.

Five Weekends in Perl 6

Perl Weekly Challenge 018/2: Priority Queue

Write a script to implement Priority Queue. It is like regular queue except each element has a priority associated with it. In a priority queue, an element with high priority is served before an element with low priority. Please check this wiki page for more information. It should serve the following operations:
  1. is_empty: check whether the queue has no elements.
  2. insert_with_priority: add an element to the queue with an associated priority.
  3. pull_highest_priority_element: remove the element from the queue that has the highest priority, and return it. If two elements have the same priority, then return element added first.

The Naive Implementation

If the priorities are non-negative integers and bounded by a reasonable maximum, the following implementation might be all you need. Let’s implement the queue as an array of arrays, each array element at position $p represents all the queue elements with priority $p.

next Math::Matrix releases

As you can see in the Changefile, there is a lot stuff coming in the next release of Math::Matrix, the de facto standard for now for Perl 6 Matrix Math, as mentioned in the docs. However, I have further plans I want to announce here and also ask my readers if I should do the proposed change of name space or not. This should be maybe a lightning talk in at TPC in Riga, but I already have a regular and a lightning there, so I choose this format.

London Perl Workshop - 2019

With great pleasure, we would like announce the London Perl Workshop 2019. After 12 years, we're moving on from Westminster University to David Game College, 31 Jewry St, London EC3N 2ET. Thanks to Westminster University for hosting us for so long. We are holding the workshop a little earlier than normal because we wanted to avoid clashing with various other things and not be too close to Xmas We would like to invite you to join us for one day technical conference on Saturday 19th October 2019.

You could register if you don't already have an account otherwise login and register.

We are also accepting talk proposal now. If you would like to be a speaker at London Perl Workshop 2019 then please submit your proposal as soon as possible.

You are free to pick your favourite technology to talk about whether it is Perl 5, Perl 6 or any other languages/technologies.

Last but not the least, we are looking for sponsors. For more information, please visit the page.

We hope to see you there,

Tom, Lee, Katherine, Julien and Mohammad (the organising team)

It's time to consider avoiding IP fragmentation in the DNS

An article on APNIC was posted earlier this month with the above title. It demonstrates the impacts of IP fragmentation in DNS by demonstrating two successful attacked using it. It is notable to us on this blog because all the examples are in Perl, in addition to everyone hopefully being concerned with running or using reliable and secure DNS.

Check it out at: https://blog.apnic.net/2019/07/12/its-time-to-consider-avoiding-ip-fragmentation-in-the-dns/

Perl Weekly Challenge 018/1: Longest Common Substring

Write a script that takes 2 or more strings as command line parameters and prints the longest common substring.

A naive solution

For a naive solution, we first need to make an observation: although the longest common substring (lcs) must be a substring of all the strings, we don’t have to process all pairs of strings to find it. We can just take all the substrings of one of the strings (using the shortest one would be fastest) and try to find each substring in all other strings. If the substring is present in all the strings and is longer than the lcs found so far, we have a new lcs candidate. I decided to keep all the lcs’s of the same length.

Perl Weekly Challenge # 18: Priority Queues and Binary Heaps In Perl 6

In this previous blog post, I provided some answers to the Week 18 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

However, I omitted the Perl 6 solution to the priority queues part of the challenge, because I wanted to use a binary heap to solve it, and this required too many explanations: the blog post was just getting too long. So this post will complete the previous post and look into binary heaps in Perl 6.

Spoiler Alert: This weekly challenge deadline is due in several days from now (July 28, 2019). This blog post offers some solutions to this challenge, please don't read on if you intend to complete the challenge on your own.

This part of the challenge goes like this:

Six slices of pie

The first task of the 15th Weekly Challenge was to find the optimal position in the line for dessert.

This is a reformulation of the old conundrum of creating buggy whips or cowboy movies or physical book stores or carbon-based energy sources: when is the best time to opt in for an ever-increasing slice of an ever-diminishing pie?

In this task, the pie is big enough for 100 people, but the first person in line gets a slice equivalent to only 1% of it. The second person gets 2% of what’s left, the third gets 3% of what remains after that, et cetera, et cetera, until the one-hundredth person receives 100% of whatever crumbs are left after the preceding ninety-nine have taken their share. In other words, we’re “Making Apple-pie Great Again!”

Windows and Perl

Doing development work on Windows is becoming easier and better all the time. However, it's still somewhat cumbersome to do development work ON Windows, FOR Windows.

Strawberry
Luckily for us Perl developers, the team making Strawberry Perl have done an excellent job for quite some time. Their efforts make installing and using Perl on our Windows environments (PowerShell or cmd.exe) dead simple.

BerryBrew
That being said, something we find ourselves doing more and more frequently when doing development work on Linux is switching between versions of Perl quickly and easily. This has been available to us with BerryBrew for some time.

What Have I Done?! (PSPerl)
What I've started work on recently is a PowerShell version of a Perl environment switcher, called PSPerl. On linux we have perlbrew and plenv, why not have another option on Windows?

Given that it's written entirely in PowerShell v5+, it should be easy to update, maintain, and add new features - or even implement your own features. Give it a try. Find problems. Help me fix them.

The Perl Community - a mixed bag of sometimes intollerance and sometimes fantastic help

I've had occassions to post a post like this a number of times in the past over a lot of years but I've resisted until now. I have little experience of other programming languages in forums/IRC/blogs etc recently because these days I've largely written Perl and PL/SQL but in the past before the WWW was prevelant I programmed in Fortran, Pascal, C, Macro 32, VMS, Z80, 8086, 6502 etc before I volunteered to port a large code base from VMS to UNIX.

Perl Weekly Challenge # 18: Longest Common Substrings, Priority Queues, and a Functional Object System

These are some answers to the Week 18 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in several days from now (July 28, 2019). This blog post offers some solutions to this challenge, please don't read on if you intend to complete the challenge on your own.

Challenge # 1: Longest Common Substrings

Write a script that takes 2 or more strings as command line parameters and print the longest common substring. For example, the longest common substring of the strings “ABABC”, “BABCA” and “ABCBA” is string “ABC” of length 3. Other common substrings are “A”, “AB”, “B”, “BA”, “BC” and “C”. Please check this wiki page for details.

Swiss Perl Workshop 2019 - Less Than One Month

It's less than one month until this year's Swiss Perl Workshop. Perhaps you would like to join us and maybe even submit a talk? It can be anything Perl, anything slightly related to Perl, or just plain old anything that you've found interesting in technology recently that isn't Perl at all.

This year's workshop wouldn't be possible without the sponsors, so we thought we should tell you a little bit about them:


Oetiker + Partner AG
OETIKER+PARTNER is a Swiss system management and software development company. Employees from O+P are involved in many Open Source Software projects.
Humanstate
Founded in 2001, Humanstate is a private technology services group that provides businesses and organisations in various vertical markets with state-of-the-art web-based software applications integrated with on-demand payment processing. Humanstate owns and manages a global transactional platform that forms the core of all its solutions.
infomaniak

The Time::Local Trap

The localtime and gmtime built-in functions are interfaces to the POSIX functions for turning a Unix epoch timestamp into date-time components in either UTC or the system local time. When you want to go the other way, there's Time::Local.

Well, almost.

The localtime and gmtime functions have two quirks as compared to the date-time components humans might expect. The value it returns for the month is 0-indexed, ostensibly so that it can be used directly in a 0-indexed array of 12 month names, and the value it returns for the year is the number of years since the year 1900.

Perl Weekly Challenge 017: Ackermann Function and URL Parsing

Ackermann Function

A(m, n) = n + 1                  if m = 0
A(m, n) = A(m - 1, 1)            if m > 0 and n = 0
A(m, n) = A(m - 1, A(m, n - 1))  if m > 0 and n > 0

I know that Perl6 supports multisubs, but when I see a function definition of this kind, I always think Erlang, where you get pattern matching and multisubs by default. Here’s how it looks:

-module(ackermann).
-export([ackermann/2]).

ackermann(0, N) ->
    N + 1;
ackermann(M, 0) ->
    ackermann(M - 1, 1);
ackermann(M, N) ->
    ackermann(M - 1, ackermann(M, N - 1)).

Perl solution is a bit less straightforward.

Perl jam @Booking.com ML center

I just spotted this on Twitter.

Booking.com are hosting a "Perl Jam" event on Monday 29 July 2019, 6:00pm
at Azrieli Sarona Tower, Tel Aviv-Yafo Israel.

There are three speakers- so it looks like a great way to spend an evening rather than a full on conference.

Registration at: https://bookingcareerseventstelaviv.com/perl-meetup-booking-com-ml-center

Up, up and Away!

This week's perl weekly challenge was a lot of fun. First of all, challenge number 2 was a breeze. The challenge was to parse and print the components of a URL. That was easy...

Perl Weekly Challenge && Damian Conway

I have been a big fan of Damian Conway ever since I bought his book "Perl Best Practices". With the recent regular blog by him about various tasks from the Perl Weekly Challenge showed us really interesting aspects of the task. The one he blogged, Vigenère vs Vigenère last was really special about Vigenere Cipher. I must admit I didn't know half of what he shared in the blog. I wonder how many knew the Vigenere Cipher wasn't invented by Vigenere.

I think, I owe him a drink next time I meet, as per the English tradition. However, being a practicing Muslim, I can't offer drink, instead I would offer box of chocolates.

In the mean time, I am going to reserve space for him in the weekly blog by name "Damian Corner" where we would talk about his blog.

What else happening these days?

I try to keep myself busy during the week by writing short blogs. In the weekly series "Meet The Champion", we recently spoke to Adam Russell, the winner of Perl Weekly Challenge - 016.

I also blogged about the optional API task which we introduced recently. Check out how Neil Bowers took it to another level. Perl Weekly Challenge - Optional API Task.

Enjoy !!!

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.