perl-weekly-challenge Archives

Perl Weekly Challenge 038: Date Finder and Word Game

Date Finder

Create a script to accept a 7 digits number, where the first number can only be 1 or 2. The second and third digits can be anything 0-9. The fourth and fifth digits corresponds to the month i.e. 01, 02, 03…, 11, 12. And the last 2 digits respresents the days in the month i.e. 01, 02, 03…, 29, 30, 31. Your script should validate if the given number is valid as per the rule and then convert into human readable format date.

RULES

  1. If 1st digit is 1, then prepend 20 otherwise 19 to the 2nd and 3rd digits to make it 4-digits year.
  2. The 4th and 5th digits together should be a valid month.
  3. The 6th and 7th digits together should be a valid day for the above month.

For example, the given number is 2230120, it should print 1923-01-20.

As we’ve done several times, we’ll use the core module Time::Piece to handle dates.

#!/usr/bin/perl
use warnings;
use strict;

use Time::Piece;

sub validate {
    my ($number) = @_;

First, we’ll check the length of the input string.

    die 'Invalid length' unless length $number == 7;

Perl Weekly Challenge 037: Weekdays and Daylight Gain/Loss

Weekdays

Write a script to calculate the total number of weekdays (Mon-Fri) in each month of the year 2019.

I used the core module Time::Piece and its companion from the same distribution, Time::Seconds. Let’s start on the first day of the month, and keep adding one day while we stay in the same month. Along the way, count the days that aren’t Saturdays and Sundays.

#!/usr/bin/perl
use warnings;
use strict;

use Time::Piece;
use Time::Seconds qw{ ONE_DAY };

sub days_in_month {
    my ($month) = @_;
    my $date = 'Time::Piece'->strptime("2019 $month 1 12:00",
                                       '%Y %b %d %H:%M');
    my $count = 0;
    while ($date->month eq $month) {
        ++$count unless grep $date->day eq $_, qw( Sat Sun );
        $date += ONE_DAY;
    }
    return $count
}

And here’s a test that the numbers are correct:

Perl Weekly Challenge 036: VIN Validation and the Knapsack Problem

VIN Validation

Write a program to validate given Vehicle Identification Number (VIN).

I followed the description at Wikipedia. Sometimes, it wasn’t exactly clear whether the described rule should be valid everywhere or just in a part of the world; the rules also developed with time, so older vehicles can bear VINs that would be considered invalid for a modern car.

Most of the validation is implemented in a single subroutine validate_vin. It takes two parameters, $vin and $sold: the second one says where the car was sold. "North America" and "China" are two values that trigger a different behaviour of the validator.

Perl Weekly Challenge 035: Binary Morse Code

The Encoder

Write a program to encode text into binary encoded Morse code.

Before we can encode Morse code into its binary representation, we need to encode normal text into Morse code. As a former Woodcraft member, I was able to write the following lines by heart:

my %to_morse = qw( a .-   b -... c -.-. d -..  e .    f ..-. g --.
                   h .... i ..   j .--- k -.-  l .-.. m --   n -.
                   o ---  p .--. q --.- r .-.  s ...  t -    u ..-
                   v ...- w .--  x -..- y -.-- z --.. );

The encoding subroutine is straightforward: split each word into separate characters, then replace each with the value from the above hash.

sub encode_to_morse {
    join '/', map $to_morse{$_} // "", split //, shift
}

Note that space is not present in the translation table, so it gets translated to an empty string, which creates the expected double slashes between words.

Perl Weekly Challenge 034: Slices and a Dispatch Table

Slices

Write a program that demonstrates using hash slices and/or array slices.

In the spirit of TIMTOWTDI I decided to write a single program that demonstrates both the tasks at the same time.

Let’s start with slices. Slices are parts of structures (arrays and hashes). Slicing has a special syntax by which you tell Perl which indices or keys you want to use to obtain a slice.

For example, consider the following array:

my @keys = qw( not_this_one
               this_one
               this_one_too
               it_was_enough );

Naturally, we want to select the second and third one. We can use

$keys[1], $keys[2]

or

map $keys[$_], 1, 2;

but there’s a shorter and cleaner syntax for the same:

@keys[1, 2]

Perl Weekly Challenge 033: Count Letters & Formatted Multiplication Table

Count Letters

Create a script that accepts one or more files specified on the command-line and count the number of times letters appeared in the files.

From the example we can see that we should count the letters in a case insensitive way (see the lc in the example below). Similarly to Challenge 032, we can use a hash to keep the counts.

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

my %count;
while (<>) {
    ++$count{ lc $1 } while /([a-zA-Z])/g;
}
for my $char (sort keys %count) {
    say "$char: $count{$char}";
}

Perl Weekly Challenge 032: Frequency Table & ASCII Bar Chart

Frequency Table

Create a script that either reads standard input or one or more files specified on the command-line. Count the number of times and then print a summary, sorted by the count of each entry.

The original title of the task was “Count instances”, but I’ve always known the output as the “frequency table”. For years, I’ve used the following bash script to produce it:

#! /bin/bash
cat "$@" | sort | uniq -c | sort -n

The first element in the pipeline is cat. It outputs all the files given to it as arguments, or the standard input if there are no arguments. sort sorts the output so duplicate lines are adjacent, which is needed for uniq. Its -c argument means “count”: it prepends the number of occurrences to each line. The final sort is invoked with -n for “numerical”, i.e. it sorts the output by the number of occurrences.

Creating a similar table in Perl is a FAQ. We store each line in a hash, incrementing the corresponding value while reading the input line by line.

If we look carefully at the assignment of the task, though, we can notice that the output should be formatted differently: the numbers should go last and the columns should be aligned. Also, there’s the extra credit which we definitely want.

Perl Weekly Challenge 031: Division by Zero & Dynamic Variable Name

Division by zero

Create a function to check divide by zero error without checking if the denominator is zero.

Perl already checks the denominator when dividing. All we need to do is to catch the exception it throws and check it instead of checking the value of the denominator.

The following division subroutine uses Try::Tiny to catch the exception. It’s a good practice not to use the low level eval, see for example Bug in eval in pre-5.14 for the reasons.

#!/usr/bin/perl
use warnings;
use strict;

use Try::Tiny;

sub division {
    my ($numerator, $denominator) = @_;
    try {
        { safe => 1, result => $numerator / $denominator }
    } catch {
        { safe => 0, error => $_ }
    }
}

use Test::More tests => 4;

ok division(1,2)->{safe};
ok !division(1,0)->{safe};

is division(1,2)->{result}, 1/2;
like division(1,0)->{error}, qr/Illegal division by zero/;

Perl Weekly Challenge 030: Sunday Christmas and Series with sum 12

Sunday Christmas

Write a script to list dates for Sunday Christmas between 2019 and 2100. For example, 25 Dec 2022 is Sunday.

I used the core module Time::Piece to check the dates.

It’s easy to create a string representing Christmas: just concatenate the year with '-12-25'. The module’s strptime method can be used to create an object if we provide a format of the input string, in this case it’s '%Y-%m-%d'. The object’s method day_of_week now tells us what day the object represents, 0 corresponds to Sunday, which is the day we’re interested in.

Perl Weekly Challenge 029: Brace expansion and calling a C function

Brace expansion

Write a script to demonstrate brace expansion. For example, the script would take the command line argument Perl {Daily,Weekly,Monthly,Yearly} Challenge and should expand it and print like below:
Perl Daily Challenge
Perl Weekly Challenge
Perl Monthly Challenge
Perl Yearly Challenge

You’ve probably heard about the glob function. It can expand wildcards like * or ?, so you e.g. can easily check what files correspond to p*.p?. But glob can do one more thing for us: brace expansion.

About E. Choroba

user-pic I blog about Perl.