aboutsummaryrefslogtreecommitdiff
path: root/challenge-099
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-099')
-rw-r--r--challenge-099/pete-houston/perl/ch-1.pl63
-rw-r--r--challenge-099/pete-houston/perl/ch-2.pl38
2 files changed, 101 insertions, 0 deletions
diff --git a/challenge-099/pete-houston/perl/ch-1.pl b/challenge-099/pete-houston/perl/ch-1.pl
new file mode 100644
index 0000000000..4af2d33b9d
--- /dev/null
+++ b/challenge-099/pete-houston/perl/ch-1.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 9901.pl
+#
+# USAGE: ./9901.pl [ STRING GLOB ]
+#
+# DESCRIPTION: Re-invent globbing but with just * and ?
+# Print 1 if GLOB matches STRING, 0 otherwise
+#
+# OPTIONS: With no arguments, runs the test suite
+# REQUIREMENTS: Test::More
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 08/02/21
+#===============================================================================
+
+use strict;
+use warnings;
+use Test::More;
+
+my ($str, $pat) = @ARGV;
+
+if (defined $str) {
+ print valid_glob($str, $pat) . "\n";
+ exit;
+}
+
+# Tests
+my @good = (
+ { str => 'abcde', pat => 'a*e' },
+ { str => 'abcde', pat => 'a*c?e' },
+ { str => 'abcde', pat => '?b*e' },
+);
+
+my @bad = (
+ { str => 'abcde', pat => 'a*d' },
+ { str => 'abcde', pat => '?b*d' },
+ { str => 'abcde', pat => '*b?e' },
+ { str => 'abcde', pat => 'a.c?e' },
+);
+
+plan tests => @good + @bad;
+
+for my $t (@good) {
+ ok valid_glob ($t->{str}, $t->{pat}),
+ "Matched $t->{str} against $t->{pat}";
+}
+
+for my $t (@bad) {
+ ok !valid_glob ($t->{str}, $t->{pat}),
+ "No match for $t->{str} against $t->{pat}";
+}
+
+sub valid_glob {
+ my ($str, $pat) = @_;
+ $pat = quotemeta $pat;
+ $pat =~ s/\\\*/.*/g;
+ $pat =~ s/\\\?/./g;
+ $pat = qr/^$pat$/;
+ return $str =~ $pat;
+}
diff --git a/challenge-099/pete-houston/perl/ch-2.pl b/challenge-099/pete-houston/perl/ch-2.pl
new file mode 100644
index 0000000000..f9024b6ae6
--- /dev/null
+++ b/challenge-099/pete-houston/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 9902.pl
+#
+# USAGE: ./9902.pl STRING SUBSTRING
+#
+# DESCRIPTION: Display count of unique subsequences matching chars of
+# SUBSTRING in order within STRING
+#
+# REQUIREMENTS: Memoize
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 08/02/21
+#===============================================================================
+
+use strict;
+use warnings;
+use Memoize;
+
+memoize ('count_seqs');
+print count_seqs(@ARGV) . "\n";
+
+sub count_seqs {
+ my ($str, $sub) = @_;
+ my $tot = 0;
+ my $pos = index ($str, substr ($sub, 0, 1));
+ my $len = 1 == length $sub;
+
+ while ($pos > -1) {
+ $tot += $len ?
+ 1 :
+ count_seqs (substr ($str, $pos + 1), substr ($sub, 1));
+ $pos = index ($str, substr ($sub, 0, 1), $pos + 1);
+ }
+ return $tot;
+}