diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-06-02 00:39:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-02 00:39:50 +0100 |
| commit | 2b83609d0089e0bf6085437d0e6dd9eaa9bb18d2 (patch) | |
| tree | 5eb7b44aede312a76114a93cf24dddb87df49320 | |
| parent | 8d4eaed362200bae49f71f732357691aa509df61 (diff) | |
| parent | d16853c4fc2787f99a4ae182b558edfdc1608753 (diff) | |
| download | perlweeklychallenge-club-2b83609d0089e0bf6085437d0e6dd9eaa9bb18d2.tar.gz perlweeklychallenge-club-2b83609d0089e0bf6085437d0e6dd9eaa9bb18d2.tar.bz2 perlweeklychallenge-club-2b83609d0089e0bf6085437d0e6dd9eaa9bb18d2.zip | |
Merge pull request #1783 from brtastic/challenge-63
challenge 63 solution by brtastic
| -rw-r--r-- | challenge-063/brtastic/README | 1 | ||||
| -rw-r--r-- | challenge-063/brtastic/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-063/brtastic/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-063/brtastic/perl/ch-2.pl | 36 |
4 files changed, 61 insertions, 0 deletions
diff --git a/challenge-063/brtastic/README b/challenge-063/brtastic/README new file mode 100644 index 0000000000..ff6c23a3ca --- /dev/null +++ b/challenge-063/brtastic/README @@ -0,0 +1 @@ +Solutions by brtastic. diff --git a/challenge-063/brtastic/blog.txt b/challenge-063/brtastic/blog.txt new file mode 100644 index 0000000000..ffcffe2832 --- /dev/null +++ b/challenge-063/brtastic/blog.txt @@ -0,0 +1 @@ +https://brtastic.xyz/blog/article/perl-weekly-63 diff --git a/challenge-063/brtastic/perl/ch-1.pl b/challenge-063/brtastic/perl/ch-1.pl new file mode 100644 index 0000000000..a0cc90e27a --- /dev/null +++ b/challenge-063/brtastic/perl/ch-1.pl @@ -0,0 +1,23 @@ +use v5.24; +use warnings; + +sub last_word +{ + my ($string, $regexp) = @_; + my @words = split /\s+/, $string; + + foreach (reverse @words) { + return $_ if /$regexp/; + } + + return; +} + +use Test::More; + +is last_word(' hello world', qr/[ea]l/), 'hello'; # 'hello' +is last_word("Don't match too much, Chet!", qr/ch.t/i), 'Chet!'; # 'Chet!' +ok !defined last_word("spaces in regexp won't match", qr/in re/); # undef +is last_word( join(' ', 1..1e6), qr/^(3.*?){3}/), '399933'; # '399933' + +done_testing; diff --git a/challenge-063/brtastic/perl/ch-2.pl b/challenge-063/brtastic/perl/ch-2.pl new file mode 100644 index 0000000000..c934ed6bce --- /dev/null +++ b/challenge-063/brtastic/perl/ch-2.pl @@ -0,0 +1,36 @@ +use v5.24; +use warnings; + +sub rotate_string +{ + my ($string) = @_; + my $original = $string; + my $length = length $string; + + my $rotate = sub { + substr shift() x 2, $_, $length + }; + + my $rotation = 0; + while (1) { + for (1 .. $length) { + $rotation += 1; + $string = $rotate->($string); + + return $rotation if $original eq $string; + } + } +} + +use Test::More; + +is rotate_string('xy'), 3; +is rotate_string('xyxx'), 7; +is rotate_string('xxxx'), 1; +is rotate_string('xyxy'), 3; +is rotate_string('xxyy'), 7; +is rotate_string('xxxxx'), 1; +is rotate_string('xyyxx'), 4; +is rotate_string('xyyxxyyyxxx'), 10; + +done_testing; |
