diff options
| author | Bob Lied <boblied+github@gmail.com> | 2023-12-11 08:58:41 -0600 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2023-12-11 08:58:41 -0600 |
| commit | cf780136eaea2075524c500d5989bcf5403d8c9b (patch) | |
| tree | 61c034d73d798050620dd94100bbf4a46ef879b4 | |
| parent | f52eeb84259418b208d965ac7f97819de86bd8de (diff) | |
| download | perlweeklychallenge-club-cf780136eaea2075524c500d5989bcf5403d8c9b.tar.gz perlweeklychallenge-club-cf780136eaea2075524c500d5989bcf5403d8c9b.tar.bz2 perlweeklychallenge-club-cf780136eaea2075524c500d5989bcf5403d8c9b.zip | |
PWC 247 Task 2 complete
| -rw-r--r-- | challenge-247/bob-lied/perl/ch-2.pl | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-247/bob-lied/perl/ch-2.pl b/challenge-247/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..07735608c3 --- /dev/null +++ b/challenge-247/bob-lied/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge 247 Task 2 Most Frequent Letter Pair +#============================================================================= +# You are given a string S of lower case letters 'a'..'z'. +# Write a script that finds the pair of consecutive letters in S that +# appears most frequently. If there is more than one such pair, choose +# the one that is the lexicographically first. +# Example 1 Input: $s = 'abcdbca' +# Output: 'bc' ('bc' appears twice in `$s`) +# Example 2 Input: $s = 'cdeabeabfcdfabgcd' +# Output: 'ab' +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub mflp($s) +{ + my %freq; + $freq{$_}++ for map { substr($s, $_, 2) } 0 .. (length($s)-2); + return (sort { $freq{$b} <=> $freq{$a} || $a cmp $b } keys %freq)[0]; +} + +sub runTest +{ + use Test2::V0; + + is( mflp("abcdbca"), 'bc', "Example 1"); + is( mflp("cdeabeabfcdfabgcd"), 'ab', "Example 2"); + + done_testing; +} |
