From bf89287dd9569fc7dc15b4d2ddfe8474aa038863 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Tue, 16 Apr 2019 17:16:27 -0400 Subject: solutions for challenge 004 --- challenge-004/adam-russell/blog.txt | 1 + challenge-004/adam-russell/perl5/ch-1.pl | 34 ++++++++++++++++++++ challenge-004/adam-russell/perl5/ch-2.pl | 55 ++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 challenge-004/adam-russell/blog.txt create mode 100644 challenge-004/adam-russell/perl5/ch-1.pl create mode 100644 challenge-004/adam-russell/perl5/ch-2.pl 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"; + } +} -- cgit