diff options
| author | Simon Green <mail@simon.green> | 2021-03-04 22:20:15 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2021-03-04 22:20:15 +1000 |
| commit | 714f4b0fdf3eb48eafd1ed22dcb6a8be26b69827 (patch) | |
| tree | 8d4071533f17bc7c322cc19c0bda7a37aa34591e | |
| parent | beb85731c96259b8a964daa71e9ec5eb7e6a047d (diff) | |
| download | perlweeklychallenge-club-714f4b0fdf3eb48eafd1ed22dcb6a8be26b69827.tar.gz perlweeklychallenge-club-714f4b0fdf3eb48eafd1ed22dcb6a8be26b69827.tar.bz2 perlweeklychallenge-club-714f4b0fdf3eb48eafd1ed22dcb6a8be26b69827.zip | |
sgreen solution to challenge 102
| -rw-r--r-- | challenge-102/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-102/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-102/sgreen/perl/ch-1.pl | 39 | ||||
| -rwxr-xr-x | challenge-102/sgreen/perl/ch-2.pl | 26 |
4 files changed, 68 insertions, 2 deletions
diff --git a/challenge-102/sgreen/README.md b/challenge-102/sgreen/README.md index 92b3149288..6fb5a4942c 100644 --- a/challenge-102/sgreen/README.md +++ b/challenge-102/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 101 +# The Weekly Challenge 102 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-101-jeo) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-102-57jb) diff --git a/challenge-102/sgreen/blog.txt b/challenge-102/sgreen/blog.txt new file mode 100644 index 0000000000..b2ec98e7ea --- /dev/null +++ b/challenge-102/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-102-57jb diff --git a/challenge-102/sgreen/perl/ch-1.pl b/challenge-102/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..01611cfee7 --- /dev/null +++ b/challenge-102/sgreen/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub main { + my $length = shift; + + # Sanity check + die "You must specify a number\n" if not defined $length; + die "The number is not a positive integer\n" unless $length =~ /^[1-9][0-9]*$/; + + # Build a hash table with all the possible squares + my %squares = (); + my $counter = 0; + while ( $counter**2 < 2 * 10**$length ) { + ++$counter; + $squares{ $counter**2 } = $counter; + } + + my @solutions = (); + my $r = 10**( $length - 1 ); + while ( $r < 10**$length ) { + my $r1 = reverse($r); + push @solutions, $r if ( $r >= $r1 and $squares{ $r - $r1 } and $squares{ $r + $r1 } ); + ++$r; + } + + if ( scalar(@solutions) ) { + say join ' ', @solutions; + } + else { + say 'No solutions found'; + } +} + +main(@ARGV); + diff --git a/challenge-102/sgreen/perl/ch-2.pl b/challenge-102/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..bb68d3ab49 --- /dev/null +++ b/challenge-102/sgreen/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub main { + my $length = shift; + + # Sanity check + die "You must specify a number\n" if not defined $length; + die "The number is not a positive integer\n" unless $length =~ /^[1-9][0-9]*$/; + + my $string = '#' x $length; + my $position = $length - 1; # The last character + + while ( $position - length( $position + 1 ) >= 0 ) { + my $char = length( $position + 1 ); + substr( $string, $position - $char, $char, $position + 1 ); + $position -= ( $char + 1 ); + } + + say $string; +} + +main(@ARGV); |
