From a19500b66852c662c458cbac616e7c64cf6c8a05 Mon Sep 17 00:00:00 2001 From: "Gustavo L. de M. Chaves" Date: Sun, 21 Apr 2019 16:24:08 -0300 Subject: 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. --- challenge-004/gustavo-chaves/perl5/ch-2b.pl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 challenge-004/gustavo-chaves/perl5/ch-2b.pl 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 [...]\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; +} <>; -- cgit