From dc51668c420ce3232aa5060aa9b569585ab8974b Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Sun, 14 Nov 2021 23:58:55 +0000 Subject: Task 1 --- challenge-138/perlboy1967/perl/ch-1.pl | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 challenge-138/perlboy1967/perl/ch-1.pl diff --git a/challenge-138/perlboy1967/perl/ch-1.pl b/challenge-138/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..0b8d525455 --- /dev/null +++ b/challenge-138/perlboy1967/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 138 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-138/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +TASK #1 › Workdays +Submitted by: Mohammad S Anwar + +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. + +=cut + +use v5.16; +use strict; +use warnings; + +use Date::Calc qw(Day_of_Week Week_of_Year); + +use Test::More; + +my %tests = ( + 2000 => 260, 2001 => 261, 2002 => 261, 2003 => 261, + 2004 => 262, 2005 => 260, 2006 => 260, 2007 => 261, + 2008 => 262, 2009 => 261, 2010 => 261, 2011 => 260, + 2012 => 261, 2013 => 261, 2014 => 261, 2015 => 261, + 2016 => 261, 2017 => 260, 2018 => 261, 2019 => 261, + 2020 => 262, 2021 => 261, 2022 => 260, 2023 => 260, + 2024 => 262, 2025 => 261, 2026 => 261, 2027 => 261, +); + +foreach my $y (sort { $a <=> $b } keys %tests) { + my ($dayOfWeek1,$dayOfWeek2) = (Day_of_Week($y,1,1),Day_of_Week($y,12,31)); + + my $workingDays = 262; + $workingDays-- if ($dayOfWeek1 > 5); + $workingDays-- if ($dayOfWeek2 > 5); + $workingDays-- if ($dayOfWeek1 == $dayOfWeek2 and $dayOfWeek1 < 6); + + is($workingDays,$tests{$y},"year=$y"); +} + +done_testing; -- cgit From 125a2addeb2919841cab724b6bca32accf6d2d52 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 15 Nov 2021 00:13:23 +0000 Subject: Task 1 & 2 --- challenge-138/perlboy1967/perl/ch-2.pl | 79 ++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 challenge-138/perlboy1967/perl/ch-2.pl diff --git a/challenge-138/perlboy1967/perl/ch-2.pl b/challenge-138/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..4cbf782274 --- /dev/null +++ b/challenge-138/perlboy1967/perl/ch-2.pl @@ -0,0 +1,79 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 138 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-138/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +TASK #2 › Split Number +Submitted by: Mohammad S Anwar + +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. + +=cut + +use v5.16; +use strict; +use warnings; + +use List::Util qw(sum); + +use Test::More; +use Test::Deep qw(cmp_deeply); + +# Protoptype(s) +sub isSplitNumber($); +sub _isSN($$\@); + +my %tests = ( + 25 => [0,[]], + 36 => [0,[]], + 81 => [1,[8,1]], + 9801 => [1,[98,0,1]], +); + +foreach my $n (sort { $a <=> $b } keys %tests) { + cmp_deeply(isSplitNumber($n),$tests{$n},"n=$n"); +} + +done_testing; + +sub isSplitNumber($) { + my ($n) = @_; + + my $iSqrt = int(sqrt($n)); + return [0,[]] if ($iSqrt != sqrt($n)); + + for my $i (1 .. length($n) - 1) { + my $j = $n; + my $r = substr($j,0,$i,''); + my @return = ($r); + if (_isSN($iSqrt,$j,@return)) { + return [1, [@return]]; + } + } + + return [0,[]]; +} + +sub _isSN ($$\@) { + my ($s,$n,$ar) = @_; + + if ($n !~ m#^0# && sum(@$ar,$n) == $s) { + push(@$ar,$n); + return 1; + } else { + for my $i (1 .. length($n) - 1) { + my $j = $n; + my $r = substr($j,0,$i,''); + push(@$ar,$r); + return 1 if _isSN($s,$j,@$ar); + pop(@$ar); + } + } +} + -- cgit From f1eac0a1b6e9fd1cdf9d84728904a9dbc97d3e4a Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 15 Nov 2021 00:22:09 +0000 Subject: Task 2 - More 'working' numbers --- challenge-138/perlboy1967/perl/ch-2.pl | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/challenge-138/perlboy1967/perl/ch-2.pl b/challenge-138/perlboy1967/perl/ch-2.pl index 4cbf782274..46af363c10 100755 --- a/challenge-138/perlboy1967/perl/ch-2.pl +++ b/challenge-138/perlboy1967/perl/ch-2.pl @@ -32,8 +32,15 @@ sub _isSN($$\@); my %tests = ( 25 => [0,[]], 36 => [0,[]], - 81 => [1,[8,1]], - 9801 => [1,[98,0,1]], + 81 => [1,[8,1]], + 1296 => [1,[1,29,6]], + 2025 => [1,[20,25]], + 3025 => [1,[30,25]], + 6724 => [1,[6,72,4]], + 8281 => [1,[8,2,81]], + 9801 => [1,[98,0,1]], + 55225 => [1,[5,5,225]], + 88209 => [1,[88,209]], ); foreach my $n (sort { $a <=> $b } keys %tests) { -- cgit