From 23760bf20d51d10cc9db1fcc4f7469b9bb460562 Mon Sep 17 00:00:00 2001 From: wanderdoc Date: Sun, 14 Nov 2021 16:05:26 +0100 Subject: Solutions to challenge-138 --- challenge-138/wanderdoc/perl/ch-1.pl | 43 ++++++++++++++++++++++++++++ challenge-138/wanderdoc/perl/ch-2.pl | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 challenge-138/wanderdoc/perl/ch-1.pl create mode 100644 challenge-138/wanderdoc/perl/ch-2.pl diff --git a/challenge-138/wanderdoc/perl/ch-1.pl b/challenge-138/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..ff56059fb9 --- /dev/null +++ b/challenge-138/wanderdoc/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a year, $year in 4-digits form. Write a script to calculate the total number of workdays in the given year. +For the task, we consider, Monday - Friday as workdays. +Example 1 Input: $year = 2021 Output: 261 +Example 2 Input: $year = 2020 Output: 262 +=cut + + + + + + + + +use Time::Piece; +use Time::Seconds; +use Test::More; + + + + + +sub workdays +{ + my $year = $_[0]; + my $workdays; + my $day = Time::Piece->strptime("${year}0101", "%Y%m%D"); + while ( $day->year == $year ) + { + $workdays++ unless ( $day->wday == 1 or $day->wday == 7 ); + $day += ONE_DAY; + } + return $workdays; +} + + +is(workdays(2020), 262, 'Test 2020'); +is(workdays(2021), 261, 'Test 2021'); +done_testing(); \ No newline at end of file diff --git a/challenge-138/wanderdoc/perl/ch-2.pl b/challenge-138/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..fbf9a37d8d --- /dev/null +++ b/challenge-138/wanderdoc/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a perfect square. Write a script to figure out if the square root the given number is same as sum of 2 or more splits of the given number. +Example 1 Input: $n = 81 Output: 1 Since, sqrt(81) = 8 + 1 +Example 2 Input: $n = 9801 Output: 1 Since, sqrt(9801) = 98 + 0 + 1 +Example 3 Input: $n = 36 Output: 0 Since, sqrt(36) != 3 + 6 +=cut + + + + + + + + +use Algorithm::Combinatorics qw(partitions); +use List::Util qw(first sum reduce); +use Test::More; + +sub split_number +{ + my $num = $_[0]; + + my $sqrt = sqrt($num); + die "Error: $num is not a perfect square.$/" unless $sqrt == int($sqrt); + my @digits = split(//, $num); + my $iter = partitions(\@digits); + while (my $p = $iter->next) + { + my @parts = map join('', @$_), @$p; + next if scalar @parts == 1; + next if join('', @parts) != $num; + next if first { /^0{2,}$|^0+[^0]+$/ } @parts; # ? + next if first { $_ > $sqrt } @parts; + next if substr(sum( map { substr($_, -1, 1) } @parts ), -1, 1) != substr($sqrt, -1, 1); + + + if ( sum(@parts) == $sqrt ) + { + print "@parts$/"; + return 1; + } + } + return 0; +} + +is(split_number(81), 1, 'Test 81'); +is(split_number(9801), 1, 'Test 9801'); +is(split_number(36), 0, 'Test 36'); +is(split_number(999998000001), 1, 'Test 999998000001'); +is(split_number(15241383936), 0, 'Test 15241383936'); +done_testing(); \ No newline at end of file -- cgit