aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-12 11:18:50 +0100
committerGitHub <noreply@github.com>2023-05-12 11:18:50 +0100
commit73518e3e5b4378521b1a8c641663f54338ba5962 (patch)
tree9f4362e59f4fd921e44963043bf62c05a3a097d6
parent7d59a3302f4d86c29c39e1c8e95e2f79cd1c1411 (diff)
parent77565c813cfa98e9e8375474f624b3a5d9e4052f (diff)
downloadperlweeklychallenge-club-73518e3e5b4378521b1a8c641663f54338ba5962.tar.gz
perlweeklychallenge-club-73518e3e5b4378521b1a8c641663f54338ba5962.tar.bz2
perlweeklychallenge-club-73518e3e5b4378521b1a8c641663f54338ba5962.zip
Merge pull request #8060 from ribtips/branch-for-challenge-216
Challenge 216 from RibTips
-rw-r--r--challenge-216/ribtips/perl/ch-1.pl61
-rw-r--r--challenge-216/ribtips/perl/ch-2.pl70
2 files changed, 131 insertions, 0 deletions
diff --git a/challenge-216/ribtips/perl/ch-1.pl b/challenge-216/ribtips/perl/ch-1.pl
new file mode 100644
index 0000000000..144dbbff2d
--- /dev/null
+++ b/challenge-216/ribtips/perl/ch-1.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+&main;
+
+sub main {
+ print "Test 1: ";
+ my @words=('abc', 'abcd', 'bcd');
+ my $reg = 'AB1 2CD';
+ my $matches=&find_matches(\@words,$reg);
+ &print_output($matches);
+ print "Test 2: ";
+ @words=('job', 'james', 'bjorg');
+ $reg = '007 JB';
+ $matches=&find_matches(\@words,$reg);
+ &print_output($matches);
+ @words=('crack', 'road', 'rac');
+ $reg = 'C7 RA2';
+ $matches=&find_matches(\@words,$reg);
+ print "Test 3: ";
+ &print_output($matches);
+}
+
+sub print_output {
+ my $matches_array=shift;
+ my $matches=join("', '",@$matches_array);
+ print "('$matches')\n";
+}
+
+sub find_matches {
+ my $words=shift;
+ my $reg=shift;
+ my @results=();
+ foreach my $word (@$words) {
+ my $result=&compare_words($reg,$word);
+ if ($result == 1) {
+ push @results,$word;
+ }
+ }
+ return \@results;
+}
+
+sub compare_words {
+ my $reg=shift;
+ my $word=shift;
+ $reg=lc($reg);
+ my @reg=split//,$reg;
+ foreach my $letter (@reg) {
+ if ($letter =~ m/[a-z]/) {
+ if ($word =~ m/$letter/) {
+ #good
+ }
+ else {
+ return 0;
+ }
+ }
+ }
+ return 1;
+}
diff --git a/challenge-216/ribtips/perl/ch-2.pl b/challenge-216/ribtips/perl/ch-2.pl
new file mode 100644
index 0000000000..6b9ff38f14
--- /dev/null
+++ b/challenge-216/ribtips/perl/ch-2.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Data::Dumper;
+
+&main;
+
+sub main {
+ my @stickers=('perl','raku','python');
+ my $word='peon';
+ my $count=&how_many(\@stickers,$word);
+ print "Output: $count\n";
+
+ @stickers=('love','hate','angry');
+ $word='goat';
+ $count=&how_many(\@stickers,$word);
+ print "Output: $count\n";
+
+ @stickers=('come','nation','delta');
+ $word='accomodation';
+ $count=&how_many(\@stickers,$word);
+ print "Output: $count\n";
+
+ @stickers=('come','country','delta');
+ $word='accomodation';
+ $count=&how_many(\@stickers,$word);
+ print "Output: $count\n";
+}
+
+sub how_many {
+ my $stickers=shift;
+ my $word=shift;
+
+ my %sticker_hash=();
+
+ foreach my $sticker (@$stickers) {
+ foreach my $letter (split//,$sticker) {
+ push @{$sticker_hash{$sticker}{'letters'}},$letter;
+ }
+ }
+
+ my @letters=split//,$word;
+
+ for(my $i=0;$i<scalar(@letters);$i++) {
+ my $found=0;
+ foreach my $sticker (@$stickers) {
+ if (grep(/$letters[$i]/,@{$sticker_hash{$sticker}{'letters'}})) {
+ push @{$sticker_hash{$sticker}{'needed_letters'}},$letters[$i];
+ $found=1;
+ last;
+ }
+ }
+ if ($found == 0) {
+ return 0;
+ }
+ }
+
+
+ my $sticker_count=0;
+ foreach my $word (%sticker_hash) {
+ if (exists($sticker_hash{$word}{'needed_letters'}) and scalar(@{$sticker_hash{$word}{'needed_letters'}}) > 0) {
+ $sticker_count++;
+ if (scalar(@{$sticker_hash{$word}{'needed_letters'}}) > length($word)) {
+ $sticker_count++;
+ }
+ }
+ }
+ return $sticker_count;
+}