aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-04-21 20:35:59 +0100
committerGitHub <noreply@github.com>2019-04-21 20:35:59 +0100
commite4225ce914623b98969aa2f8c9e3e0a4647c14d8 (patch)
treed41ddb7ec28bfe837677f3850b873b0b68179283
parent6497301a623987e417a3bc047e36fe503adbf518 (diff)
parenta19500b66852c662c458cbac616e7c64cf6c8a05 (diff)
downloadperlweeklychallenge-club-e4225ce914623b98969aa2f8c9e3e0a4647c14d8.tar.gz
perlweeklychallenge-club-e4225ce914623b98969aa2f8c9e3e0a4647c14d8.tar.bz2
perlweeklychallenge-club-e4225ce914623b98969aa2f8c9e3e0a4647c14d8.zip
Merge pull request #83 from gnustavo/gustavo-chaves-004b
Another solution to challenge 004
-rwxr-xr-xchallenge-004/gustavo-chaves/perl5/ch-2b.pl27
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;
+} <>;