diff options
| author | Gustavo L. de M. Chaves <gustavo@cpqd.com.br> | 2019-04-17 22:03:04 -0300 |
|---|---|---|
| committer | Gustavo L. de M. Chaves <gustavo@cpqd.com.br> | 2019-04-17 22:03:04 -0300 |
| commit | 0c48a02a29732da2aa1aef4ff1dd124665f3cd62 (patch) | |
| tree | 534eebe62311a67c2c2a0584833d755820c7d43f | |
| parent | 3a064726397cf74b0513e71c237b2321c7cebd67 (diff) | |
| download | perlweeklychallenge-club-0c48a02a29732da2aa1aef4ff1dd124665f3cd62.tar.gz perlweeklychallenge-club-0c48a02a29732da2aa1aef4ff1dd124665f3cd62.tar.bz2 perlweeklychallenge-club-0c48a02a29732da2aa1aef4ff1dd124665f3cd62.zip | |
Add Perl 5 solutions to challenge 004
| -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; |
