diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-08 19:29:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-08 19:29:16 +0000 |
| commit | 10d4eb7921126b96a73490987cf1b381cd8cd3a5 (patch) | |
| tree | 0dbb68d627003ae11b40c01331e888ecae057ad4 | |
| parent | c00d44784aca4a276f7629a9ca2c733bfd68f944 (diff) | |
| parent | 75d7dc93b1629888da13f19e1b58657d407f1c13 (diff) | |
| download | perlweeklychallenge-club-10d4eb7921126b96a73490987cf1b381cd8cd3a5.tar.gz perlweeklychallenge-club-10d4eb7921126b96a73490987cf1b381cd8cd3a5.tar.bz2 perlweeklychallenge-club-10d4eb7921126b96a73490987cf1b381cd8cd3a5.zip | |
Merge pull request #5183 from pauloscustodio/devel
Add Perl solution to challenge 138
| -rw-r--r-- | challenge-138/paulo-custodio/perl/ch-1.pl | 34 | ||||
| -rw-r--r-- | challenge-138/paulo-custodio/perl/ch-2.pl | 62 | ||||
| -rw-r--r-- | challenge-138/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-138/paulo-custodio/t/test-2.yaml | 15 |
4 files changed, 121 insertions, 0 deletions
diff --git a/challenge-138/paulo-custodio/perl/ch-1.pl b/challenge-138/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..37eb05d527 --- /dev/null +++ b/challenge-138/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +# 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. +# +# Example 1 +# Input: $year = 2021 +# Output: 261 +# Example 2 +# Input: $year = 2020 +# Output: 262 + +use Modern::Perl; +use DateTime; + +my $year = shift||DateTime->today()->year(); +say count_work_days($year); + +sub count_work_days { + my($year) = @_; + my $count = 0; + my $date = DateTime->new(year=>$year, month=>1, day=>1); + while ($date->year == $year) { + my $dow = $date->dow; # 1-monday..7-sunday + $count++ if $dow<6; + $date->add(days=>1); + } + return $count; +} diff --git a/challenge-138/paulo-custodio/perl/ch-2.pl b/challenge-138/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..c64a445618 --- /dev/null +++ b/challenge-138/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl + +# 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. +# +# 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 + +use Modern::Perl; +use List::Util 'sum'; + +my $n = shift||1; +say sqroot_is_sum_splits($n); + +sub num_splits { + my($n) = @_; + my @splits; + add_splits(\@splits, [], $n); + return @splits; +} + +sub add_splits { + my($splits, $path, $rest) = @_; + if ($rest eq '') { + push @$splits, [@$path]; + } + else { + for my $i (1..length($rest)) { + my $a = substr($rest, 0, $i); + my $b = substr($rest, $i); + add_splits($splits, [@$path, $a], $b); + } + } +} + +sub sqroot_is_sum_splits { + my($n) = @_; + my $sq = sqrt($n); + return 0 if int($sq) != $sq; # not pefect square + for (num_splits($n)) { + my @split = @$_; + return 1 if sum(@split) == $sq; + } + return 0; +} diff --git a/challenge-138/paulo-custodio/t/test-1.yaml b/challenge-138/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..db0b174101 --- /dev/null +++ b/challenge-138/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 2020 + input: + output: 262 +- setup: + cleanup: + args: 2021 + input: + output: 261 diff --git a/challenge-138/paulo-custodio/t/test-2.yaml b/challenge-138/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..1af15c8d6a --- /dev/null +++ b/challenge-138/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 81 + input: + output: 1 +- setup: + cleanup: + args: 9801 + input: + output: 1 +- setup: + cleanup: + args: 36 + input: + output: 0 |
