aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-04-24 23:36:10 +0100
committerGitHub <noreply@github.com>2025-04-24 23:36:10 +0100
commita17b397ae8596e6e5e94ea2f41963460c804e99d (patch)
tree23091910e7f095de3b02e29daf60bfbdfc6235df
parent6e87910f452544204e64d4f79177cd9d12a0d870 (diff)
parent5a74a0da44b9c5530066988c5bb180cb82f6e528 (diff)
downloadperlweeklychallenge-club-a17b397ae8596e6e5e94ea2f41963460c804e99d.tar.gz
perlweeklychallenge-club-a17b397ae8596e6e5e94ea2f41963460c804e99d.tar.bz2
perlweeklychallenge-club-a17b397ae8596e6e5e94ea2f41963460c804e99d.zip
Merge pull request #11928 from jacoby/master
DAJ 318 blogged
-rw-r--r--blog.txt1
-rw-r--r--challenge-318/dave-jacoby/perl/ch-1.pl40
-rw-r--r--challenge-318/dave-jacoby/perl/ch-2.pl51
3 files changed, 92 insertions, 0 deletions
diff --git a/blog.txt b/blog.txt
new file mode 100644
index 0000000000..93e54b29a8
--- /dev/null
+++ b/blog.txt
@@ -0,0 +1 @@
+https://jacoby-lpwk.onrender.com/2025/04/24/your-embrace-and-my-collapse-weekly-challenge-318.html
diff --git a/challenge-318/dave-jacoby/perl/ch-1.pl b/challenge-318/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..0f06d19a0a
--- /dev/null
+++ b/challenge-318/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say state postderef signatures };
+
+my @examples = (qw{ abccccd aaabcddddeefff abcdd });
+
+for my $example (@examples) {
+ my @output = group_position($example);
+ my $output = join ', ', map { qq{"$_"} } @output;
+ say <<"END";
+ Input: \$str = "$example"
+ Output: $output
+END
+}
+
+sub group_position ($example) {
+ return grep { length $_ > 2 } $example =~ m{
+ # (\w) matches any word character
+ # (\w)\1{2,} matches when there's one characters
+ # that is followed by two or more identical
+ # characters. The form is { at least, no more than}
+ # ((\w)\1) would give problems because it's trying to
+ # use the outer match
+ # ((\w)\2) would return first the repeated characters
+ # (like "aa") and then the first match itself ("a")
+ # ((\w)\2{2,}) returns the "aaaaa" and then the "a"
+ #
+ # there is perhaps magic that allows (\w) to be used
+ # within the regex but pass out, but I don't know it.
+ # Therefore the grep.
+ #
+ # also //x allows you to comment your complex regular
+ # expressions.
+
+ ( (\w)\2{2,} )
+ }gmx;
+}
+
diff --git a/challenge-318/dave-jacoby/perl/ch-2.pl b/challenge-318/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..3bff651c26
--- /dev/null
+++ b/challenge-318/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say state postderef signatures };
+
+my @examples = (
+
+ [ [ 3, 2, 1, 4 ], [ 1, 2, 3, 4 ], ],
+ [
+ [ 1, 3, 4 ],
+ [ 4, 1, 3 ],
+ ],
+ [
+ [2],
+ [2],
+ ],
+
+);
+
+for my $example (@examples) {
+ my $source = join ', ', $example->[0]->@*;
+ my $target = join ', ', $example->[1]->@*;
+ my $output = reverse_equals($example);
+ say <<"END";
+ Input: \@source = ($source)
+ \@target = ($target)
+ Output: $output
+END
+}
+
+sub reverse_equals ($obj) {
+ my @source = $obj->[0]->@*;
+ my @target = $obj->[1]->@*;
+ my $s = join ' ', @source;
+ my $t = join ' ', @target;
+ return 'true' if $s eq $t;
+
+ for my $i ( 0 .. $#source ) {
+ for my $j ( $i + 1 .. $#source ) {
+ my @copy = map { $_ } @source;
+ $copy[$i] = $source[$j];
+ $copy[$j] = $source[$i];
+ my $t = join ' ', @target;
+ my $c = join ' ', @copy;
+ return 'true' if $c eq $t;
+ }
+ }
+
+ return 'false';
+}