aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Russell <adam.russell@optum.com>2019-04-16 17:16:27 -0400
committerAdam Russell <adam.russell@optum.com>2019-04-16 17:16:27 -0400
commitbf89287dd9569fc7dc15b4d2ddfe8474aa038863 (patch)
tree567eb304ed88e3a94cf5ef39c6ab717867f1b2c9
parent010b302870886327f53966cbf906b68f2b13b446 (diff)
downloadperlweeklychallenge-club-bf89287dd9569fc7dc15b4d2ddfe8474aa038863.tar.gz
perlweeklychallenge-club-bf89287dd9569fc7dc15b4d2ddfe8474aa038863.tar.bz2
perlweeklychallenge-club-bf89287dd9569fc7dc15b4d2ddfe8474aa038863.zip
solutions for challenge 004
-rw-r--r--challenge-004/adam-russell/blog.txt1
-rw-r--r--challenge-004/adam-russell/perl5/ch-1.pl34
-rw-r--r--challenge-004/adam-russell/perl5/ch-2.pl55
3 files changed, 90 insertions, 0 deletions
diff --git a/challenge-004/adam-russell/blog.txt b/challenge-004/adam-russell/blog.txt
new file mode 100644
index 0000000000..4c79e37d17
--- /dev/null
+++ b/challenge-004/adam-russell/blog.txt
@@ -0,0 +1 @@
+https://adamcrussell.livejournal.com/1247.html
diff --git a/challenge-004/adam-russell/perl5/ch-1.pl b/challenge-004/adam-russell/perl5/ch-1.pl
new file mode 100644
index 0000000000..9150261c6b
--- /dev/null
+++ b/challenge-004/adam-russell/perl5/ch-1.pl
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+##
+# 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 bignum;
+
+sub pi{
+ my($max) = @_;
+ my @digits;
+ my($k, $a, $b, $a1, $b1) = (2, 4, 1, 12, 4);
+ while(@digits < $max){
+ my($p, $q) = ($k ** 2, 2 * $k + 1);
+ $k = $k + 1;
+ ($a, $b , $a1, $b1) = ($a1, $b1, $p * $a + $q * $a1, $p * $b + $q * $b1);
+ my($d, $d1) = (int($a/$b), int($a1/$b1));
+ while($d == $d1){
+ push @digits, $d;
+ if(@digits >= $max){
+ return \@digits;
+ }
+ ($a, $a1) = (10 * ($a % $b), 10 * ($a1 % $b1));
+ ($d, $d1) = (int($a / $b), int($a1 / $b1));
+ }
+ }
+ return \@digits;
+}
+
+##
+# Main
+##
+my $digits=pi((stat($0))[7]);
+print $digits->[0] . "." . join("", @{$digits}[1 .. @{$digits} - 1]);
diff --git a/challenge-004/adam-russell/perl5/ch-2.pl b/challenge-004/adam-russell/perl5/ch-2.pl
new file mode 100644
index 0000000000..75e57258d4
--- /dev/null
+++ b/challenge-004/adam-russell/perl5/ch-2.pl
@@ -0,0 +1,55 @@
+use strict;
+use warnings;
+##
+# 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 boolean;
+
+my @words=qw|
+ cat
+ mouse
+ rabbit
+ horse
+ sheep
+ cow
+ aardvark
+|;
+
+my @letters = qw|
+ a b c e h i o p r s t w b e z
+|;
+
+sub contains_remove{
+ my($c) = @_;
+ return sub{
+ my($word) = @_;
+ $word =~ s/$c//;
+ return $word;
+ }
+}
+
+my @checks;
+for my $c (@letters){
+ push @checks, contains_remove(lc($c));
+}
+
+sub check_word{
+ my($word, $checks) = @_;
+ if($word && !@{$checks}){
+ return false;
+ }
+ $word = &{$checks->[0]}($word);
+ if(!$word){
+ return true;
+ }
+ check_word($word, [@{$checks}[1 .. @{$checks} - 1]]);
+}
+
+for my $w (@words){
+ if(check_word(lc($w), \@checks)){
+ print "$w\n";
+ }
+}