aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-004/paulo-custodio/README1
-rw-r--r--challenge-004/paulo-custodio/perl/ch-1.pl17
-rw-r--r--challenge-004/paulo-custodio/perl/ch-2.pl31
-rw-r--r--challenge-004/paulo-custodio/test.pl45
4 files changed, 94 insertions, 0 deletions
diff --git a/challenge-004/paulo-custodio/README b/challenge-004/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-004/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-004/paulo-custodio/perl/ch-1.pl b/challenge-004/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..9604e919a8
--- /dev/null
+++ b/challenge-004/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/perl
+
+# Challenge 004
+#
+# Challenge #1
+# Write a script to output the same number of PI digits as the size of your script. Say, if your script size is 10, it should print 3.141592653.
+
+use strict;
+use warnings;
+use 5.030;
+
+# we need a big-math library to compute any large number of digits; pi is offered for free!
+use Math::Big 'pi';
+
+my $size = -s $0; # size of script
+my $pi = pi();
+say substr($pi, 0, $size+1); # +1 to account for decimal dot
diff --git a/challenge-004/paulo-custodio/perl/ch-2.pl b/challenge-004/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..9bcaa2cdfc
--- /dev/null
+++ b/challenge-004/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/perl
+
+# 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 that 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;
+use 5.030;
+
+@ARGV==2 or die "Usage: ch-2.pl dict letters\n";
+my($dict_file, $letters) = @ARGV;
+
+$letters = lc($letters);
+
+open(my $fh, "<", $dict_file) or die "open $dict_file: $!\n";
+while (<$fh>) {
+ chomp;
+ say $_ if matches($_, $letters);
+}
+
+
+sub matches {
+ my($word, $letters) = @_;
+ for my $c (split //, $letters) {
+ $word =~ s/$c//;
+ return 1 if $word eq '';
+ }
+ return 0;
+}
diff --git a/challenge-004/paulo-custodio/test.pl b/challenge-004/paulo-custodio/test.pl
new file mode 100644
index 0000000000..0bfa5e390a
--- /dev/null
+++ b/challenge-004/paulo-custodio/test.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+use 5.030;
+
+is capture("perl perl/ch-1.pl 20"), <<END;
+3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996
+END
+
+# build list of words for testing
+ok 0==system("aspell -d en dump master | aspell -l en expand > words.txt");
+
+is capture("perl perl/ch-2.pl words.txt world"), <<END;
+w
+o
+or
+ow
+owl
+old
+l
+lo
+low
+lord
+r
+row
+rd
+rod
+d
+do
+wold
+world
+word
+END
+
+
+done_testing;
+
+sub capture {
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \t\v\f\r]*\n/\n/g;
+ return $out;
+}