Last active
April 16, 2019 21:11
-
-
Save adamcrussell/4e1d8621ff29e5121ac3b0bdcd607d56 to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 004
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment