DEV Community

loading...

Perl Weekly Challenge 035

kianmeng profile image Kian Meng, Ang ・6 min read

Originally published in kianmeng.org and Perl Weekly Challenge.

Continues from previous week.

Feel free to submit a merge request or open a ticket if you found any issues with this post. We highly appreciate and welcome your feedback.

For a quick overview, go through the original tasks and recap of the weekly challenge.

Additional feedback to the our Perl Weekly Challenge’s Twitter account is much appreciated.

Task #1

CPAN modules used: Data::Dumper, Term::ANSIColor, Term::ANSIScreen, Term::Size::Perl, Test::More, Text::Morse, Text::Trim, Time::HiRes, autodie, constant, experimental, feature, lib, strict, utf8, warnings

If you need to have a quick overview on which submitted solutions to read or study, look at the submissions by Andrezgz and Javier Luque. Both solutions were rather easy to follow and read.

Going through all the submissions, we observed two approaches used by participants to encode text into binary encoded Morse code. The first was to generate the binary form from two signal durations (dots and dashes) dynamically. Participant who preferred this way were Petr Roubicek, Javier Luque, Ruben Westerberg, Laurent Rosenfeld, Ulrich Rieke, Roger Bell West, Ryan Thompson, Andrezgz, Colin Crain, and E. Choroba.

While the second was to use a pre-generate list of characters to binary lookup table. Participants who used such approach were Duane Powell, Adam Russell, Burkhard Nickels, Jaldhar H. Vyas, Steven Wilson, Giuseppe Terlizzi, and Giuseppe Terlizzi.

As usual, selected submissions that caught our attention worth the discussions.

Only Ruben Westerberg and E. Choroba used pack and unpack function to process binary data. The default Perl tutorial on this matter will shed some light on using these two functions.

As usual, Burkhard Nickels solved this week challenges using different CPAN modules to demonstrate Morse code in a visual manner. Read his very details write-up on how he tackle this challenge. While looking at his solution, we noticed that he used the CPAN module, Term::Size::Perl to determine the width of the terminal. In comparison,Laurent Rosenfeld formatted the output to a predefined 80 characters as shown in code below.

    sub format80c {
        shift =~ s/(.{80})/$1\n/gr;
    }

Ryan Thompson was the only participant that wrote a script that download, parse, and generate the Morse code into Perl script from Wikipedia page. While the script itself was far more complex that the actual solution, we can learn a few Perl tricks.

Colin Crain solution demonstrated that writing a proper program according to a specification or standard, in this case, M.1677 : International Morse code was a tedious effort.

Task #2

CPAN modules used: Data::Dumper, Test::More, autodie, constant, experimental, feature, lib, strict, utf8, warnings

Since this task was similar to Task 1 but done in reverse way (some reverse the lookup table), most of the participants reuse the same code when decoding Morse code. However, there were a selected few submissions we can read and learn from.

Again, start with the solution by Javier Luque. Submission with error handling and correction was easy to read and understand.

Andrezgz applied the Dispatch table (in past challenge) when decoding the Morse code.

    my %morse_bin_inv = (
        '1'        => \&add_dot   ,  # dot
        '111'      => \&add_dash  ,  # dash
        '0'        => sub { }     ,  # intra-character gap
        '000'      => \&add_char  ,  # character gap
        '0000000'  => \&add_word  ,  # word gap
    );

Colin Crain, besides solving it against a specification, used a State Machine to decode the Morse signal continuously. This was probably the only comprehensive solution to handle exception with erroneous Morse code signal.

See Also

(1) Perl Weekly Challenge - 035 by Javier Luque
(2) Morse and Remorse with Raku by Arne Sommer
(3) Perl Weekly Challenge 035 by Adam Russell
(4) Perl Weekly Challenge #35 by Burkhard Nickels
(5) Perl Weekly Challenge 35: Binary Encoded Morse Code by Laurent Rosenfeld
(6) Perl Weekly Challenge: Week 35 by Jaldhar H. Vyas
(7) Perl Weekly Challenge 35: Binary Morse Code by Roger Bell West
(8) Perl Weekly Challenge 035: Binary Morse Code by E. Choroba

Discussion

pic
Editor guide