aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-099/sgreen/README.md4
-rw-r--r--challenge-099/sgreen/blog.txt1
-rwxr-xr-xchallenge-099/sgreen/perl/ch-1.pl27
-rwxr-xr-xchallenge-099/sgreen/perl/ch-2.pl39
4 files changed, 69 insertions, 2 deletions
diff --git a/challenge-099/sgreen/README.md b/challenge-099/sgreen/README.md
index 78a02d34a3..5aabfe64f0 100644
--- a/challenge-099/sgreen/README.md
+++ b/challenge-099/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 098
+# The Weekly Challenge 099
-Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-098-235h)
+Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-099-ncp)
diff --git a/challenge-099/sgreen/blog.txt b/challenge-099/sgreen/blog.txt
new file mode 100644
index 0000000000..c6de69cdeb
--- /dev/null
+++ b/challenge-099/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-099-ncp
diff --git a/challenge-099/sgreen/perl/ch-1.pl b/challenge-099/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..5d87a192ba
--- /dev/null
+++ b/challenge-099/sgreen/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+sub main {
+ my ( $string, $pattern ) = @_;
+
+ # Sanity check
+ die "You must enter a string\n" unless defined $string;
+ die "You must enter a pattern\n" unless defined $pattern;
+
+ # Escape regexp meta characters, except ? and *
+ $pattern =~ s/([\{\}\[\]\(\)\^\$\.\|\+\\])/\\$1/g;
+
+ # Replace ? and *
+ $pattern =~ s/\?/./g;
+ $pattern =~ s/\*/.+/g;
+
+ # Ensure entire match
+ $pattern = "^$pattern\$";
+
+ say $string =~ $pattern ? 1 : 0;
+}
+
+main(@ARGV);
diff --git a/challenge-099/sgreen/perl/ch-2.pl b/challenge-099/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..c4848785ce
--- /dev/null
+++ b/challenge-099/sgreen/perl/ch-2.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+sub _find_string {
+ my ( $string, $substring ) = @_;
+ my ( $first, $rest ) = ( $substring =~ /^(.)(.*)$/ );
+ my $matches = 0;
+
+ foreach my $i ( 0 .. length($string) ) {
+ if ( substr( $string, $i, 1 ) eq $first ) {
+ if ( length($rest) ) {
+ # Call the function again with the remaining characters
+ $matches += _find_string( substr( $string, $i + 1 ), $rest );
+ }
+ else {
+ # We have found a match!
+ $matches++;
+ }
+ }
+ }
+
+ return $matches;
+}
+
+sub main {
+ my ( $string, $substring ) = @_;
+
+ # Sanity check
+ die "You must enter a string\n" unless defined $string;
+ die "You must enter a sub-string\n" unless defined $substring;
+
+ say _find_string( $string, $substring );
+}
+
+main(@ARGV);
+