diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-10 00:16:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-10 00:16:09 +0000 |
| commit | 642cc596a1f730cc8b3f0f6cec85251fb744e90d (patch) | |
| tree | 5778c39cd7a9a689f2b60f76cb2b66ee04493f42 | |
| parent | ee9aa5b41b939ae04f7c0be7f5bb570322ec0a70 (diff) | |
| parent | c10a5192b3a6a07b127e10bee1bab2c51d8a2ede (diff) | |
| download | perlweeklychallenge-club-642cc596a1f730cc8b3f0f6cec85251fb744e90d.tar.gz perlweeklychallenge-club-642cc596a1f730cc8b3f0f6cec85251fb744e90d.tar.bz2 perlweeklychallenge-club-642cc596a1f730cc8b3f0f6cec85251fb744e90d.zip | |
Merge pull request #3492 from gnustavo/099
Add Gustavo Chaves's solutions to challenge 099
| -rwxr-xr-x | challenge-099/gustavo-chaves/perl/ch-1.pl | 42 | ||||
| -rwxr-xr-x | challenge-099/gustavo-chaves/perl/ch-2.pl | 28 |
2 files changed, 70 insertions, 0 deletions
diff --git a/challenge-099/gustavo-chaves/perl/ch-1.pl b/challenge-099/gustavo-chaves/perl/ch-1.pl new file mode 100755 index 0000000000..a11b6d7390 --- /dev/null +++ b/challenge-099/gustavo-chaves/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-099/ +# TASK #1 › Pattern Match + +use 5.030; +use warnings; + +my ($S, $P) = @ARGV; + +say "Pattern: '$P'"; + +say match($P, $S) ? "Matches: " : "Does not match: ", "'$S'"; + +sub match { + my ($pattern, $string) = @_; + + my ($s, $p) = (0, 0); + + CHAR: + while ($p < length($pattern) && $s < length($string)) { + my $c = substr($pattern, $p, 1); + + if ($c eq '?') { + ++$s; + ++$p; + } elsif ($c eq '*') { + my $patterntail = substr($pattern, $p+1); + for (my $i=$s; $i < length($string); ++$i) { + return 1 if match($patterntail, substr($string, $i)); + } + return 0; + } elsif ($c eq substr($string, $s, 1)) { + ++$s; + ++$p; + } else { + return 0; + } + } + + return $p == length($pattern) && $s == length($string); +} diff --git a/challenge-099/gustavo-chaves/perl/ch-2.pl b/challenge-099/gustavo-chaves/perl/ch-2.pl new file mode 100755 index 0000000000..25f36ff228 --- /dev/null +++ b/challenge-099/gustavo-chaves/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-099/ +# TASK #2 › Unique Subsequence + +use 5.030; +use warnings; + +my ($S, $T) = @ARGV; + +sub matches { + my ($s, $t) = @_; + + return 1 if $t == length($T); + + my $matches = 0; + + my $c = substr($T, $t, 1); + foreach my $i ($s .. length($S)-1) { + if ($c eq substr($S, $i, 1)) { + $matches += matches($i+1, $t+1); + } + } + + return $matches; +} + +say matches(0, 0); |
