aboutsummaryrefslogtreecommitdiff
path: root/challenge-072
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-05 04:38:00 +0100
committerGitHub <noreply@github.com>2020-08-05 04:38:00 +0100
commit5a5df5e5aff20f1c828fc884ca04022642ba66b6 (patch)
treea62acd9f33d0e5a52313de8684358527cf626a6e /challenge-072
parentd178b91f5dc2dd57bee4fff787ac38fa8d2c5cdf (diff)
parent643a0a978d25ff97fcfaf58cc0db222f5e68019d (diff)
downloadperlweeklychallenge-club-5a5df5e5aff20f1c828fc884ca04022642ba66b6.tar.gz
perlweeklychallenge-club-5a5df5e5aff20f1c828fc884ca04022642ba66b6.tar.bz2
perlweeklychallenge-club-5a5df5e5aff20f1c828fc884ca04022642ba66b6.zip
Merge pull request #2038 from waltman/branch-for-challenge-072
Branch for challenge 072
Diffstat (limited to 'challenge-072')
-rw-r--r--challenge-072/walt-mankowski/blog.txt1
-rw-r--r--challenge-072/walt-mankowski/c++/.gitignore2
-rw-r--r--challenge-072/walt-mankowski/c++/Makefile23
-rw-r--r--challenge-072/walt-mankowski/c++/ch-1.cpp30
-rw-r--r--challenge-072/walt-mankowski/c++/ch-2.cpp29
l---------challenge-072/walt-mankowski/c++/input.txt1
-rw-r--r--challenge-072/walt-mankowski/perl/ch-1.pl46
-rw-r--r--challenge-072/walt-mankowski/perl/ch-2.pl21
-rw-r--r--challenge-072/walt-mankowski/perl/input.txt100
-rw-r--r--challenge-072/walt-mankowski/python/ch-1.py21
-rw-r--r--challenge-072/walt-mankowski/python/ch-2.py14
l---------challenge-072/walt-mankowski/python/input.txt1
12 files changed, 289 insertions, 0 deletions
diff --git a/challenge-072/walt-mankowski/blog.txt b/challenge-072/walt-mankowski/blog.txt
new file mode 100644
index 0000000000..154dcccd61
--- /dev/null
+++ b/challenge-072/walt-mankowski/blog.txt
@@ -0,0 +1 @@
+http://www.mawode.com/blog/blog/2020/08/04/perl-weekly-challenge-72/
diff --git a/challenge-072/walt-mankowski/c++/.gitignore b/challenge-072/walt-mankowski/c++/.gitignore
new file mode 100644
index 0000000000..ac77297bfe
--- /dev/null
+++ b/challenge-072/walt-mankowski/c++/.gitignore
@@ -0,0 +1,2 @@
+ch-1
+ch-2
diff --git a/challenge-072/walt-mankowski/c++/Makefile b/challenge-072/walt-mankowski/c++/Makefile
new file mode 100644
index 0000000000..0192513d07
--- /dev/null
+++ b/challenge-072/walt-mankowski/c++/Makefile
@@ -0,0 +1,23 @@
+CPP = /usr/bin/c++
+INCLDIRS =
+LIBDIRS =
+LIBS =
+CFLAGS = -std=c++17 -Wall -O3 $(INCLDIRS)
+OBJECTS1 = ch-1.o
+OBJECTS2 = ch-2.o
+
+all: ch-1 ch-2
+
+%.o: %.cpp
+ $(CPP) $(CFLAGS) -c $<
+
+ch-1: $(OBJECTS1)
+ $(CPP) -o $@ $(OBJECTS1) $(LIBDIRS) $(LIBS)
+
+ch-2: $(OBJECTS2)
+ $(CPP) -o $@ $(OBJECTS2) $(LIBDIRS) $(LIBS)
+
+clean:
+ rm -f *~
+ rm -f *.o
+ rm -f ch-1 ch-2
diff --git a/challenge-072/walt-mankowski/c++/ch-1.cpp b/challenge-072/walt-mankowski/c++/ch-1.cpp
new file mode 100644
index 0000000000..ba8c614fa1
--- /dev/null
+++ b/challenge-072/walt-mankowski/c++/ch-1.cpp
@@ -0,0 +1,30 @@
+#include <stdlib.h>
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+const unsigned long long fact(const int n) {
+ unsigned long long res = 1;
+ for (int i = 2; i <= n; i++)
+ res *= i;
+ return res;
+}
+
+const int num_trailing_zeros(const unsigned long long n) {
+ int cnt = 0;
+ unsigned long long pwr = 10;
+ while (n % pwr == 0) {
+ cnt++;
+ pwr *= 10;
+ }
+ return cnt;
+}
+
+int main(int argc, char *argv[]) {
+ int n = atoi(argv[1]);
+ unsigned long long f = fact(n);
+ int z = num_trailing_zeros(f);
+ string zeros = (z == 1) ? "zero" : "zeros";
+ cout << n << " as N! = " << f << " has " << z << " trailing " << zeros << endl;
+}
diff --git a/challenge-072/walt-mankowski/c++/ch-2.cpp b/challenge-072/walt-mankowski/c++/ch-2.cpp
new file mode 100644
index 0000000000..03f5d1c067
--- /dev/null
+++ b/challenge-072/walt-mankowski/c++/ch-2.cpp
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <iostream>
+#include <fstream>
+#include <string>
+
+using namespace std;
+
+int main(int argc, char *argv[]) {
+ const string fname = argv[1];
+ const int a = atoi(argv[2]);
+ const int b = atoi(argv[3]);
+
+ ifstream infile(fname);
+ if (!infile) {
+ perror(fname.c_str());
+ exit(1);
+ }
+
+ int lineno = 1;
+ string s;
+ while (infile >> s) {
+ if (a <= lineno && lineno <= b)
+ cout << s << endl;
+ lineno++;
+ if (lineno > b)
+ break;
+ }
+}
diff --git a/challenge-072/walt-mankowski/c++/input.txt b/challenge-072/walt-mankowski/c++/input.txt
new file mode 120000
index 0000000000..9d20b30bd1
--- /dev/null
+++ b/challenge-072/walt-mankowski/c++/input.txt
@@ -0,0 +1 @@
+../perl/input.txt \ No newline at end of file
diff --git a/challenge-072/walt-mankowski/perl/ch-1.pl b/challenge-072/walt-mankowski/perl/ch-1.pl
new file mode 100644
index 0000000000..d211135385
--- /dev/null
+++ b/challenge-072/walt-mankowski/perl/ch-1.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.32);
+use experimental qw(signatures);
+use Lingua::EN::Inflect qw(PL);
+
+# TASK #1 › Trailing Zeroes
+# Submitted by: Mohammad S Anwar
+#
+# You are given a positive integer $N (<= 10).
+#
+# Write a script to print number of trailing zeroes in $N!.
+# Example 1
+# Input: $N = 10
+# Output: 2 as $N! = 3628800 has 2 trailing zeroes
+#
+# Example 2
+# Input: $N = 7
+# Output: 1 as $N! = 5040 has 1 trailing zero
+#
+# Example 3
+# Input: $N = 4
+# Output: 0 as $N! = 24 has 0 trailing zero
+
+sub fact($n) {
+ my $res = 1;
+ $res *= $_ for 2..$n;
+ return $res;
+}
+
+sub num_trailing_zeros($n) {
+ my $cnt = 0;
+ my $pwr = 10;
+ while ($n % $pwr == 0) {
+ $cnt++;
+ $pwr *= 10;
+ }
+ return $cnt;
+}
+
+my $n = shift @ARGV;
+my $f = fact($n);
+my $z = num_trailing_zeros($f);
+say "$n as \$N! = $f has $z trailing ", PL("zero", $z);
+
diff --git a/challenge-072/walt-mankowski/perl/ch-2.pl b/challenge-072/walt-mankowski/perl/ch-2.pl
new file mode 100644
index 0000000000..a429abd68f
--- /dev/null
+++ b/challenge-072/walt-mankowski/perl/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.32);
+use experimental qw(signatures);
+use autodie;
+
+# TASK #2 › Lines Range
+# Submitted by: Mohammad S Anwar
+#
+# You are given a text file name $file and range $A - $B where $A <= $B.
+#
+# Write a script to display lines in the given file in the line number
+# range between $A and $B, inclusive.
+
+my ($fname, $a, $b) = @ARGV;
+
+open my $f, '<', $fname;
+while (<$f>) {
+ print if $a == $. .. $b == $.;
+}
diff --git a/challenge-072/walt-mankowski/perl/input.txt b/challenge-072/walt-mankowski/perl/input.txt
new file mode 100644
index 0000000000..e5a15512e0
--- /dev/null
+++ b/challenge-072/walt-mankowski/perl/input.txt
@@ -0,0 +1,100 @@
+L1
+L2
+L3
+L4
+L5
+L6
+L7
+L8
+L9
+L10
+L11
+L12
+L13
+L14
+L15
+L16
+L17
+L18
+L19
+L20
+L21
+L22
+L23
+L24
+L25
+L26
+L27
+L28
+L29
+L30
+L31
+L32
+L33
+L34
+L35
+L36
+L37
+L38
+L39
+L40
+L41
+L42
+L43
+L44
+L45
+L46
+L47
+L48
+L49
+L50
+L51
+L52
+L53
+L54
+L55
+L56
+L57
+L58
+L59
+L60
+L61
+L62
+L63
+L64
+L65
+L66
+L67
+L68
+L69
+L70
+L71
+L72
+L73
+L74
+L75
+L76
+L77
+L78
+L79
+L80
+L81
+L82
+L83
+L84
+L85
+L86
+L87
+L88
+L89
+L90
+L91
+L92
+L93
+L94
+L95
+L96
+L97
+L98
+L99
+L100
diff --git a/challenge-072/walt-mankowski/python/ch-1.py b/challenge-072/walt-mankowski/python/ch-1.py
new file mode 100644
index 0000000000..51106843ad
--- /dev/null
+++ b/challenge-072/walt-mankowski/python/ch-1.py
@@ -0,0 +1,21 @@
+from sys import argv
+
+def fact(n):
+ res = 1
+ for i in range(2, n+1):
+ res *= i
+ return res
+
+def num_trailing_zeros(n):
+ cnt = 0
+ pwr = 10
+ while n % pwr == 0:
+ cnt += 1
+ pwr *= 10
+ return cnt
+
+n = int(argv[1])
+f = fact(n)
+z = num_trailing_zeros(f)
+zeros = "zero" if z == 1 else "zeros"
+print(f"{n} as N! = {f} has {z} trailing {zeros}")
diff --git a/challenge-072/walt-mankowski/python/ch-2.py b/challenge-072/walt-mankowski/python/ch-2.py
new file mode 100644
index 0000000000..ca5b938bf9
--- /dev/null
+++ b/challenge-072/walt-mankowski/python/ch-2.py
@@ -0,0 +1,14 @@
+from sys import argv
+
+fname, a, b = argv[1:5]
+a = int(a)
+b = int(b)
+
+with open(fname) as f:
+ lineno = 1
+ for line in f:
+ if a <= lineno <= b:
+ print(line, end='')
+ lineno += 1
+ if lineno > b:
+ break
diff --git a/challenge-072/walt-mankowski/python/input.txt b/challenge-072/walt-mankowski/python/input.txt
new file mode 120000
index 0000000000..9d20b30bd1
--- /dev/null
+++ b/challenge-072/walt-mankowski/python/input.txt
@@ -0,0 +1 @@
+../perl/input.txt \ No newline at end of file