diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-04-19 09:50:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-04-19 09:50:24 +0100 |
| commit | 223f566490d33eb6ef01b963b4cae8d99ab7c183 (patch) | |
| tree | 9a4af3578fd40c5d0c6afeaddc4239ac76cd060f | |
| parent | 3992857525eb669e474cbb0f39c0861f2d71800b (diff) | |
| parent | 50b28acf2851a70c78276367f9ade185c06af701 (diff) | |
| download | perlweeklychallenge-club-223f566490d33eb6ef01b963b4cae8d99ab7c183.tar.gz perlweeklychallenge-club-223f566490d33eb6ef01b963b4cae8d99ab7c183.tar.bz2 perlweeklychallenge-club-223f566490d33eb6ef01b963b4cae8d99ab7c183.zip | |
Merge pull request #73 from aliciabielsa/new-branch
challenge 4 alicia bielsa
| -rw-r--r-- | challenge-004/alicia-bielsa/perl5/ch-1.pl | 5 | ||||
| -rw-r--r-- | challenge-004/alicia-bielsa/perl5/ch-2.pl | 69 | ||||
| -rw-r--r-- | challenge-004/alicia-bielsa/perl5/ch-2.txt | 58 |
3 files changed, 132 insertions, 0 deletions
diff --git a/challenge-004/alicia-bielsa/perl5/ch-1.pl b/challenge-004/alicia-bielsa/perl5/ch-1.pl new file mode 100644 index 0000000000..7dcfb9c9f4 --- /dev/null +++ b/challenge-004/alicia-bielsa/perl5/ch-1.pl @@ -0,0 +1,5 @@ +use strict; +use warnings; + +use bignum 'bpi'; +print bpi((stat($0))[7]); diff --git a/challenge-004/alicia-bielsa/perl5/ch-2.pl b/challenge-004/alicia-bielsa/perl5/ch-2.pl new file mode 100644 index 0000000000..0e2c35fe1f --- /dev/null +++ b/challenge-004/alicia-bielsa/perl5/ch-2.pl @@ -0,0 +1,69 @@ +#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. +#(Disclaimer: The challenge was proposed by Scimon Proctor) +use strict; +use warnings; + +my $fileWithWords = 'ch-2.txt'; +my @aWordsFromFile = readLinesFromFile($fileWithWords); + +#Letters must be lowercase +my @aListOfLetters = qw(h o l a e t s p r r k k a n w z d); +my %hLettersAvailable = arrayToHash( \@aListOfLetters ); + + +foreach my $wordFromFile (@aWordsFromFile){ + if (canWordBeMade( $wordFromFile )){ + print "$wordFromFile\n"; + } +} + +sub readLinesFromFile { + my $file = shift; + my @aLines =(); + unless ( -f $file) { + 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)); + } + 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; + } + } + 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; +} + +exit 0; diff --git a/challenge-004/alicia-bielsa/perl5/ch-2.txt b/challenge-004/alicia-bielsa/perl5/ch-2.txt new file mode 100644 index 0000000000..98e90672c4 --- /dev/null +++ b/challenge-004/alicia-bielsa/perl5/ch-2.txt @@ -0,0 +1,58 @@ +New +There +Zealand +Zealands +a +adult +ago +alive +although +and +are +around +before +being +bird +birds +breeding +brink +by +common +criticallyendangered +extinction +farming +fattest +few +finally +forest +fortunes +had +has +homes +hundred +hunted +in +introduced +killed +kakapo +losing +most +of +one +only +parrot +pests +recordbreaking +saying +scientists +season +species +the +their +they +to +today +turning +were +with +years
\ No newline at end of file |
