aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-06-01 07:57:35 +0100
committerGitHub <noreply@github.com>2020-06-01 07:57:35 +0100
commit81dbbe3c5e83ad59f1e1c7f1c3daffc287c8a4e9 (patch)
treebeddb1f22b735d38968fce91c6f0b3ff06a60061
parentc7c4932fc737683a90788770a5b2662c9035bd77 (diff)
parent6bc322e92ee3b5bfe27893f853cb6be736307a2d (diff)
downloadperlweeklychallenge-club-81dbbe3c5e83ad59f1e1c7f1c3daffc287c8a4e9.tar.gz
perlweeklychallenge-club-81dbbe3c5e83ad59f1e1c7f1c3daffc287c8a4e9.tar.bz2
perlweeklychallenge-club-81dbbe3c5e83ad59f1e1c7f1c3daffc287c8a4e9.zip
Merge pull request #1778 from PerlBoy1967/branch-for-challenge-063
Task 1
-rw-r--r--challenge-063/perlboy1967/perl/ch-1.pl29
-rw-r--r--challenge-063/perlboy1967/perl/ch-2.pl30
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-063/perlboy1967/perl/ch-1.pl b/challenge-063/perlboy1967/perl/ch-1.pl
new file mode 100644
index 0000000000..f88288d2b1
--- /dev/null
+++ b/challenge-063/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 063
+#
+# Task 1 - Last Word
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use strict;
+use warnings;
+
+# Prototype
+sub last_word($$);
+
+printf "%s\n", last_word(' hello world', qr/[ea]l/); # 'hello'
+printf "%s\n", last_word("Don't match too much, Chet!", qr/ch.t/i); # 'Chet!'
+printf "%s\n", last_word("spaces in regexp won't match", qr/in re/) // '(undef)'; # undef
+printf "%s\n", last_word( join(' ', 1..1e6), qr/^(3.*?){3}/); # '399933'
+
+sub last_word ($$) {
+ my ($line, $re) = @_;
+
+ foreach my $word (reverse split(/\s+/, $line) {
+ return $word if ($word =~ /$re/);
+ }
+
+ return undef;
+}
+
diff --git a/challenge-063/perlboy1967/perl/ch-2.pl b/challenge-063/perlboy1967/perl/ch-2.pl
new file mode 100644
index 0000000000..10c617d68d
--- /dev/null
+++ b/challenge-063/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 063
+#
+# Task 2 - Rotate String
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use strict;
+use warnings;
+
+# Prototype
+sub rotateWord ($$);
+
+while (<>) {
+ my ($word, $n) = $_ =~ /(\S+)\s+(\d+)/;
+
+ printf "rotateWord('%s', %d) => '%s'\n",
+ $word, $n, rotateWord($word, $n);
+}
+
+sub rotateWord ($$) {
+ my ($word, $rotateNtimes) = @_;
+
+ my @chars = split(//, $word);
+
+ push(@chars, splice(@chars, 0, $rotateNtimes % scalar(@chars)));
+
+ return join('', @chars);
+}