aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-06-24 22:32:01 +0100
committerGitHub <noreply@github.com>2024-06-24 22:32:01 +0100
commitd91fc46c5edf34fd96b58608eeb0e3835afffdec (patch)
tree809f64727268784751d774213d79f88451dfedaf
parent17667dadffe84537f2b15b6f2ef6b429c587cf8b (diff)
parent2a181d86de5581d752d7542c76ae6091d7d760c7 (diff)
downloadperlweeklychallenge-club-d91fc46c5edf34fd96b58608eeb0e3835afffdec.tar.gz
perlweeklychallenge-club-d91fc46c5edf34fd96b58608eeb0e3835afffdec.tar.bz2
perlweeklychallenge-club-d91fc46c5edf34fd96b58608eeb0e3835afffdec.zip
Merge pull request #10318 from pjcs00/wk275
Week 275 - Broken digits
-rw-r--r--challenge-275/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-275/peter-campbell-smith/perl/ch-1.pl36
-rwxr-xr-xchallenge-275/peter-campbell-smith/perl/ch-2.pl37
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);
+}