diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2021-06-08 16:45:43 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2021-06-08 16:45:43 -0400 |
| commit | d26784f3824077ef37b82dfe08d45cc058b1eeb1 (patch) | |
| tree | 07444240de62e9b5906c011d12a6d47e9e78b680 /challenge-116/dave-jacoby/perl | |
| parent | 4fded3c386bc27c9b7693cb7fd53a86dc2312471 (diff) | |
| download | perlweeklychallenge-club-d26784f3824077ef37b82dfe08d45cc058b1eeb1.tar.gz perlweeklychallenge-club-d26784f3824077ef37b82dfe08d45cc058b1eeb1.tar.bz2 perlweeklychallenge-club-d26784f3824077ef37b82dfe08d45cc058b1eeb1.zip | |
Challenge 116
Diffstat (limited to 'challenge-116/dave-jacoby/perl')
| -rw-r--r-- | challenge-116/dave-jacoby/perl/ch-1.pl | 60 | ||||
| -rw-r--r-- | challenge-116/dave-jacoby/perl/ch-2.pl | 21 |
2 files changed, 81 insertions, 0 deletions
diff --git a/challenge-116/dave-jacoby/perl/ch-1.pl b/challenge-116/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..de98873f64 --- /dev/null +++ b/challenge-116/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ postderef say signatures state }; +no warnings qw{ experimental }; + +my @numbers = qw{ 1234 91011 10203 }; + +for my $n (@numbers) { + say base($n); +} + +# we are asked to return the sequence +# or the given number, and accounting +# for that makes recursion difficult, +# so we pass to base to determine that + +sub base ( $n ) { + my $s = get_sequence($n); + return $s//$n; +} + +# test for success and return if successful +# then add commas within (a copy of) the +# string + +sub get_sequence ( $n ) { + my $t = test($n); + return $n if $t; + + my $output; + my @n = split /,/, $n; + my $flag = 0; + map { $flag += 1 if $_ > 10 } @n; + if ( $flag > 0 ) { + for my $i ( 0 .. length $n ) { + my $cp = $n; + my $l = substr( $cp, $i, 1 ); + substr( $cp, $i, 1 ) = ',' . $l; + next if $cp =~ m{^\,|\,\,|\,$}; + my $x = get_sequence($cp); + return $x if $x; + } + } + return undef; +} + +sub test ( $n ) { + my $t = 1; + my @n = split /,/, $n; + $t = 0 if $n[0] =~ m{^0}mx; + $t = 0 if scalar @n < 2; + for my $i ( 1 .. -1 + scalar @n ) { + my $h = $i - 1; + $t = 0 if $n[$i] =~ m{^0}mx; + $t = 0 unless $n[$h] + 1 == $n[$i]; + } + return $t; +} diff --git a/challenge-116/dave-jacoby/perl/ch-2.pl b/challenge-116/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..af368b69ca --- /dev/null +++ b/challenge-116/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ postderef say signatures state }; +no warnings qw{ experimental }; + +use List::Util qw{sum0}; + +my @numbers = sort (34, 50, 52, 10 ); + +for my $n ( @numbers ) { + my $b = sum_of_squares($n); + say join "\t", $n,$b?'Yes':'No'; +} + +sub sum_of_squares ( $n ) { + my $sum = sum0 map { $_ ** 2 } split //, $n; + my $root = sqrt $sum; + return int $root == $root ? 1 : 0 ; +}
\ No newline at end of file |
