aboutsummaryrefslogtreecommitdiff
path: root/challenge-063
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-06-03 23:51:28 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-06-03 23:51:28 +0100
commit0b33db6d7ec47fbde3d3a91c202e633d5715a608 (patch)
treefcb04afa0285f05eea125268dbbeaf4b5fbf888b /challenge-063
parent2449f9a8821f7ed54f532107cfb11dbb43e138af (diff)
downloadperlweeklychallenge-club-0b33db6d7ec47fbde3d3a91c202e633d5715a608.tar.gz
perlweeklychallenge-club-0b33db6d7ec47fbde3d3a91c202e633d5715a608.tar.bz2
perlweeklychallenge-club-0b33db6d7ec47fbde3d3a91c202e633d5715a608.zip
- Added solutions by Arne Sommer.
Diffstat (limited to 'challenge-063')
-rw-r--r--challenge-063/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-063/arne-sommer/perl/ch-1.pl25
-rwxr-xr-xchallenge-063/arne-sommer/perl/ch-2.pl43
-rwxr-xr-xchallenge-063/arne-sommer/perl/lawo-perl25
-rwxr-xr-xchallenge-063/arne-sommer/perl/rostr-perl43
-rwxr-xr-xchallenge-063/arne-sommer/raku/ch-1.p621
-rwxr-xr-xchallenge-063/arne-sommer/raku/ch-2.p636
-rwxr-xr-xchallenge-063/arne-sommer/raku/lawo21
-rwxr-xr-xchallenge-063/arne-sommer/raku/rostr36
9 files changed, 251 insertions, 0 deletions
diff --git a/challenge-063/arne-sommer/blog.txt b/challenge-063/arne-sommer/blog.txt
new file mode 100644
index 0000000000..e7276aadc2
--- /dev/null
+++ b/challenge-063/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/last-rotate.html
diff --git a/challenge-063/arne-sommer/perl/ch-1.pl b/challenge-063/arne-sommer/perl/ch-1.pl
new file mode 100755
index 0000000000..4e40edc8eb
--- /dev/null
+++ b/challenge-063/arne-sommer/perl/ch-1.pl
@@ -0,0 +1,25 @@
+#! /usr/bin/env perl
+
+use feature 'say';
+use feature 'signatures';
+no warnings qw(experimental::signatures);
+
+my $verbose = (@ARGV && @ARGV[0] eq "--verbose");
+
+sub last_word ($string, $regex)
+{
+ say ": String: $string" if $verbose;
+
+ for my $word (reverse split(/\s/, $string))
+ {
+ say ": Word: $word (regex: $regex)" if $verbose;
+
+ return $word if $word =~ $regex;
+ }
+ return;
+}
+
+say last_word(' hello world', qr/[ea]l/); # 'hello'
+say last_word("Don't match too much, Chet!", qr/ch.t/i); # 'Chet!'
+say last_word("spaces in regexp won't match", qr/in re/); # undef
+say last_word( join(' ', 1..1e6), qr/^(3.*?){3}/); # '399933'
diff --git a/challenge-063/arne-sommer/perl/ch-2.pl b/challenge-063/arne-sommer/perl/ch-2.pl
new file mode 100755
index 0000000000..991fb08255
--- /dev/null
+++ b/challenge-063/arne-sommer/perl/ch-2.pl
@@ -0,0 +1,43 @@
+#! /usr/bin/env perl
+
+use feature 'say';
+
+use Getopt::Long;
+
+my $verbose;
+
+GetOptions ("verbose" => \$verbose);
+
+my $string = @ARGV[0] || 'xyxx';
+
+die "Illegal input string" unless $string =~ /^[xy]+$/;
+
+my $length = length($string);
+my $current = $string;
+
+my $count = 0;
+
+while (1)
+{
+ $count++;
+
+ my $rotate = $count % $length;
+
+ if ($rotate)
+ {
+ my $a = substr($current, $rotate);
+ my $b = substr($current, 0, $rotate);
+
+ $current = $a . $b;
+
+ say ": Rotation $count: $current (moved '$b' to the end)" if $verbose;
+ }
+ elsif ($verbose)
+ {
+ say ": Rotation $count: $current (moved nothing)";
+ }
+
+ last if $current eq $string;
+}
+
+say "$count Rotations";
diff --git a/challenge-063/arne-sommer/perl/lawo-perl b/challenge-063/arne-sommer/perl/lawo-perl
new file mode 100755
index 0000000000..4e40edc8eb
--- /dev/null
+++ b/challenge-063/arne-sommer/perl/lawo-perl
@@ -0,0 +1,25 @@
+#! /usr/bin/env perl
+
+use feature 'say';
+use feature 'signatures';
+no warnings qw(experimental::signatures);
+
+my $verbose = (@ARGV && @ARGV[0] eq "--verbose");
+
+sub last_word ($string, $regex)
+{
+ say ": String: $string" if $verbose;
+
+ for my $word (reverse split(/\s/, $string))
+ {
+ say ": Word: $word (regex: $regex)" if $verbose;
+
+ return $word if $word =~ $regex;
+ }
+ return;
+}
+
+say last_word(' hello world', qr/[ea]l/); # 'hello'
+say last_word("Don't match too much, Chet!", qr/ch.t/i); # 'Chet!'
+say last_word("spaces in regexp won't match", qr/in re/); # undef
+say last_word( join(' ', 1..1e6), qr/^(3.*?){3}/); # '399933'
diff --git a/challenge-063/arne-sommer/perl/rostr-perl b/challenge-063/arne-sommer/perl/rostr-perl
new file mode 100755
index 0000000000..991fb08255
--- /dev/null
+++ b/challenge-063/arne-sommer/perl/rostr-perl
@@ -0,0 +1,43 @@
+#! /usr/bin/env perl
+
+use feature 'say';
+
+use Getopt::Long;
+
+my $verbose;
+
+GetOptions ("verbose" => \$verbose);
+
+my $string = @ARGV[0] || 'xyxx';
+
+die "Illegal input string" unless $string =~ /^[xy]+$/;
+
+my $length = length($string);
+my $current = $string;
+
+my $count = 0;
+
+while (1)
+{
+ $count++;
+
+ my $rotate = $count % $length;
+
+ if ($rotate)
+ {
+ my $a = substr($current, $rotate);
+ my $b = substr($current, 0, $rotate);
+
+ $current = $a . $b;
+
+ say ": Rotation $count: $current (moved '$b' to the end)" if $verbose;
+ }
+ elsif ($verbose)
+ {
+ say ": Rotation $count: $current (moved nothing)";
+ }
+
+ last if $current eq $string;
+}
+
+say "$count Rotations";
diff --git a/challenge-063/arne-sommer/raku/ch-1.p6 b/challenge-063/arne-sommer/raku/ch-1.p6
new file mode 100755
index 0000000000..064388d9ef
--- /dev/null
+++ b/challenge-063/arne-sommer/raku/ch-1.p6
@@ -0,0 +1,21 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (:$verbose);
+
+sub last_word ($string, Regex $regex)
+{
+ say ": String: $string" if $verbose;
+
+ for $string.split(/\s/).reverse -> $word
+ {
+ say ": Word: $word (regex: { $regex.gist })" if $verbose;
+
+ return $word if $word ~~ $regex;
+ }
+ return;
+}
+
+say last_word(' hello world', rx/<[ea]>l/); # 'hello'
+say last_word("Don't match too much, Chet!", rx:i/ch.t/); # 'Chet!'
+say last_word("spaces in regexp won't match", rx:s/in re/); # undef
+say last_word( join(' ', 1..1e6), rx/^(3.*?) ** 3 /); # '399933'
diff --git a/challenge-063/arne-sommer/raku/ch-2.p6 b/challenge-063/arne-sommer/raku/ch-2.p6
new file mode 100755
index 0000000000..1e81450e5e
--- /dev/null
+++ b/challenge-063/arne-sommer/raku/ch-2.p6
@@ -0,0 +1,36 @@
+#! /usr/bin/env raku
+
+subset XY where /^<[xy]>+$/;
+
+sub MAIN (XY $string = 'xyxx', :$verbose)
+{
+ my $length = $string.chars;
+ my $current = $string;
+
+ my $count = 0;
+
+ loop
+ {
+ $count++;
+
+ my $rotate = $count % $length;
+
+ if $rotate
+ {
+ my $a = $current.substr($rotate);
+ my $b = $current.substr(0, $rotate);
+
+ $current = $a ~ $b;
+
+ say ": Rotation $count: $current (moved '$b' to the end)" if $verbose;
+ }
+ elsif $verbose
+ {
+ say ": Rotation $count: $current (moved nothing)";
+ }
+
+ last if $current eq $string;
+ }
+
+ say "$count Rotations";
+}
diff --git a/challenge-063/arne-sommer/raku/lawo b/challenge-063/arne-sommer/raku/lawo
new file mode 100755
index 0000000000..064388d9ef
--- /dev/null
+++ b/challenge-063/arne-sommer/raku/lawo
@@ -0,0 +1,21 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (:$verbose);
+
+sub last_word ($string, Regex $regex)
+{
+ say ": String: $string" if $verbose;
+
+ for $string.split(/\s/).reverse -> $word
+ {
+ say ": Word: $word (regex: { $regex.gist })" if $verbose;
+
+ return $word if $word ~~ $regex;
+ }
+ return;
+}
+
+say last_word(' hello world', rx/<[ea]>l/); # 'hello'
+say last_word("Don't match too much, Chet!", rx:i/ch.t/); # 'Chet!'
+say last_word("spaces in regexp won't match", rx:s/in re/); # undef
+say last_word( join(' ', 1..1e6), rx/^(3.*?) ** 3 /); # '399933'
diff --git a/challenge-063/arne-sommer/raku/rostr b/challenge-063/arne-sommer/raku/rostr
new file mode 100755
index 0000000000..1e81450e5e
--- /dev/null
+++ b/challenge-063/arne-sommer/raku/rostr
@@ -0,0 +1,36 @@
+#! /usr/bin/env raku
+
+subset XY where /^<[xy]>+$/;
+
+sub MAIN (XY $string = 'xyxx', :$verbose)
+{
+ my $length = $string.chars;
+ my $current = $string;
+
+ my $count = 0;
+
+ loop
+ {
+ $count++;
+
+ my $rotate = $count % $length;
+
+ if $rotate
+ {
+ my $a = $current.substr($rotate);
+ my $b = $current.substr(0, $rotate);
+
+ $current = $a ~ $b;
+
+ say ": Rotation $count: $current (moved '$b' to the end)" if $verbose;
+ }
+ elsif $verbose
+ {
+ say ": Rotation $count: $current (moved nothing)";
+ }
+
+ last if $current eq $string;
+ }
+
+ say "$count Rotations";
+}