From c10a5192b3a6a07b127e10bee1bab2c51d8a2ede Mon Sep 17 00:00:00 2001 From: "Gustavo L. de M. Chaves" Date: Mon, 8 Feb 2021 22:30:11 -0300 Subject: Add Gustavo Chaves's solutions to challenge 099 --- challenge-099/gustavo-chaves/perl/ch-1.pl | 42 +++++++++++++++++++++++++++++++ challenge-099/gustavo-chaves/perl/ch-2.pl | 28 +++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100755 challenge-099/gustavo-chaves/perl/ch-1.pl create mode 100755 challenge-099/gustavo-chaves/perl/ch-2.pl 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); -- cgit