aboutsummaryrefslogtreecommitdiff
path: root/challenge-072
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-04 19:24:36 +0100
committerGitHub <noreply@github.com>2020-08-04 19:24:36 +0100
commit6101e96984512a69dc9d7da7f5264f4d62c09695 (patch)
tree007c01fab926b9822f48fe214505d2bc89020896 /challenge-072
parent4374602aa1d0c2e7cdb494c677b0c3ba771fa47c (diff)
parent08b3bed202ba08488e9081e904f771c9c4f5b8f7 (diff)
downloadperlweeklychallenge-club-6101e96984512a69dc9d7da7f5264f4d62c09695.tar.gz
perlweeklychallenge-club-6101e96984512a69dc9d7da7f5264f4d62c09695.tar.bz2
perlweeklychallenge-club-6101e96984512a69dc9d7da7f5264f4d62c09695.zip
Merge pull request #2035 from wanderdoc/master
Solutions to challenge-072.
Diffstat (limited to 'challenge-072')
-rw-r--r--challenge-072/wanderdoc/Perl/ch-1.pl56
-rw-r--r--challenge-072/wanderdoc/Perl/ch-2.pl50
-rw-r--r--challenge-072/wanderdoc/R/ch-1.R6
-rw-r--r--challenge-072/wanderdoc/R/ch-2.R20
4 files changed, 132 insertions, 0 deletions
diff --git a/challenge-072/wanderdoc/Perl/ch-1.pl b/challenge-072/wanderdoc/Perl/ch-1.pl
new file mode 100644
index 0000000000..3cc64884e6
--- /dev/null
+++ b/challenge-072/wanderdoc/Perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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
+=cut
+
+
+
+
+# Variant #1. A factorial number gets a trailing zero after 4*5 and 9*10.
+use List::Util qw(max);
+
+my %zeroes = (1 => 0, 5 => 1, 10 => 2);
+
+sub find_zeroes_1
+{
+ my ($num, $hr) = @_;
+ return exists $hr->{$num} ? $hr->{$num} : $hr->{max(grep $_ < $num, keys %$hr)};
+
+}
+
+for my $i ( 1 .. 10 )
+{
+ print join("->", $i, find_zeroes_1($i, \%zeroes)), $/;
+}
+
+
+# Variant 2. Calculate factorials for 1 .. 10 (or just save the results in a hash).
+# I could use a factorial subroutine from "Mastering Perl" again :-)
+my %factorials = (1 => 1, 2 => 2, 3 => 6, 4 => 24, 5 => 120,
+6 => 720, 7 => 5040, 8 => 40320, 9 => 362880, 10 => 3628800);
+
+
+sub find_zeroes_2
+{
+ my $num = $_[0];
+
+ my $trailing_zeroes;
+ if ( $num =~ /[^0]([0]+)$/ )
+ {
+ $trailing_zeroes = $1;
+ }
+ return length($trailing_zeroes) || 0;
+}
+
+for ( 1 .. 10 )
+{
+ print join('->', $_, find_zeroes_2($factorials{$_})), $/;
+} \ No newline at end of file
diff --git a/challenge-072/wanderdoc/Perl/ch-2.pl b/challenge-072/wanderdoc/Perl/ch-2.pl
new file mode 100644
index 0000000000..1885e99fa5
--- /dev/null
+++ b/challenge-072/wanderdoc/Perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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
+ ...
+ L100
+
+$A = 4 and $B = 12
+Output:
+ L4
+ ...
+ L12
+=cut
+
+use File::Spec;
+my $FROM = shift || 4;
+my $TO = shift || 12;
+
+my $tempfile = File::Spec->catfile(File::Spec->tmpdir(), 'input.txt');
+
+
+{
+ open my $out, ">", $tempfile or die "$!";
+ for my $i ( 1 .. 100 )
+ {
+ print {$out} "L${i}", $/;
+ }
+}
+
+{
+
+ open my $in, "<", $tempfile or die "$!";
+
+
+ while ( my $line = <$in> )
+ {
+ next if $. < $FROM;
+
+ last if $. > $TO;
+ print $line;
+ }
+}
+
+unlink $tempfile or die "$!"; \ No newline at end of file
diff --git a/challenge-072/wanderdoc/R/ch-1.R b/challenge-072/wanderdoc/R/ch-1.R
new file mode 100644
index 0000000000..e457460b2a
--- /dev/null
+++ b/challenge-072/wanderdoc/R/ch-1.R
@@ -0,0 +1,6 @@
+trailingZeroes <- function(x) nchar(regmatches(x, regexpr("[^0]\\K[0]+$", x, perl=TRUE)))
+result <- as.integer(sapply(factorial(1:10), trailingZeroes))
+result[is.na(result)] <- 0
+
+result
+rm(trailingZeroes, result)
diff --git a/challenge-072/wanderdoc/R/ch-2.R b/challenge-072/wanderdoc/R/ch-2.R
new file mode 100644
index 0000000000..7494de2dab
--- /dev/null
+++ b/challenge-072/wanderdoc/R/ch-2.R
@@ -0,0 +1,20 @@
+data <- tempfile("input.txt")
+write(matrix(paste0("L", 1:100), ncol = 1), data )
+con <- file(description=data, open="r")
+
+from <- 4
+to <- 12
+counter <- 0
+
+
+while (length(oneLine <- readLines(con, n = 1, warn = FALSE)) > 0)
+{
+ counter = counter + 1
+
+ if (counter >= from && counter <= to )
+ {
+ print(oneLine)
+ }
+}
+unlink(data)
+# rm(data, con, from, to, counter)