diff options
| -rw-r--r-- | challenge-275/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-275/peter-campbell-smith/perl/ch-1.pl | 36 | ||||
| -rwxr-xr-x | challenge-275/peter-campbell-smith/perl/ch-2.pl | 37 |
3 files changed, 74 insertions, 0 deletions
diff --git a/challenge-275/peter-campbell-smith/blog.txt b/challenge-275/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..7a7f960cc1 --- /dev/null +++ b/challenge-275/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/275 diff --git a/challenge-275/peter-campbell-smith/perl/ch-1.pl b/challenge-275/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..464bc4ee42 --- /dev/null +++ b/challenge-275/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-06-24 +use utf8; # Week 275 - task 1 - Broken keys +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +# 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. + +my ($sentence, @keys); + +broken_keys('Perl Weekly Challenge', 'l', 'a'); +broken_keys('The joys of polyglottism', 'T'); +broken_keys('Write a script to find out how many words can be typed fully', 'i', 'o'); +broken_keys('All cows eat grass', 'b', 'd', 'f', 'h', 'i'); +broken_keys('Vitamins keep you healthy', 'v', 'k', 'u', 'y'); + +sub broken_keys { + + my ($sentence, @keys, $count); + + printf(qq[\nInput: \$sentence = ('%s'), \@keys = ('%s')\n], $_[0], join(q[', '], @_[1 .. @_ - 1])); + $sentence = lc(shift @_); + push @keys, lc($_) for @_; + + # change any occurrences of keys to '#' + $sentence =~ s|$_|#|g for @keys; + + # count the words which don't contain '#' + $count += ($_ !~ m|#| ? 1 : 0) for split(/ /, $sentence); + + printf(qq[Output: %s\n], defined $count ? $count : 0); +} diff --git a/challenge-275/peter-campbell-smith/perl/ch-2.pl b/challenge-275/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..2fa54e9f38 --- /dev/null +++ b/challenge-275/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-06-24 +use utf8; # Week 275 - task 2 - Replace digits +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +replace_digits('a9c9e9'); +replace_digits('a1b2c3d4'); +replace_digits('a2c6d1n6'); + +sub replace_digits { + + my ($j, $char, $str, $result, $prev); + + $str = shift; + + # loop over characters in $str + for $j (0 .. length($str)) { + $char = substr($str, $j, 1); + + # if it's a digit do the magic + if ($j > 0 and $char =~ m|([0-9])|) { + $result .= chr(ord($prev) + $char); + + # else don't + } else { + $result .= $char; + } + $prev = $char; + } + + printf(qq[\nInput: \@str = '%s'\n], $str); + printf(qq[Output: '%s'\n], $result); +} |
