aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-09 23:03:12 +0000
committerGitHub <noreply@github.com>2021-02-09 23:03:12 +0000
commit63e61d7d76765318009d27be48a6ca3acae67c4c (patch)
treef8eb41b6edaf24d1d58fc6f0377873cbb3b41170
parent4d931b3e22475a5b297cccd8c3f0d51f38d17bb9 (diff)
parentf9eb9c82b88e04c28a9323021c8e02ff968cdfe9 (diff)
downloadperlweeklychallenge-club-63e61d7d76765318009d27be48a6ca3acae67c4c.tar.gz
perlweeklychallenge-club-63e61d7d76765318009d27be48a6ca3acae67c4c.tar.bz2
perlweeklychallenge-club-63e61d7d76765318009d27be48a6ca3acae67c4c.zip
Merge pull request #3479 from simongreen-net/swg-099
sgreen solution to challenge 099
-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);
+