aboutsummaryrefslogtreecommitdiff
path: root/challenge-004
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-04-18 10:12:02 +0100
committerGitHub <noreply@github.com>2019-04-18 10:12:02 +0100
commit0709e5314dd9f414e0887bf35f1e1e29526e734b (patch)
treed64ab739d52166d5ffd71f196fd161f1904a6bd0 /challenge-004
parent8b438a02a5e889f3129ddd4f8016d8256563176a (diff)
parent0c48a02a29732da2aa1aef4ff1dd124665f3cd62 (diff)
downloadperlweeklychallenge-club-0709e5314dd9f414e0887bf35f1e1e29526e734b.tar.gz
perlweeklychallenge-club-0709e5314dd9f414e0887bf35f1e1e29526e734b.tar.bz2
perlweeklychallenge-club-0709e5314dd9f414e0887bf35f1e1e29526e734b.zip
Merge pull request #71 from gnustavo/gustavo-chaves-004
Add Perl 5 solutions to challenge 004
Diffstat (limited to 'challenge-004')
-rwxr-xr-xchallenge-004/gustavo-chaves/perl5/ch-1.pl3
-rwxr-xr-xchallenge-004/gustavo-chaves/perl5/ch-2.pl38
2 files changed, 41 insertions, 0 deletions
diff --git a/challenge-004/gustavo-chaves/perl5/ch-1.pl b/challenge-004/gustavo-chaves/perl5/ch-1.pl
new file mode 100755
index 0000000000..0e38ac4e6e
--- /dev/null
+++ b/challenge-004/gustavo-chaves/perl5/ch-1.pl
@@ -0,0 +1,3 @@
+#!/usr/bin/perl
+use bignum 'bpi';
+print bpi(49);
diff --git a/challenge-004/gustavo-chaves/perl5/ch-2.pl b/challenge-004/gustavo-chaves/perl5/ch-2.pl
new file mode 100755
index 0000000000..4afacefaf0
--- /dev/null
+++ b/challenge-004/gustavo-chaves/perl5/ch-2.pl
@@ -0,0 +1,38 @@
+#!/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;
+
+die "usage: $0 <file> <letters>\n" unless @ARGV == 2;
+my $file = shift;
+my $letters = shift;
+
+# Construct a hash mapping each letter to its count number
+my %letters;
+++$letters{$_} foreach split //, lc $letters;
+
+# Return true if $word matches %letters
+sub match {
+ my ($word) = @_;
+ my %letters = %letters;
+ foreach my $char (split //, lc $word) {
+ return 0 unless exists $letters{$char} && $letters{$char}-- > 0;
+ }
+ return 1;
+}
+
+open my $fh, '<', $file;
+while (<$fh>) {
+ chomp;
+ say $_ if match($_);
+}
+close $fh;