From 77565c813cfa98e9e8375474f624b3a5d9e4052f Mon Sep 17 00:00:00 2001 From: RibTips Date: Fri, 12 May 2023 00:04:55 -0400 Subject: Challenge 216 from RibTips --- challenge-216/ribtips/perl/ch-1.pl | 61 +++++++++++++++++++++++++++++++++ challenge-216/ribtips/perl/ch-2.pl | 70 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 challenge-216/ribtips/perl/ch-1.pl create mode 100644 challenge-216/ribtips/perl/ch-2.pl 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 0) { + $sticker_count++; + if (scalar(@{$sticker_hash{$word}{'needed_letters'}}) > length($word)) { + $sticker_count++; + } + } + } + return $sticker_count; +} -- cgit