diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-05-19 15:14:59 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-05-19 15:14:59 +0100 |
| commit | fc7ef68e2e6290ce18d41de4ea3b78971224e012 (patch) | |
| tree | c9b4984c2d0f39913519f0db1a9dac1a9200e4cb | |
| parent | 22cd1b407333e4a2849bff1c339f206281bfd990 (diff) | |
| download | perlweeklychallenge-club-fc7ef68e2e6290ce18d41de4ea3b78971224e012.tar.gz perlweeklychallenge-club-fc7ef68e2e6290ce18d41de4ea3b78971224e012.tar.bz2 perlweeklychallenge-club-fc7ef68e2e6290ce18d41de4ea3b78971224e012.zip | |
Add Perl solution
| -rw-r--r-- | challenge-216/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-216/paulo-custodio/perl/ch-1.pl | 45 | ||||
| -rw-r--r-- | challenge-216/paulo-custodio/perl/ch-2.pl | 84 | ||||
| -rw-r--r-- | challenge-216/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-216/paulo-custodio/t/test-2.yaml | 20 |
5 files changed, 166 insertions, 0 deletions
diff --git a/challenge-216/paulo-custodio/Makefile b/challenge-216/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-216/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-216/paulo-custodio/perl/ch-1.pl b/challenge-216/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..c4e5d3069d --- /dev/null +++ b/challenge-216/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +# Challenge 216 +# +# Task 1: Registration Number +# Submitted by: Mohammad S Anwar +# +# You are given a list of words and a random registration number. +# +# Write a script to find all the words in the given list that has every letter +# in the given registration number. +# +# Example 1 +# +# Input: @words = ('abc', 'abcd', 'bcd'), $reg = 'AB1 2CD' +# Output: ('abcd') +# +# The only word that matches every alphabets in the given registration number +# is 'abcd'. +# +# Example 2 +# +# Input: @words = ('job', 'james', 'bjorg'), $reg = '007 JB' +# Output: ('job', 'bjorg') +# +# Example 3 +# +# Input: @words = ('crack', 'road', 'rac'), $reg = 'C7 RA2' +# Output: ('crack', 'rac') + +use Modern::Perl; + +sub has_every_letter { + my($word, $reg) = @_; + $reg =~ s/\s+//g; + $reg =~ s/0/o/g; + $reg =~ s/\d+//g; + for my $letter (split //, $word) { + $reg =~ s/$letter//gi; + } + return $reg eq ''; +} + +my($reg, @words) = @ARGV; +say join ", ", grep {has_every_letter($_, $reg)} @words; diff --git a/challenge-216/paulo-custodio/perl/ch-2.pl b/challenge-216/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..7c31c839e1 --- /dev/null +++ b/challenge-216/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,84 @@ +#!/usr/bin/env perl + +# Challenge 216 +# +# Task 2: Word Stickers +# Submitted by: Mohammad S Anwar +# +# You are given a list of word stickers and a target word. +# +# Write a script to find out how many word stickers is needed to make up the +# given target word. +# +# Example 1: +# +# Input: @stickers = ('perl','raku','python'), $word = 'peon' +# Output: 2 +# +# We just need 2 stickers i.e. 'perl' and 'python'. +# 'pe' from 'perl' and +# 'on' from 'python' to get the target word. +# +# Example 2: +# +# Input: @stickers = ('love','hate','angry'), $word = 'goat' +# Output: 3 +# +# We need 3 stickers i.e. 'angry', 'love' and 'hate'. +# 'g' from 'angry' +# 'o' from 'love' and +# 'at' from 'hate' to get the target word. +# +# Example 3: +# +# Input: @stickers = ('come','nation','delta'), $word = 'accommodation' +# Output: 4 +# +# We just need 2 stickers of 'come' and one each of 'nation' & 'delta'. +# 'a' from 'delta' +# 'ccommo' from 2 stickers 'come' +# 'd' from the same sticker 'delta' and +# 'ation' from 'nation' to get the target word. +# +# Example 4: +# +# Input: @stickers = ('come','country','delta'), $word = 'accommodation' +# Output: 0 +# +# as there's no "i" in the inputs. + +use Modern::Perl; + +sub max_match { + my($word, @stickers) = @_; + for my $len (reverse 1 .. length($word)) { + for my $s (0 .. length($word)-$len) { + my $match = substr($word, $s, $len); + + for my $i (0 .. $#stickers) { + if ($stickers[$i] =~ /$match/) { + return ($match, $i); + } + } + } + } + return ("", -1); +} + +sub count_stickers { + my($word, @stickers) = @_; + @stickers = (@stickers) x length($word); + my %used; + while ($word ne '') { + my($match, $sticker) = max_match($word, @stickers); + return 0 if $match eq ""; + $used{$sticker} = 1; + $word =~ s/$match//; + $stickers[$sticker] =~ s/$match//; + } + return scalar keys %used; +} + +say count_stickers(@ARGV); + + diff --git a/challenge-216/paulo-custodio/t/test-1.yaml b/challenge-216/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..dcaf2567aa --- /dev/null +++ b/challenge-216/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: "'AB1 2CD' abc abcd bcd" + input: + output: abcd +- setup: + cleanup: + args: "'007 JB' job james bjorg" + input: + output: job, bjorg +- setup: + cleanup: + args: "'C7 RA2' crack road rac" + input: + output: crack, rac diff --git a/challenge-216/paulo-custodio/t/test-2.yaml b/challenge-216/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..daa51a1a87 --- /dev/null +++ b/challenge-216/paulo-custodio/t/test-2.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: peon perl raku python + input: + output: 2 +- setup: + cleanup: + args: goat love hate angry + input: + output: 3 +- setup: + cleanup: + args: accommodation come nation delta + input: + output: 4 +- setup: + cleanup: + args: accommodation come country delta + input: + output: 0 |
