aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-03 14:25:33 +0100
committerGitHub <noreply@github.com>2020-08-03 14:25:33 +0100
commit2e5f90f7031223a8d908d8f4411271ebf7aba64f (patch)
tree8e25ad7263d968f550132a676feff9941aa0e910
parent99521f09f4b8dced34101562d0eff194b1c643bd (diff)
parent77066a3f4b210fec62d2721db2b3af23b9d322a6 (diff)
downloadperlweeklychallenge-club-2e5f90f7031223a8d908d8f4411271ebf7aba64f.tar.gz
perlweeklychallenge-club-2e5f90f7031223a8d908d8f4411271ebf7aba64f.tar.bz2
perlweeklychallenge-club-2e5f90f7031223a8d908d8f4411271ebf7aba64f.zip
Merge pull request #2024 from dcw803/master
two easy questions this week
-rw-r--r--challenge-072/duncan-c-white/README103
-rwxr-xr-xchallenge-072/duncan-c-white/perl/ch-1.pl49
-rwxr-xr-xchallenge-072/duncan-c-white/perl/ch-1a.pl54
-rwxr-xr-xchallenge-072/duncan-c-white/perl/ch-2.pl53
4 files changed, 196 insertions, 63 deletions
diff --git a/challenge-072/duncan-c-white/README b/challenge-072/duncan-c-white/README
index 6f2a919fd6..8f0971a5c6 100644
--- a/challenge-072/duncan-c-white/README
+++ b/challenge-072/duncan-c-white/README
@@ -1,81 +1,58 @@
-Task 1: "Character Swapping
+Task 1: "Trailing Zeroes
-You are given a string $S of size $N.
+You are given a positive integer $N (<= 10).
-You are also given swap count $C and offset $O such that $C >= 1, $O >=
-1, $C <= $O and $C + $O <= $N.
-
-Write a script to perform character swapping like below:
-
-for i = 1 to $C
- S[ i % N ] <=> S[ (i + O) % N ]
+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
-Input:
- $S = 'perlandraku'
- $C = 3
- $O = 4
+Example 2
+Input: $N = 7
+Output: 1 as $N! = 5040 has 1 trailing zero
-01234567890
-perlandraku
+Example 3
+Input: $N = 4
+Output: 0 as $N! = 24 has 0 trailing zero
-Character Swapping:
- swap 1: pos 1 and 5: e <=> n = pnrlaedraku
- swap 2: pos 2 and 6: r <=> d = pndlaerraku
- swap 3: pos 3 and 7: l <=> r = pndraerlaku
-
-Output:
- pndraerlaku
"
-My notes: ok. none of $i % N (for i=1..$C) needs the % N
-given that $C + $O <= $N and min $O is 1. So that becomes:
-
-for i = 1 to $C
- S[ i ] <=> S[ (i + O) % N ]
-
-and the only one of the (i + O) % N expressions that may need
-the % N part is i=C, when C+O == N. Of course i + O increments
-so pre-calculate it, and set it back to 0 hit it hits N.
-
-
-Task 2: "Gray Code Sequence
-
-You are given an integer 2 <= $N <= 5.
+My notes: ok. Very easy. fact and "while mod 10 == 0 inc & div 10"
+See also ch-1a.pl, which TABULATES n, n! and trailing-zeroes(n!)..
-Write a script to generate $N-bit gray code sequence.
+Task 2: "Lines Range
-2-bit Gray Code Sequence: [0, 1, 3, 2]
+You are given a text file name $file and range $A - $B where $A <= $B.
-To generate the 3-bit Gray code sequence from the 2-bit Gray code sequence,
-follow the step below:
-
-2-bit Gray Code sequence: [0, 1, 3, 2]
-
-Binary form of the sequence a) S1 = [00, 01, 11, 10]
-
-Reverse of S1 b) S2 = [10, 11, 01, 00]
-
-Prefix all entries of S1 with '0' c) S1 = [000, 001, 011, 010]
-
-Prefix all entries of S2 with '1' d) S2 = [110, 111, 101, 100]
-
-Concatenate S1 and S2 gives 3-bit Gray Code sequence
- e) [000, 001, 011, 010, 110, 111, 101, 100]
+Write a script to display lines range $A and $B in the given file.
+Example
+Input:
-Now convert them back to decimal:
+ $ cat input.txt
+ L1
+ L2
+ L3
+ L4
+ ...
+ ...
+ ...
+ ...
+ L100
-3-bit Gray Code sequence [0, 1, 3, 2, 6, 7, 5, 4]
+$A = 4 and $B = 12
-Example
+Output:
-Input: N = 4
-Output: [0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8]
+ L4
+ L5
+ L6
+ L7
+ L8
+ L9
+ L10
+ L11
+ L12
"
-My notes: ok. However, there's no point in taking the original N-1-bit gray
-code sequence, converting it to binary, prepending zeroes and converting it
-back to decimal - because leading zeroes don't change the decimal numbers.
-So the first half of the N-bit gray code sequence is simply the N-1-bit gray
-code sequence.
+My notes: ok. Seems even easier. I/O and a line number count (let's use $.)
diff --git a/challenge-072/duncan-c-white/perl/ch-1.pl b/challenge-072/duncan-c-white/perl/ch-1.pl
new file mode 100755
index 0000000000..8c17543cb2
--- /dev/null
+++ b/challenge-072/duncan-c-white/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+#
+# Task 1: "Trailing Zeroes
+#
+# 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
+#
+# "
+#
+# My notes: ok. Very easy. fact and "while mod 10 == 0 inc & div 10"
+#
+
+use strict;
+use warnings;
+use feature 'say';
+#use Function::Parameters;
+#use Data::Dumper;
+
+die "Usage: fact-trailing-zeroes N\n" unless @ARGV==1;
+my( $n ) = @ARGV;
+
+die "fact-trailing-zeroes: N must be >= 0 ($n given)\n" unless $n>=0;
+
+my $fact = 1;
+$fact *= $_ for 1..$n;
+say "fact(n) = $fact";
+
+# trailing zeroes of fact
+my $tz = 0;
+my $i = $fact;
+while( $i % 10 == 0 )
+{
+ $i /= 10;
+ $tz++;
+}
+say "trailing zeroes($fact) = $tz";
diff --git a/challenge-072/duncan-c-white/perl/ch-1a.pl b/challenge-072/duncan-c-white/perl/ch-1a.pl
new file mode 100755
index 0000000000..9fe74145c1
--- /dev/null
+++ b/challenge-072/duncan-c-white/perl/ch-1a.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+#
+# Task 1: "Trailing Zeroes
+#
+# 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
+#
+# "
+#
+# My notes: ok. Very easy. fact and "while mod 10 == 0 inc & div 10"
+# Let's TABULATE n, fact n, trailing zeroes(fact n)
+#
+
+use strict;
+use warnings;
+use feature 'say';
+#use Function::Parameters;
+#use Data::Dumper;
+
+die "Usage: fact-trailing-zeroes N\n" unless @ARGV==1;
+my( $n ) = @ARGV;
+
+die "fact-trailing-zeroes: N must be >= 0 ($n given)\n" unless $n>=0;
+
+say " N N! tz(N!)";
+
+my $fact = 1;
+foreach (1..$n)
+{
+ $fact *= $_;
+
+ # trailing zeroes of fact
+ my $tz = 0;
+ my $i = $fact;
+ while( $i % 10 == 0 )
+ {
+ $i /= 10;
+ $tz++;
+ }
+ printf " %2d %10d %d\n", $_, $fact, $tz;
+}
diff --git a/challenge-072/duncan-c-white/perl/ch-2.pl b/challenge-072/duncan-c-white/perl/ch-2.pl
new file mode 100755
index 0000000000..0a8f24618b
--- /dev/null
+++ b/challenge-072/duncan-c-white/perl/ch-2.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/perl
+#
+# Task 2: "Lines Range
+#
+# You are given a text file name $file and range $A - $B where $A <= $B.
+#
+# Write a script to display lines range $A and $B in the given file.
+#
+# Example
+# Input:
+# $ cat input.txt
+# L1
+# L2
+# L3
+# L4
+# ...
+# ...
+# L100
+#
+# $A = 4 and $B = 12
+#
+# Output:
+#
+# L4
+# L5
+# L6
+# ...
+# L10
+# L11
+# L12
+# "
+#
+# My notes: ok. Seems even easier. I/O and a line number count (let's use $.)
+#
+
+
+use strict;
+use warnings;
+use feature 'say';
+#use Function::Parameters;
+#use Data::Dumper;
+
+die "Usage: line-range FROM TO [filename]\n" unless @ARGV==2 || @ARGV==3;
+my $from = shift;
+my $to = shift;
+
+die "line-range: 1 <= FROM <= TO ($from..$to given)\n"
+ unless $from>=1 && $to>=$from;
+
+while( <> )
+{
+ print if $. >= $from && $. <= $to;
+}