aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2023-03-06 15:58:42 +0000
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2023-03-06 15:58:42 +0000
commit9d6384646870b277c64dec757178d07eabbe3423 (patch)
tree763dc2da8200936dc1b93ee47595dc45847cba37
parentd75c83429332efc88f29eee14f988b199f2fa10c (diff)
downloadperlweeklychallenge-club-9d6384646870b277c64dec757178d07eabbe3423.tar.gz
perlweeklychallenge-club-9d6384646870b277c64dec757178d07eabbe3423.tar.bz2
perlweeklychallenge-club-9d6384646870b277c64dec757178d07eabbe3423.zip
This is week 207
-rw-r--r--challenge-207/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-207/peter-campbell-smith/perl/ch-1.pl32
-rwxr-xr-xchallenge-207/peter-campbell-smith/perl/ch-2.pl34
3 files changed, 67 insertions, 0 deletions
diff --git a/challenge-207/peter-campbell-smith/blog.txt b/challenge-207/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..f095018792
--- /dev/null
+++ b/challenge-207/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/207/1
diff --git a/challenge-207/peter-campbell-smith/perl/ch-1.pl b/challenge-207/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..e8308e39ad
--- /dev/null
+++ b/challenge-207/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2023-02-20
+
+use v5.28;
+use utf8;
+use warnings;
+
+# We are given an array of words and asked to write a script to print all the words in the
+# array that can be typed using the letters on only one row of the keyboard.
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/207/1
+
+linear_words('Hello','Alaska','Dad','Peace');
+linear_words('OMG','Bye');
+linear_words('typewriter', 'zmncbxvz', 'shakalshas');
+linear_words(qw[We are given an array of words and asked to write a script to print all the words in the
+ array that can be typed using the letters on only one row of the keyboard]);
+
+sub linear_words {
+
+ my (@test, $word, $result);
+ $result = '';
+
+ # loop over words and save those that are monolinear
+ for $word (@_) {
+ $result .= qq['$word', ] if $word =~ m¦^([qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+)$¦i;
+ }
+
+ say qq[\nInput: ('] . join(q[', '], @_) . qq[')];
+ say qq[Output: (] . substr($result, 0, -2) . qq[)];
+}
diff --git a/challenge-207/peter-campbell-smith/perl/ch-2.pl b/challenge-207/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..724f9ac8f2
--- /dev/null
+++ b/challenge-207/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2023-02-20
+
+use v5.28;
+use utf8;
+use warnings;
+
+# We are given a list of the number of citations a researcher has received
+# for each of his published papers, ordered from most cited to least.
+# We are asked to write a script to compute the researcher’s H-index, which is the maximum n
+# where the n'th number in the list is at least n.
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/207/2
+
+h_index(10, 8, 5, 4, 3);
+h_index(25, 8, 5, 3, 3);
+h_index(10, 9, 8, 7, 6, 5, 4, 3, 2, 1);
+h_index(0);
+h_index();
+h_index(4, 4, 4, 4);
+
+sub h_index {
+
+ my (@list, $j);
+
+ # loop over list (0-based!) to find first where n'th number in list < $n
+ @list = @_;
+ for ($j = 0; $j < scalar @list; $j ++) {
+ last unless $list[$j] >= $j + 1;
+ }
+ say qq[\nInput: \@citations = (] . join(', ', @list) .
+ qq[)\nOutput: $j];
+}