diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2025-04-24 16:49:56 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2025-04-24 16:49:56 -0400 |
| commit | 5a74a0da44b9c5530066988c5bb180cb82f6e528 (patch) | |
| tree | d5110a6ebdb17fc4a09c2ccd32e93fabe6937558 | |
| parent | b63d53f9a626169c28883c080a9275cef3b522b8 (diff) | |
| download | perlweeklychallenge-club-5a74a0da44b9c5530066988c5bb180cb82f6e528.tar.gz perlweeklychallenge-club-5a74a0da44b9c5530066988c5bb180cb82f6e528.tar.bz2 perlweeklychallenge-club-5a74a0da44b9c5530066988c5bb180cb82f6e528.zip | |
DAJ 318 blogged
| -rw-r--r-- | blog.txt | 1 | ||||
| -rw-r--r-- | challenge-318/dave-jacoby/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-318/dave-jacoby/perl/ch-2.pl | 51 |
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'; +} |
