diff options
| author | Jaldhar H. Vyas <jaldhar@braincells.com> | 2019-04-21 13:27:40 -0400 |
|---|---|---|
| committer | Jaldhar H. Vyas <jaldhar@braincells.com> | 2019-04-21 13:32:05 -0400 |
| commit | ba9e2fcd8750a35a0ccb4e3af448d89cf2c6a0fc (patch) | |
| tree | fb1e916e0959c13b6cf68d8fcbf2ea4e6dedf192 | |
| parent | a4107445098418ff93fb0ba54aad3087d37561c6 (diff) | |
| download | perlweeklychallenge-club-ba9e2fcd8750a35a0ccb4e3af448d89cf2c6a0fc.tar.gz perlweeklychallenge-club-ba9e2fcd8750a35a0ccb4e3af448d89cf2c6a0fc.tar.bz2 perlweeklychallenge-club-ba9e2fcd8750a35a0ccb4e3af448d89cf2c6a0fc.zip | |
Challenge 4 by Jaldhar H. Vyas
| -rwxr-xr-x | challenge-004/jaldhar-h-vyas/perl5/ch-1.pl | 9 | ||||
| -rwxr-xr-x | challenge-004/jaldhar-h-vyas/perl5/ch-2.pl | 51 | ||||
| -rwxr-xr-x | challenge-004/jaldhar-h-vyas/perl6/ch-1.p6 | 6 | ||||
| -rwxr-xr-x | challenge-004/jaldhar-h-vyas/perl6/ch-2.p6 | 30 |
4 files changed, 96 insertions, 0 deletions
diff --git a/challenge-004/jaldhar-h-vyas/perl5/ch-1.pl b/challenge-004/jaldhar-h-vyas/perl5/ch-1.pl new file mode 100755 index 0000000000..e6838ba912 --- /dev/null +++ b/challenge-004/jaldhar-h-vyas/perl5/ch-1.pl @@ -0,0 +1,9 @@ +#!/usr/bin/perl +use constant SIZE => 7; +use warnings; +use strict; +use English qw/ -no_match_vars /; +use Math::BigFloat; +use 5.010; + +say Math::BigFloat->bpi((stat $PROGRAM_NAME)[SIZE]); diff --git a/challenge-004/jaldhar-h-vyas/perl5/ch-2.pl b/challenge-004/jaldhar-h-vyas/perl5/ch-2.pl new file mode 100755 index 0000000000..7813df179f --- /dev/null +++ b/challenge-004/jaldhar-h-vyas/perl5/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/perl +use warnings; +use strict; +use 5.010; +use English qw/ -no_match_vars /; + +# I'm assuming the list of allowed letters is given on the command line and the +# list of words is in a file in the current directory called word.list. +# This wasn't clear in the spec. + +sub usage { + print <<"-USAGE-"; + Usage: + $PROGRAM_NAME <letters> + + <letters> A case-insensitive string of letters to be matched. + Can include repeats. +-USAGE- + + exit(1); +} + +if (scalar @ARGV < 1) { + usage(); +} + +my $letters = $ARGV[0]; + +open my $wordList, '<', 'word.list' or die "word.list: $!\n"; +local $RS; +my $contents = <$wordList>; +close $wordList; +my @words = split /\n/, $contents; + + +foreach my $word (@words) { + my $upword = uc $word; # converting word and letter list to upper case + my $used = uc $letters; # for case-insensitive comparisons. + my $bogus = undef; + for my $letter (split //, $upword) { + if ($used !~ s/$letter//) { + $bogus = 1; + last; + } + } + + if (!$bogus) { + say $word; + } +} + diff --git a/challenge-004/jaldhar-h-vyas/perl6/ch-1.p6 b/challenge-004/jaldhar-h-vyas/perl6/ch-1.p6 new file mode 100755 index 0000000000..039e7f4c51 --- /dev/null +++ b/challenge-004/jaldhar-h-vyas/perl6/ch-1.p6 @@ -0,0 +1,6 @@ +#!/usr/bin/perl6 +# Thanks to thundergnat for the module and informing me about it on IRC. +use Rat::Precise; + +my $size = $*PROGRAM-NAME.IO.s; +π.FatRat.precise($size - 1).say; # - 1 because 3. is a digit of pi diff --git a/challenge-004/jaldhar-h-vyas/perl6/ch-2.p6 b/challenge-004/jaldhar-h-vyas/perl6/ch-2.p6 new file mode 100755 index 0000000000..cb83d02af3 --- /dev/null +++ b/challenge-004/jaldhar-h-vyas/perl6/ch-2.p6 @@ -0,0 +1,30 @@ +#!/usr/bin/perl6 + +# I'm assuming the list of allowed letters is given on the command line and the +# list of words is in a file in the current directory called word.list. +# This wasn't clear in the spec. + +multi sub MAIN( + Str $letters #= A case-insensitive string of letters to be matched. + #= Can include repeats. +) { + my $contents = "word.list".IO.slurp; + my @words = $contents.lines; + + for @words -> $word { + my $upword = $word.uc; # converting word and letter list to upper case + my $used = $letters.uc; # for case-insensitive comparisons. + my Bool $bogus = False; + + for $upword.comb -> $letter { + if ($used !~~ s/$letter//) { + $bogus = True; + last; + } + } + + if (!$bogus) { + say $word; + } + } +} |
