aboutsummaryrefslogtreecommitdiff
path: root/challenge-072/dave-jacoby
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-072/dave-jacoby')
-rw-r--r--challenge-072/dave-jacoby/README1
-rw-r--r--challenge-072/dave-jacoby/blog.txt1
-rwxr-xr-xchallenge-072/dave-jacoby/perl/ch-1.pl32
-rwxr-xr-xchallenge-072/dave-jacoby/perl/ch-2.pl33
4 files changed, 67 insertions, 0 deletions
diff --git a/challenge-072/dave-jacoby/README b/challenge-072/dave-jacoby/README
new file mode 100644
index 0000000000..7c06689f16
--- /dev/null
+++ b/challenge-072/dave-jacoby/README
@@ -0,0 +1 @@
+Solution by Dave Jacoby
diff --git a/challenge-072/dave-jacoby/blog.txt b/challenge-072/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..56e3e3f802
--- /dev/null
+++ b/challenge-072/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2020/08/03/perl-weekly-challenge-72.html
diff --git a/challenge-072/dave-jacoby/perl/ch-1.pl b/challenge-072/dave-jacoby/perl/ch-1.pl
new file mode 100755
index 0000000000..b975439405
--- /dev/null
+++ b/challenge-072/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say signatures state };
+no warnings qw{ experimental };
+
+use Carp;
+use Getopt::Long;
+use List::Util qw{ reduce };
+
+my $n = 4;
+GetOptions( 'n=i' => \$n, );
+croak 'n must be positive' if $n < 1;
+croak 'n must be less than 11' if $n > 10;
+
+my $f = factorial($n);
+my $t = trailing($f);
+
+my $zero = 'zero';
+$zero = 'zeroes' if $t > 1;
+
+say qq{Output: $n as N! = $f has $t trailing $zero};
+
+sub trailing( $f ) {
+ my ($z) = $f =~ /(0+)$/mix;
+ return length $z || 0;
+}
+
+sub factorial ( $n ) {
+ return reduce { $a * $b } 1 .. $n;
+}
diff --git a/challenge-072/dave-jacoby/perl/ch-2.pl b/challenge-072/dave-jacoby/perl/ch-2.pl
new file mode 100755
index 0000000000..8b35a67453
--- /dev/null
+++ b/challenge-072/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say signatures state };
+no warnings qw{ experimental };
+
+use Carp;
+use Getopt::Long;
+
+my $f = $0; # this program
+my $a = 1;
+my $b = 4;
+GetOptions(
+ 'f=s' => \$f,
+ 'a=i' => \$a,
+ 'b=i' => \$b,
+);
+
+show_lines( $a, $b, $f );
+
+sub show_lines ( $a, $b, $f ) {
+ my $c = 0;
+ if ( open my $fh, '<', $f ) {
+ my @array = <$fh>;
+ for my $i (@array) {
+ $c++;
+ next if $c > $b;
+ next if $c < $a;
+ print $i ;
+ }
+ }
+}