diff options
| author | Simon Green <mail@simon.green> | 2020-08-09 21:22:13 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2020-08-09 21:22:13 +1000 |
| commit | ef5d50a6ba38f1fe9e6acf66df2bba796c171cf5 (patch) | |
| tree | 2d36b308afa94792b7c79bb9b4964e5947808166 /challenge-072 | |
| parent | 7f3dde6f89d1d0df575807e0ec56a07a191cd7bb (diff) | |
| download | perlweeklychallenge-club-ef5d50a6ba38f1fe9e6acf66df2bba796c171cf5.tar.gz perlweeklychallenge-club-ef5d50a6ba38f1fe9e6acf66df2bba796c171cf5.tar.bz2 perlweeklychallenge-club-ef5d50a6ba38f1fe9e6acf66df2bba796c171cf5.zip | |
Simon's solution to challenge 72
Diffstat (limited to 'challenge-072')
| -rw-r--r-- | challenge-072/sgreen/README.md | 40 | ||||
| -rw-r--r-- | challenge-072/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-072/sgreen/perl/ch-1.pl | 20 | ||||
| -rwxr-xr-x | challenge-072/sgreen/perl/ch-2.pl | 38 |
4 files changed, 85 insertions, 14 deletions
diff --git a/challenge-072/sgreen/README.md b/challenge-072/sgreen/README.md index 0f43e97b94..6dec32d966 100644 --- a/challenge-072/sgreen/README.md +++ b/challenge-072/sgreen/README.md @@ -1,27 +1,39 @@ -# Perl Weekly Challenge 070 +# Perl Weekly Challenge 072 Solution by Simon Green. - -## TASK #1 › Character Swapping +No solutions for last week as I was moving interstate. I'm back on board this week :) -For this I used the fact that `($a, $b) = ($b, a)` does what it should. Therefore it was a simple exercise of doing this for `$C` times, and then display the output. +## TASK #1 › Trailing Zeroes -The task states that `$C + $O <= $O`. This however is not possible. When `$C + $O = $N`, you will get a `substr outside of string` error. For this reason, I've only allowed `$C + $O < $N` to be valid. +When you think about it, a trailing zero is a product of 10, which is 2 × 5. Forgetting about the twos (since every second number is even), the five occurs every 5 numbers. Thus the factorial from 1 to 4 will contain no trailing zeros, 5-9 one trailing zero, 10-14 three trailing zeros, and so on. Once you reach 25, it gets a little more complicated as 25 is 5 × 5. Anyway, I digress from the actual task. -### Example +For this task, I take the input, calculate the factorial value, I then use a regular expression to find the trailing zeros (if any), and then display it. - » ./ch-1.pl perlandraku 3 4 - pndraerlaku +### Examples - » perl/ch-1.pl helloworld 8 1 - hlloworlde + » ./ch-1.pl 10 + 2 -## TASK 2 › Gray Code Sequence + » ./ch-1.pl 7 + 1 -Not really much to say about this task, as it is string forward. Given that `$N` can not be greater than five, I simply stored the array in memory. Even at 24 bits, it will run without any issues. + » ./ch-1.pl 4 + 0 + +## TASK 2 › Lines Range + +This tasks was relatively simple. Read the file, skip the lines < `$A` and exit the loop once line `$B` is reached (or the end of file). ## Example - » ./ch-2.pl 4 - 0 1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 + » ./ch-2.pl input.txt 4 12 + L4 + L5 + L6 + L7 + L8 + L9 + L10 + L11 + L12
\ No newline at end of file diff --git a/challenge-072/sgreen/blog.txt b/challenge-072/sgreen/blog.txt new file mode 100644 index 0000000000..71fedf0051 --- /dev/null +++ b/challenge-072/sgreen/blog.txt @@ -0,0 +1 @@ +https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-072/sgreen/README.md diff --git a/challenge-072/sgreen/perl/ch-1.pl b/challenge-072/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..a3836ffa0a --- /dev/null +++ b/challenge-072/sgreen/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use 5.10.1; + +sub main (@) { + my $N = shift; + + # Calculate N! + my $total = 1; + $total *= $_ for ( 1 .. $N ); + + # Strip out everything except the last zeros (if any) + # And display the length of the string + $total =~ /^.*?(0*)$/; + say length($1); +} + +main(@ARGV); diff --git a/challenge-072/sgreen/perl/ch-2.pl b/challenge-072/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..37c5060596 --- /dev/null +++ b/challenge-072/sgreen/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use 5.10.1; + +sub main (@) { + my ( $file, $start, $end ) = @_; + + # Sanity check + die "File '$file' does not exist or is not readable\n" unless -r $file; + die "The start value must be a positive number\n" + unless $start =~ /^[0-9]+$/; + die "The end value must be a positive number\n" unless $end =~ /^[0-9]+$/; + die "The start value cannot be greater than the end value\n" + if $start > $end; + + # Open the file + open( my $fh, '<', $file ) or die $!; + + my $cnt = 0; + while ( my $line = <$fh> ) { + ++$cnt; + + # We don't print lines before $start + next if $cnt < $start; + + print $line; + + # Likewise, if we have reached $end, their is nothing more to print + last if $cnt == $end; + } + + close $fh; + +} + +main(@ARGV); |
