aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-11-08 18:07:00 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-11-08 18:07:00 +0000
commit75d7dc93b1629888da13f19e1b58657d407f1c13 (patch)
tree13320133cf078df12803a3b7aa554ae4a3c21acf
parentdfcf558b2190d45a5ebf3c486a7c316dbc3e197e (diff)
downloadperlweeklychallenge-club-75d7dc93b1629888da13f19e1b58657d407f1c13.tar.gz
perlweeklychallenge-club-75d7dc93b1629888da13f19e1b58657d407f1c13.tar.bz2
perlweeklychallenge-club-75d7dc93b1629888da13f19e1b58657d407f1c13.zip
Add Perl solution to challenge 138
-rw-r--r--challenge-138/paulo-custodio/perl/ch-1.pl34
-rw-r--r--challenge-138/paulo-custodio/perl/ch-2.pl62
-rw-r--r--challenge-138/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-138/paulo-custodio/t/test-2.yaml15
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