diff options
| author | brtastic <brtastic.dev@gmail.com> | 2020-06-01 22:48:58 +0200 |
|---|---|---|
| committer | brtastic <brtastic.dev@gmail.com> | 2020-06-01 22:48:58 +0200 |
| commit | fbbe8919f0859badcecb909838a8c221a34c90ba (patch) | |
| tree | 2cfcc4381858d1d059c16f82e8f6ee5b8ef4b50a | |
| parent | d67822d626374fde369ee19ba98553661df3c3b3 (diff) | |
| download | perlweeklychallenge-club-fbbe8919f0859badcecb909838a8c221a34c90ba.tar.gz perlweeklychallenge-club-fbbe8919f0859badcecb909838a8c221a34c90ba.tar.bz2 perlweeklychallenge-club-fbbe8919f0859badcecb909838a8c221a34c90ba.zip | |
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..3561f416d4 --- /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..8af25688ef --- /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'), 4; +# +# done_testing; |
