blob: 47cbb7d32d7f62141dcd99d5c8a733a8998265cf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#!/usr/bin/perl
# https://perlweeklychallenge.org/blog/perl-weekly-challenge-004/
# Challenge #2
# 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.
use strict;
use warnings;
die "Usage: ch-2.pl <words_file> <letters_list>" if scalar(@ARGV) < 2;
my $words_file = $ARGV[0];
my $letters_list = $ARGV[1];
#Make a hash with available letters and quantity
my %letters;
for my $l (split /,/,$letters_list ){
$letters{ lc($l) }++;
}
open(my $fh, "<", $words_file) or die "Could not open words file '$words_file': $!";
while( my $word = <$fh> ) {
chomp $word; #remove new line trailing string
print $word.$/ if is_word_printable( lc $word , %letters );
}
close $fh;
#Return 1 if $word is composed by letters on the hashed list %letters
sub is_word_printable {
my ( $word, %letters ) = @_;
foreach my $l (split //,$word ){
return 0 unless (exists $letters{ $l } && $letters{ $l } > 0);
$letters{ $l }--;
}
return 1;
}
|