diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-06-07 22:35:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-07 22:35:50 +0100 |
| commit | 2b49121f84bcd87a5dbd92de22215bdddffc33c7 (patch) | |
| tree | 954b1b06aa68cfbf2eaee364a3290f3d00e14688 | |
| parent | 62b180d78e7c4508eeb3fd57fce7f3af6bda65a1 (diff) | |
| parent | 1ad71b18b03454a19c00ae5d34b9d3398272e470 (diff) | |
| download | perlweeklychallenge-club-2b49121f84bcd87a5dbd92de22215bdddffc33c7.tar.gz perlweeklychallenge-club-2b49121f84bcd87a5dbd92de22215bdddffc33c7.tar.bz2 perlweeklychallenge-club-2b49121f84bcd87a5dbd92de22215bdddffc33c7.zip | |
Merge pull request #1798 from choroba/ech063
Solve 063 Last Word + Rotate String by E. Choroba
| -rwxr-xr-x | challenge-063/e-choroba/perl/ch-1.pl | 15 | ||||
| -rwxr-xr-x | challenge-063/e-choroba/perl/ch-2.pl | 21 |
2 files changed, 36 insertions, 0 deletions
diff --git a/challenge-063/e-choroba/perl/ch-1.pl b/challenge-063/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..93a9104abb --- /dev/null +++ b/challenge-063/e-choroba/perl/ch-1.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub last_word { + my ($string, $re) = @_; + return (grep /$re/, split ' ', $string)[-1] +} + +use Test::More tests => 4; + +is last_word(' hello world', qr/[ea]l/), 'hello'; +is last_word("Don't match too much, Chet!", qr/ch.t/i), 'Chet!'; +is last_word("spaces in regexp won't match", qr/in re/), undef; +is last_word( join(' ', 1..1e6), qr/^(3.*?){3}/), '399933'; diff --git a/challenge-063/e-choroba/perl/ch-2.pl b/challenge-063/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..3281a5f2e7 --- /dev/null +++ b/challenge-063/e-choroba/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub rotate { + my ($word) = @_; + my $length = length $word; + my $original = $word; + my $i = 0; + do { + $word .= substr $word, 0, ++$i % $length, ""; + # print "$i: $word\n"; + } until $word eq $original; + return $i +} + +use Test::More; + +is rotate('xyxx'), 7; + +done_testing(); |
