diff options
| author | pme <hauptadler@gmail.com> | 2024-06-24 18:25:26 +0200 |
|---|---|---|
| committer | pme <hauptadler@gmail.com> | 2024-06-24 18:25:26 +0200 |
| commit | 991ec4276b44b5e3724c12484ff396e9363e5f7b (patch) | |
| tree | 5271421c8b27a57e1bdd761a9a260a7f110fc8ca | |
| parent | e706d2548a3ce7ff5d3ae480224ee3ee6c5577de (diff) | |
| download | perlweeklychallenge-club-991ec4276b44b5e3724c12484ff396e9363e5f7b.tar.gz perlweeklychallenge-club-991ec4276b44b5e3724c12484ff396e9363e5f7b.tar.bz2 perlweeklychallenge-club-991ec4276b44b5e3724c12484ff396e9363e5f7b.zip | |
challenge-275
| -rwxr-xr-x | challenge-275/peter-meszaros/perl/ch-1.pl | 69 | ||||
| -rwxr-xr-x | challenge-275/peter-meszaros/perl/ch-2.pl | 78 |
2 files changed, 147 insertions, 0 deletions
diff --git a/challenge-275/peter-meszaros/perl/ch-1.pl b/challenge-275/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..69149d8055 --- /dev/null +++ b/challenge-275/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Broken Keys + +You are given a sentence, $sentence and list of broken keys @keys. + +Write a script to find out how many words can be typed fully. + +=head2 Example 1 + + Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a') + Output: 0 + +=head2 Example 2 + + Input: $sentence = "Perl and Raku", @keys = ('a') + Output: 1 + +Only Perl since the other word two words contain 'a' and can't be typed fully. + +=head2 Example 3 + + Input: $sentence = "Well done Team PWC", @keys = ('l', 'o') + Output: 2 + +=head2 Example 4 + + Input: $sentence = "The joys of polyglottism", @keys = ('T') + Output: 2 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [["Perl Weekly Challenge", ['l', 'a']], 0], + [["Perl and Raku", ['a']], 1], + [["Well done Team PWC", ['l', 'o']], 2], + [["The joys of polyglottism", ['T']], 2], +]; + +sub broken_keys +{ + my $s = $_[0]->[0]; + my $keys = $_[0]->[1]; + + my $cnt = 0; + my %h; + + WORD: for my $w (split / /, $s) { + for my $k (@$keys) { + next WORD unless index(lc($w), lc($k)) < 0; + } + ++$cnt; + } + + return $cnt; + +} + +for (@$cases) { + is(broken_keys($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-275/peter-meszaros/perl/ch-2.pl b/challenge-275/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..b25938d8f9 --- /dev/null +++ b/challenge-275/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,78 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Replace Digits + +You are given an alphanumeric string, $str, where each character is either a +letter or a digit. + +Write a script to replace each digit in the given string with the value of the +previous letter plus (digit) places. + +=head2 Example 1 + + Input: $str = 'a1c1e1' + Ouput: 'abcdef' + + shift('a', 1) => 'b' + shift('c', 1) => 'd' + shift('e', 1) => 'f' + +=head2 Example 2 + + Input: $str = 'a1b2c3d4' + Output: 'abbdcfdh' + + shift('a', 1) => 'b' + shift('b', 2) => 'd' + shift('c', 3) => 'f' + shift('d', 4) => 'h' + +=head2 Example 3 + + Input: $str = 'b2b' + Output: 'bdb' + +=head2 Example 4 + + Input: $str = 'a16z' + Output: 'abgz' + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ['a1c1e1', 'abcdef'], + ['a1b2c3d4', 'abbdcfdh'], + ['b2b', 'bdb'], + ['a16z', 'abgz'], + ['816z', undef], +]; + +sub replace_digits +{ + my $str = shift; + + my @str = split //, $str; + return undef if $str[0] =~ /\d/; + my $c = ord($str[0]); + for my $i (1..$#str) { + if ($str[$i] =~ /\d/) { + $str[$i] = chr($c+$str[$i]); + } else { + $c = ord($str[$i]); + } + } + return join '', @str; +} + +for (@$cases) { + is(replace_digits($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; + |
