diff options
| -rwxr-xr-x | challenge-004/gustavo-chaves/perl5/ch-1.pl | 3 | ||||
| -rwxr-xr-x | challenge-004/gustavo-chaves/perl5/ch-2.pl | 38 |
2 files changed, 41 insertions, 0 deletions
diff --git a/challenge-004/gustavo-chaves/perl5/ch-1.pl b/challenge-004/gustavo-chaves/perl5/ch-1.pl new file mode 100755 index 0000000000..0e38ac4e6e --- /dev/null +++ b/challenge-004/gustavo-chaves/perl5/ch-1.pl @@ -0,0 +1,3 @@ +#!/usr/bin/perl +use bignum 'bpi'; +print bpi(49); diff --git a/challenge-004/gustavo-chaves/perl5/ch-2.pl b/challenge-004/gustavo-chaves/perl5/ch-2.pl new file mode 100755 index 0000000000..4afacefaf0 --- /dev/null +++ b/challenge-004/gustavo-chaves/perl5/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +# You are given a file containing a list of words (case insensitive 1 word per +# line) and a list of letters. Print each word from the file than can be made +# using only letters from the list. You can use each letter only once (though +# there can be duplicates and you can use each of them once), you don’t have to +# use all the letters. (Disclaimer: The challenge was proposed by Scimon +# Proctor) + +use 5.026; +use strict; +use autodie; +use warnings; + +die "usage: $0 <file> <letters>\n" unless @ARGV == 2; +my $file = shift; +my $letters = shift; + +# Construct a hash mapping each letter to its count number +my %letters; +++$letters{$_} foreach split //, lc $letters; + +# Return true if $word matches %letters +sub match { + my ($word) = @_; + my %letters = %letters; + foreach my $char (split //, lc $word) { + return 0 unless exists $letters{$char} && $letters{$char}-- > 0; + } + return 1; +} + +open my $fh, '<', $file; +while (<$fh>) { + chomp; + say $_ if match($_); +} +close $fh; |
