diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-02-14 18:26:20 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-02-14 18:26:20 +0000 |
| commit | 9b3200fe6ced2d6175feeba7ed832cd2477a1e45 (patch) | |
| tree | cd1674b413d60371fccf6e7c008a9cbe7819d777 /challenge-099 | |
| parent | ab1cb4bf3b1f38397d7baa50dcac583de1616637 (diff) | |
| download | perlweeklychallenge-club-9b3200fe6ced2d6175feeba7ed832cd2477a1e45.tar.gz perlweeklychallenge-club-9b3200fe6ced2d6175feeba7ed832cd2477a1e45.tar.bz2 perlweeklychallenge-club-9b3200fe6ced2d6175feeba7ed832cd2477a1e45.zip | |
- Added solution by Cristina Heredia.
Diffstat (limited to 'challenge-099')
| -rw-r--r-- | challenge-099/cristian-heredia/perl/ch-1.pl | 64 | ||||
| -rw-r--r-- | challenge-099/cristian-heredia/python/ch-1.py | 39 |
2 files changed, 103 insertions, 0 deletions
diff --git a/challenge-099/cristian-heredia/perl/ch-1.pl b/challenge-099/cristian-heredia/perl/ch-1.pl new file mode 100644 index 0000000000..237d145780 --- /dev/null +++ b/challenge-099/cristian-heredia/perl/ch-1.pl @@ -0,0 +1,64 @@ +=begin + +TASK #1 › Pattern Match +Submitted by: Mohammad S Anwar +You are given a string $S and a pattern $P. + +Write a script to check if given pattern validate the entire string. Print 1 if pass otherwise 0. + +The patterns can also have the following characters: + +? - Match any single character. +* - Match any sequence of characters. + Example 1: + Input: $S = "abcde" $P = "a*e" + Output: 1 + Example 2: + Input: $S = "abcde" $P = "a*d" + Output: 0 + Example 3: + Input: $S = "abcde" $P = "?b*d" + Output: 0 + Example 4: + Input: $S = "abcde" $P = "a*c?e" + Output: 1 + +=end +=cut + + +use strict; +use warnings; +use Data::Dumper; + +#Input +my $S = "abcde"; +my $P = "a*e"; + + +my @pattern = split //, $P; + +replaceCharacter(); + +sub replaceCharacter { + foreach ( @pattern ) { + if ($_ eq '?') { + $_ = '.'; + } + elsif ($_ eq '*') { + $_ = '.+'; + } + } + my $redex = join '', @pattern; + printResult($redex); +} + +sub printResult{ + my $redex = shift; + if ($S =~ /^$redex$/){ + print "1\n"; + } + else { + print "0\n"; + } +} diff --git a/challenge-099/cristian-heredia/python/ch-1.py b/challenge-099/cristian-heredia/python/ch-1.py new file mode 100644 index 0000000000..c2e470bc11 --- /dev/null +++ b/challenge-099/cristian-heredia/python/ch-1.py @@ -0,0 +1,39 @@ +''' + +TASK #1 › Pattern Match +Submitted by: Mohammad S Anwar +You are given a string $S and a pattern $P. + +Write a script to check if given pattern validate the entire string. Print 1 if pass otherwise 0. + +The patterns can also have the following characters: + +? - Match any single character. +* - Match any sequence of characters. + Example 1: + Input: $S = "abcde" $P = "a*e" + Output: 1 + Example 2: + Input: $S = "abcde" $P = "a*d" + Output: 0 + Example 3: + Input: $S = "abcde" $P = "?b*d" + Output: 0 + Example 4: + Input: $S = "abcde" $P = "a*c?e" + Output: 1 + +''' +import re + +#Input +S = "abcde"; +P = "a*c?e"; + +pattern = P.replace('*' , '.+').replace('?' , '.') +pattern = "^" + pattern + "$" + +if re.match(pattern, S): + print (1) +else: + print (0) |
