diff options
| author | Gustavo L. de M. Chaves <gustavo@cpqd.com.br> | 2019-04-21 16:24:08 -0300 |
|---|---|---|
| committer | Gustavo L. de M. Chaves <gustavo@cpqd.com.br> | 2019-04-21 16:24:08 -0300 |
| commit | a19500b66852c662c458cbac616e7c64cf6c8a05 (patch) | |
| tree | d41ddb7ec28bfe837677f3850b873b0b68179283 | |
| parent | 6497301a623987e417a3bc047e36fe503adbf518 (diff) | |
| download | perlweeklychallenge-club-a19500b66852c662c458cbac616e7c64cf6c8a05.tar.gz perlweeklychallenge-club-a19500b66852c662c458cbac616e7c64cf6c8a05.tar.bz2 perlweeklychallenge-club-a19500b66852c662c458cbac616e7c64cf6c8a05.zip | |
Another solution to challenge 004
The new solution is more concise and easier to use, because it uses the
<> filehandle to read words from STDIN or from a sequence of files
provided as arguments.
| -rwxr-xr-x | challenge-004/gustavo-chaves/perl5/ch-2b.pl | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/challenge-004/gustavo-chaves/perl5/ch-2b.pl b/challenge-004/gustavo-chaves/perl5/ch-2b.pl new file mode 100755 index 0000000000..0c265e5e02 --- /dev/null +++ b/challenge-004/gustavo-chaves/perl5/ch-2b.pl @@ -0,0 +1,27 @@ +#!/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; +use List::Util 'all'; + +my $letters = shift or die "usage: $0 <letters> [<wordsfile>...]\n"; + +# Construct a hash mapping each letter to its count number +my %letters; +++$letters{$_} foreach split //, lc $letters; + +say foreach grep { + chomp; + my %letters = %letters; + --$letters{$_} foreach split //, lc $_; + all {$_ >= 0} values %letters; +} <>; |
