aboutsummaryrefslogtreecommitdiff
path: root/challenge-072
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-08-06 16:00:07 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-08-06 16:00:07 +0100
commite08b42ecae515d2f138ed9775adc85d430e8c5b1 (patch)
tree76c082eb12359c13ec92215fd3f1a4246f5a638a /challenge-072
parentdaa1a36e173b7fac0781494a34d2b45e730857ad (diff)
downloadperlweeklychallenge-club-e08b42ecae515d2f138ed9775adc85d430e8c5b1.tar.gz
perlweeklychallenge-club-e08b42ecae515d2f138ed9775adc85d430e8c5b1.tar.bz2
perlweeklychallenge-club-e08b42ecae515d2f138ed9775adc85d430e8c5b1.zip
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-072')
-rw-r--r--challenge-072/pete-houston/awk/ch-1.awk29
-rw-r--r--challenge-072/pete-houston/awk/ch-2.awk18
-rw-r--r--challenge-072/pete-houston/perl/ch-1.pl30
-rw-r--r--challenge-072/pete-houston/perl/ch-2.pl29
4 files changed, 106 insertions, 0 deletions
diff --git a/challenge-072/pete-houston/awk/ch-1.awk b/challenge-072/pete-houston/awk/ch-1.awk
new file mode 100644
index 0000000000..cc8b3bbc2f
--- /dev/null
+++ b/challenge-072/pete-houston/awk/ch-1.awk
@@ -0,0 +1,29 @@
+#!/usr/bin/gawk -f
+#===============================================================================
+#
+# FILE: 7201.awk
+#
+# DESCRIPTION: Given a whole number N display the number of trailing
+# zeros in N!
+#
+# NOTES: The task was up to N = 10 but this works up to 22 on a
+# 64-bit platform. Tested on gawk but should work on most
+# awks.
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 03/08/20
+#===============================================================================
+BEGIN { print "Enter a whole number on each line." }
+!/^[0-9]+$/ { print "Input is not a whole number - skipping"; next; }
+$1 > 22 { print "Too big for integer type - skipping"; next; }
+{
+ f = x = $1;
+ while (x > 2) {
+ x--;
+ f = f * x;
+ }
+ print "Factorial is " f;
+ match (f, /0*$/);
+ print "Trailing zeros: " RLENGTH;
+}
diff --git a/challenge-072/pete-houston/awk/ch-2.awk b/challenge-072/pete-houston/awk/ch-2.awk
new file mode 100644
index 0000000000..31b580817d
--- /dev/null
+++ b/challenge-072/pete-houston/awk/ch-2.awk
@@ -0,0 +1,18 @@
+#!/usr/bin/gawk -f
+#===============================================================================
+#
+# FILE: 7202.awk
+# USAGE: 7202.awk X Y filename [ filename ... ]
+#
+# DESCRIPTION: Only print the lines of the supplied file(s) between
+# line numbers X and Y inclusive.
+#
+# NOTES: Arguments X and Y are whole numbers. Y >= X, X >= 1.
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 04/08/20
+#===============================================================================
+BEGIN { start = ARGV[1]; finish = ARGV[2]; delete ARGV[2]; delete ARGV[1]; }
+FNR > finish { nextfile; }
+FNR >= start
diff --git a/challenge-072/pete-houston/perl/ch-1.pl b/challenge-072/pete-houston/perl/ch-1.pl
new file mode 100644
index 0000000000..6a72e6f57f
--- /dev/null
+++ b/challenge-072/pete-houston/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 7201.pl
+#
+# USAGE: ./7201.pl N [ N ... ]
+#
+# DESCRIPTION: Given a whole number N display the number of trailing
+# zeros in N!
+#
+# REQUIREMENTS: Math::GMP, Params::Util, Lingua::EN::Inflexion
+# NOTES: Unclever but effective. The task was up to N = 10 but
+# this seems good for any arbitrary N thanks to Math::GMP.
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 03/08/20
+#===============================================================================
+
+use strict;
+use warnings;
+use Math::GMP;
+use Params::Util '_POSINT';
+use Lingua::EN::Inflexion;
+
+for my $n (@ARGV) {
+ next unless _POSINT ($n);
+ my $z =()= Math::GMP->new($n)->bfac =~ /0(?=0*$)/g;
+ print inflect ("$n! has <#n:$z> trailing <N:zero>\n");
+}
diff --git a/challenge-072/pete-houston/perl/ch-2.pl b/challenge-072/pete-houston/perl/ch-2.pl
new file mode 100644
index 0000000000..4bcb7862ec
--- /dev/null
+++ b/challenge-072/pete-houston/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 7202.pl
+#
+# USAGE: ./7202.pl LINEA LINEB FILE [ FILE ... ]
+#
+# DESCRIPTION: Display lines from number A to B inclusive of FILE on STDOUT
+#
+# NOTES: Optimised for memory, not speed
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 04/08/20
+#===============================================================================
+
+use strict;
+use warnings;
+use autodie;
+
+my $start = shift @ARGV;
+my $end = shift @ARGV;
+print "start = $start, end = $end\n";
+for my $file (@ARGV) {
+ open my $in, '<', $file;
+ <$in> for 2 .. $start;
+ print '' . <$in> for ($start .. $end);
+ close $in;
+}