From 8aeb4e2c74c70428cc82c52bb94c08a2f48dcd4e Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 4 Aug 2020 19:28:06 +0100 Subject: - Added solutions by Wanderdoc. --- challenge-072/wanderdoc/Perl/ch-1.pl | 56 ------------------------------------ challenge-072/wanderdoc/Perl/ch-2.pl | 50 -------------------------------- challenge-072/wanderdoc/perl/ch-1.pl | 56 ++++++++++++++++++++++++++++++++++++ challenge-072/wanderdoc/perl/ch-2.pl | 50 ++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 106 deletions(-) delete mode 100644 challenge-072/wanderdoc/Perl/ch-1.pl delete mode 100644 challenge-072/wanderdoc/Perl/ch-2.pl create mode 100644 challenge-072/wanderdoc/perl/ch-1.pl create mode 100644 challenge-072/wanderdoc/perl/ch-2.pl (limited to 'challenge-072') diff --git a/challenge-072/wanderdoc/Perl/ch-1.pl b/challenge-072/wanderdoc/Perl/ch-1.pl deleted file mode 100644 index 3cc64884e6..0000000000 --- a/challenge-072/wanderdoc/Perl/ch-1.pl +++ /dev/null @@ -1,56 +0,0 @@ -#!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 deleted file mode 100644 index 1885e99fa5..0000000000 --- a/challenge-072/wanderdoc/Perl/ch-2.pl +++ /dev/null @@ -1,50 +0,0 @@ -#!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/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 -- cgit