Peter’s blog ✴ Week 154 ✴ 28 February 2022

THE WEEKLY CHALLENGE
Perming Perl plus Padovan primes

The Perl Camel

Task 1

Missing permutation

You are given possible permutations of the string 'PERL'.

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

Examples


Example 1
Input:
PELR, PREL, PERL, PRLE, PLER, PLRE, EPRL, EPLR, ERPL,
ERLP, ELPR, ELRP, RPEL, RPLE, REPL, RELP, RLPE, RLEP,
LPER, LPRE, LEPR, LRPE, LREP
Output: 'LERP' is missing

Analysis

I went for the easy solution of using Algorithm::Combinatorics to generate all the possible permutations and then simply looking any of those that didn't match the supplied list - and there was only one.

Perl Weekly’s review

from PW issue 554

Short and precise blog to explain Peter's solutions. Thanks for sharing.

This review may cover either or both challenges for this week.

Script


#!/usr/bin/perl

# Peter Campbell Smith - 2022-02-28
# PWC 154 task 1

use v5.28;
use strict;
use utf8;
use Algorithm::Combinatorics qw(permutations);

my ($given, $iter, $perm, $word);

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

# get all permutations
$iter = permutations(['P', 'E', 'R', 'L']);

# print the one(s) that don't match $given
while ($perm = $iter->next) {
    $word = join('', @$perm);
    say qq['$word' is missing] unless $given =~ m|$word|;
}   

12 lines of code

Output from script


'LERP' is missing

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain