diff options
| author | RibTips <rib@tips.com> | 2023-05-12 00:04:55 -0400 |
|---|---|---|
| committer | RibTips <rib@tips.com> | 2023-05-12 00:04:55 -0400 |
| commit | 77565c813cfa98e9e8375474f624b3a5d9e4052f (patch) | |
| tree | edd02ed9584d349799a7d48f87ef8f781901a762 | |
| parent | 488c528fc8725f3e183eae4073709f95edca33fb (diff) | |
| download | perlweeklychallenge-club-77565c813cfa98e9e8375474f624b3a5d9e4052f.tar.gz perlweeklychallenge-club-77565c813cfa98e9e8375474f624b3a5d9e4052f.tar.bz2 perlweeklychallenge-club-77565c813cfa98e9e8375474f624b3a5d9e4052f.zip | |
Challenge 216 from RibTips
| -rw-r--r-- | challenge-216/ribtips/perl/ch-1.pl | 61 | ||||
| -rw-r--r-- | challenge-216/ribtips/perl/ch-2.pl | 70 |
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; +} |
