From fbbe8919f0859badcecb909838a8c221a34c90ba Mon Sep 17 00:00:00 2001 From: brtastic Date: Mon, 1 Jun 2020 22:48:58 +0200 Subject: challenge 63 solution by brtastic --- challenge-063/brtastic/README | 1 + challenge-063/brtastic/blog.txt | 1 + challenge-063/brtastic/perl/ch-1.pl | 23 +++++++++++++++++++++++ challenge-063/brtastic/perl/ch-2.pl | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 challenge-063/brtastic/README create mode 100644 challenge-063/brtastic/blog.txt create mode 100644 challenge-063/brtastic/perl/ch-1.pl create mode 100644 challenge-063/brtastic/perl/ch-2.pl 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; -- cgit From d16853c4fc2787f99a4ae182b558edfdc1608753 Mon Sep 17 00:00:00 2001 From: brtastic Date: Mon, 1 Jun 2020 23:25:35 +0200 Subject: Uncomment & fix the tests --- challenge-063/brtastic/perl/ch-1.pl | 16 ++++++++-------- challenge-063/brtastic/perl/ch-2.pl | 24 ++++++++++++------------ 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/challenge-063/brtastic/perl/ch-1.pl b/challenge-063/brtastic/perl/ch-1.pl index 3561f416d4..a0cc90e27a 100644 --- a/challenge-063/brtastic/perl/ch-1.pl +++ b/challenge-063/brtastic/perl/ch-1.pl @@ -13,11 +13,11 @@ sub last_word 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; +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 index 8af25688ef..c934ed6bce 100644 --- a/challenge-063/brtastic/perl/ch-2.pl +++ b/challenge-063/brtastic/perl/ch-2.pl @@ -22,15 +22,15 @@ sub rotate_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; +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; -- cgit