aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-099/gustavo-chaves/perl/ch-1.pl42
-rwxr-xr-xchallenge-099/gustavo-chaves/perl/ch-2.pl28
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);