diff options
| -rw-r--r-- | challenge-279/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-279/bob-lied/perl/ch-1.pl | 66 | ||||
| -rw-r--r-- | challenge-279/bob-lied/perl/ch-2.pl | 64 |
3 files changed, 133 insertions, 3 deletions
diff --git a/challenge-279/bob-lied/README b/challenge-279/bob-lied/README index 280b5bfa87..c57c6cd9ae 100644 --- a/challenge-279/bob-lied/README +++ b/challenge-279/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 277 by Bob Lied +Solutions to weekly challenge 279 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-277/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-277/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-279/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-279/bob-lied diff --git a/challenge-279/bob-lied/perl/ch-1.pl b/challenge-279/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..d00b02cf85 --- /dev/null +++ b/challenge-279/bob-lied/perl/ch-1.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-1.pl Perl Weekly Challenge 279 Task 1 Sort Letters +#============================================================================= +# You are given two arrays, @letters and @weights. +# Write a script to sort the given array @letters based on the @weights. +# Example 1 Input: @letters = ('R', 'E', 'P', 'L') +# @weights = (3, 2, 1, 4) +# Output: PERL +# Example 2 Input: @letters = ('A', 'U', 'R', 'K') +# @weights = (2, 4, 1, 3) +# Output: RAKU +# Example 3 Input: @letters = ('O', 'H', 'Y', 'N', 'P', 'T') +# @weights = (5, 4, 2, 6, 1, 3) +# Output: PYTHON +#============================================================================= + +use v5.40; + +use Getopt::Long; +my $Verbose = false; +my $DoTest = false; +my $Benchmark = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark); +exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +my @letters = split(//, shift); +my @weights = @ARGV; +say sortLetters(\@letters, \@weights); + +sub sortLetters($letters, $weights) +{ + join '', + map { $_->[0] } + sort { $a->[1] <=> $b->[1] } + map { [ $letters->[$_] => $weights->[$_] ] } + 0 .. $letters->$#* +} + +sub runTest +{ + use Test2::V0; + + is( sortLetters( [qw(R E P L)], [3,2,1,4] ), "PERL", "Example 1"); + is( sortLetters( [qw(A U R K)], [2,4,1,3] ), "RAKU", "Example 2"); + is( sortLetters( [qw(O H Y N P T)], [5,4,2,6,1,3] ), "PYTHON", "Example 3"); + + is( sortLetters( [qw(B B D E L I O)], [3,1,7,6,4,5,2] ), "BOBLIED", "Duplicate letters"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} + diff --git a/challenge-279/bob-lied/perl/ch-2.pl b/challenge-279/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..530bc73264 --- /dev/null +++ b/challenge-279/bob-lied/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-2.pl Perl Weekly Challenge 279 Task 2 Split String +#============================================================================= +# You are given a string, $str. +# Write a script to split the given string into two containing exactly +# same number of vowels and return true if you can otherwise false. +# Example 1 Input: $str = "perl" +# Ouput: false +# Example 2 Input: $str = "book" +# Ouput: true +# Two possible strings "bo" and "ok" containing exactly one vowel each. +# Example 3 Input: $str = "good morning" +# Ouput: true +# Two possible strings "good " and "morning" containing two vowels each +# or "good m" and "orning" containing two vowels each. (Others also work). +#============================================================================= + +use v5.40; + +use Getopt::Long; +my $Verbose = false; +my $DoTest = false; +my $Benchmark = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark); +exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +say splitString($_) for @ARGV; + +sub splitString($str) +{ + # Too easy. It's possible if there's an even number of vowels. + return ($str =~ tr/aeiou//d) % 2 == 0; +} + +sub runTest +{ + use Test2::V0; + + is( splitString("perl"), false, "Example 1"); + is( splitString("book"), true, "Example 2"); + is( splitString("good morning"), true, "Example 3"); + is( splitString("xyzzy"), true, "No vowels"); + is( splitString("aeiou"), false, "All vowels, odd"); + is( splitString("aeioua"), true, "All vowels, even"); + + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} + |
