Peter
Peter Campbell Smith

Perming Perl
plus Padovan primes

Weekly challenge 154 — 28 February 2022

Week 154: 28 Feb 2022

Task 1

Task — Missing permutation

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.

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.

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

Output


'LERP' is missing

 

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