diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-04-19 09:53:54 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-04-19 09:53:54 +0100 |
| commit | 0e36d8938df0e641a9fa1ae48a90930ae6ea7b6f (patch) | |
| tree | 2924812030cbc6e82edf9e5de9ab22282b8b83dc /challenge-004 | |
| parent | 223f566490d33eb6ef01b963b4cae8d99ab7c183 (diff) | |
| download | perlweeklychallenge-club-0e36d8938df0e641a9fa1ae48a90930ae6ea7b6f.tar.gz perlweeklychallenge-club-0e36d8938df0e641a9fa1ae48a90930ae6ea7b6f.tar.bz2 perlweeklychallenge-club-0e36d8938df0e641a9fa1ae48a90930ae6ea7b6f.zip | |
- Added solutions by Alicia Bielsa.
Diffstat (limited to 'challenge-004')
| -rw-r--r-- | challenge-004/alicia-bielsa/perl5/ch-2.pl | 54 | ||||
| -rw-r--r-- | challenge-004/alicia-bielsa/perl5/file.txt (renamed from challenge-004/alicia-bielsa/perl5/ch-2.txt) | 0 |
2 files changed, 27 insertions, 27 deletions
diff --git a/challenge-004/alicia-bielsa/perl5/ch-2.pl b/challenge-004/alicia-bielsa/perl5/ch-2.pl index 0e2c35fe1f..f001741927 100644 --- a/challenge-004/alicia-bielsa/perl5/ch-2.pl +++ b/challenge-004/alicia-bielsa/perl5/ch-2.pl @@ -1,13 +1,13 @@ #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. +# 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 strict; use warnings; -my $fileWithWords = 'ch-2.txt'; +my $fileWithWords = 'file.txt'; my @aWordsFromFile = readLinesFromFile($fileWithWords); #Letters must be lowercase @@ -17,53 +17,53 @@ my %hLettersAvailable = arrayToHash( \@aListOfLetters ); foreach my $wordFromFile (@aWordsFromFile){ if (canWordBeMade( $wordFromFile )){ - print "$wordFromFile\n"; - } + print "$wordFromFile\n"; + } } sub readLinesFromFile { my $file = shift; my @aLines =(); unless ( -f $file) { - die "ERROR: File '$file' does not exist\n"; + die "ERROR: File '$file' does not exist\n"; } open (my $fh, '<', $file) or die "ERROR: Unable to open file '$file':'$!'\n"; while (my $line = <$fh>) { $line =~ s%^\s+%%; - $line =~ s%\s+$%%; - next unless ($line); - push (@aLines,lc($line)); + $line =~ s%\s+$%%; + next unless ($line); + push (@aLines,lc($line)); } close ($fh); return @aLines ; - + } sub arrayToHash { my $refArray = shift; my %hHash = (); foreach my $item (@{$refArray}){ - if (exists($hHash{$item})){ - $hHash{$item} ++; - } else { - $hHash{$item} = 1; - } - } + if (exists($hHash{$item})){ + $hHash{$item} ++; + } else { + $hHash{$item} = 1; + } + } return %hHash; } sub canWordBeMade { my $wordToCheck = shift; - my $canBeMade = 0; - my %hLettersToUse = %hLettersAvailable; - foreach my $letterInWord (split(//, $wordToCheck)){ - if (exists($hLettersToUse{$letterInWord}) && $hLettersToUse{$letterInWord}){ - $hLettersToUse{$letterInWord}--; - } else { - return $canBeMade; - } - } - return 1; + my $canBeMade = 0; + my %hLettersToUse = %hLettersAvailable; + foreach my $letterInWord (split(//, $wordToCheck)){ + if (exists($hLettersToUse{$letterInWord}) && $hLettersToUse{$letterInWord}){ + $hLettersToUse{$letterInWord}--; + } else { + return $canBeMade; + } + } + return 1; } exit 0; diff --git a/challenge-004/alicia-bielsa/perl5/ch-2.txt b/challenge-004/alicia-bielsa/perl5/file.txt index 98e90672c4..98e90672c4 100644 --- a/challenge-004/alicia-bielsa/perl5/ch-2.txt +++ b/challenge-004/alicia-bielsa/perl5/file.txt |
