diff options
| author | Simon Green <mail@simon.green> | 2021-11-10 04:12:58 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2021-11-10 04:12:58 +1100 |
| commit | ee6c04acbab562b8135146416db74703d5bd2cb6 (patch) | |
| tree | 0937a35739e73f403ba00043607e8b99e7f07757 | |
| parent | 85079d2c7bd184953301ee9d0c1a22520141e7af (diff) | |
| download | perlweeklychallenge-club-ee6c04acbab562b8135146416db74703d5bd2cb6.tar.gz perlweeklychallenge-club-ee6c04acbab562b8135146416db74703d5bd2cb6.tar.bz2 perlweeklychallenge-club-ee6c04acbab562b8135146416db74703d5bd2cb6.zip | |
sgreen solutions to challenge 138
| -rw-r--r-- | challenge-138/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-138/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-138/sgreen/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-138/sgreen/perl/ch-2.pl | 43 |
4 files changed, 76 insertions, 2 deletions
diff --git a/challenge-138/sgreen/README.md b/challenge-138/sgreen/README.md index 413edf3cdd..9d6781fb66 100644 --- a/challenge-138/sgreen/README.md +++ b/challenge-138/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 137 +# The Weekly Challenge 138 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-137-3jon) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-138-dh1) diff --git a/challenge-138/sgreen/blog.txt b/challenge-138/sgreen/blog.txt new file mode 100644 index 0000000000..2c44b95f2a --- /dev/null +++ b/challenge-138/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-138-dh1 diff --git a/challenge-138/sgreen/perl/ch-1.pl b/challenge-138/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..3eaf027be6 --- /dev/null +++ b/challenge-138/sgreen/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +use Date::Calc ( 'Day_of_Week', 'leap_year' ); + +sub main { + my $year = shift; + + # Sanity check + die "You must specify the year" unless defined $year; + die "Year must be four digits > 1752" unless $year =~ /^[0-9]{4}$/ and $year > 1752; + + # Get the day of week of January 1st, and leap year flag + my $day_of_week = Day_of_Week( $year, 1, 1 ); # 1 - Monday, 7 - Sunday + my $leap = leap_year($year); + + # If the 1st of January is Saturday or Sunday, then the Dec 31st (or + # Dec 30th in a leap year) isn't a work day. + my $workdays = ( $day_of_week != 6 and $day_of_week != 7 ) ? 261 : 260; + + # In a leap year, the Dec 31st is two days later than Jan 1st. + ++$workdays if $leap and $day_of_week != 5 and $day_of_week != 6; + + say $workdays; +} + +main(@ARGV); diff --git a/challenge-138/sgreen/perl/ch-2.pl b/challenge-138/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..5a134362ac --- /dev/null +++ b/challenge-138/sgreen/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +use List::Util 'sum'; + +sub _can_split { + my ( $target, $used, $pool ) = @_; + + # The first number cannot be all digits + my $max = $#$used == -1 ? length($pool) - 1 : length($pool); + + foreach my $l ( 1 .. $max ) { + if ( $l == length($pool) ) { + return 1 if sum( @$used, $pool ) == $target; + } + else { + # We can split the number + my $found = _can_split( $target, [ @$used, substr( $pool, 0, $l ) ], substr( $pool, $l ) ); + return 1 if $found; + } + } + + return 0; +} + +sub main { + my $n = shift; + + # Sanity check + die "You must specify a number\n" unless defined $n; + die "The input doesn't look like a number\n" unless $n =~ /^[1-9][0-9]*$/; + + # Check the number is a perfect square + my $s = sqrt($n); + die "The number isn't a perfect sqaure\n" unless $s == int($s); + + say _can_split( $s, [], $n ); +} + +main(@ARGV); |
