diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-10 07:46:07 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-10 07:46:07 +0000 |
| commit | efaf601ffa49dfc75fa01e8ffac7601776ffdaaf (patch) | |
| tree | ead60c1ddfccf8e8af188eefa7dd73fa166e67d6 | |
| parent | 57b0889a96a840210e23aea7bbcdb829dd4f2ca2 (diff) | |
| parent | f73ff890f4e654931043943ea3b83b969279ac02 (diff) | |
| download | perlweeklychallenge-club-efaf601ffa49dfc75fa01e8ffac7601776ffdaaf.tar.gz perlweeklychallenge-club-efaf601ffa49dfc75fa01e8ffac7601776ffdaaf.tar.bz2 perlweeklychallenge-club-efaf601ffa49dfc75fa01e8ffac7601776ffdaaf.zip | |
Merge pull request #3493 from jacoby/master
9 and 90 challenges...
| -rw-r--r-- | challenge-099/dave-jacoby/perl/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-099/dave-jacoby/perl/ch-2.pl | 67 |
2 files changed, 114 insertions, 0 deletions
diff --git a/challenge-099/dave-jacoby/perl/ch-1.pl b/challenge-099/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..afc18c48b7 --- /dev/null +++ b/challenge-099/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use utf8; +use feature qw{say signatures }; +no warnings qw{experimental}; + +my @arr; +push @arr, [ 'abcde', 'a*e' ]; +push @arr, [ 'abcde', 'a*d' ]; +push @arr, [ 'abcde', '?b*d' ]; +push @arr, [ 'abcde', 'a?c*e' ]; + +for my $n (@arr) { + my $p = pattern_match( $n->@* ); + say $n->[0]; + say $n->[1]; + say $p; +} + +sub pattern_match ( $S, $P ) { + my $pattern = $P; + + # this was my first pass on this part + # $pattern =~ s/\*/.*/g; + # $pattern =~ s/\?/.?/g; + # + # a comment from Jonas Berlin (xkr47) + # made me reconsider. By the rules of the + # task, ? is ONE character and * is MANY + # CHARACTERS, but in Perl's regular + # expressions, .? is ZERO OR ONE CHARACTER + # and .* is ZERO OR MORE CHARACTERS + # to get ONE OR MORE CHARACTERS, we instead + # use .+ and to get ONE CHARACTER, we use . + # + # but of course, we need to match the WHOLE + # string, so we're matching the beginning (^) + # and the end ($) + + $pattern =~ s/\*/.+/g; + $pattern =~ s/\?/./g; + $pattern = qq{^$pattern\$}; + return $S =~ /$pattern/mix ? 1 : 0; +} + diff --git a/challenge-099/dave-jacoby/perl/ch-2.pl b/challenge-099/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..aabda6b46c --- /dev/null +++ b/challenge-099/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use utf8; +use feature qw{say state signatures }; +no warnings qw{experimental}; + +use List::Util qw{ uniq }; + +my @arr; +push @arr, [ 'littleit', 'lit' ]; +push @arr, [ 'london', 'lon' ]; +push @arr, [ 'abracadabra', 'abra' ]; + +for my $n (@arr) { + my @p = unique_sub( $n->@* ); + say ' '; + for my $o (@p) { + state $c = 0; + $c++; + my $string = display_sub( $n->[0], $o ); + say qq{ $c: $string }; + + } +} + +sub unique_sub ( $S, $T, $p = 0, $q = 0, $done = undef ) { + if ( $p > length $S ) { return } + $done //= []; + my @output; + my $l1 = substr $S, $p, 1; + my $l2 = substr $T, $q, 1; + my $key = join '.', $done->@*; + + if ( $q == length $T ) { + push @output, $key; + } + my $copy->@* = $done->@*; + push @output, unique_sub( $S, $T, $p + 1, $q, $copy ); + if ( $l1 eq $l2 ) { + push $copy->@*, $p; + push @output, unique_sub( $S, $T, $p + 1, $q + 1, $copy ); + } + return uniq sort @output; +} + +sub display_sub ( $string, $key ) { + my @key = split /\D/, $key; + my %key = map { $_ => 1 } @key; + my $state = 0; + my $output; + + for my $i ( 0 .. length $string ) { + my $l = substr( $string, $i, 1 ); + my $k = $key{$i} || 0; + + $output .= ' [' if $state == 0 && $k == 1; + $output .= '] ' if $state == 1 && $k == 0; + $output .= $l; + $state = $k; + } + + $output .= '] ' if $state == 1; + $output =~ s/^\s+//mix; + return $output; +} |
