aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-178/paulo-custodio/Makefile2
-rw-r--r--challenge-178/paulo-custodio/perl/ch-1.pl54
-rw-r--r--challenge-178/paulo-custodio/perl/ch-2.pl47
-rw-r--r--challenge-178/paulo-custodio/t/test-1.yaml8
-rw-r--r--challenge-178/paulo-custodio/t/test-2.yaml10
5 files changed, 121 insertions, 0 deletions
diff --git a/challenge-178/paulo-custodio/Makefile b/challenge-178/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-178/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-178/paulo-custodio/perl/ch-1.pl b/challenge-178/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..75bbc20fcd
--- /dev/null
+++ b/challenge-178/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+
+# Challenge 178
+#
+# Task 1: Quater-imaginary Base
+# Submitted by: Mohammad S Anwar
+#
+# Write a script to convert a given number (base 10) to quater-imaginary base
+# number and vice-versa. For more informations, please checkout wiki page.
+#
+# For example,
+#
+# $number_base_10 = 4
+# $number_quater_imaginary_base = 10300
+
+use Modern::Perl;
+use Test::More;
+
+sub dec_to_base2i {
+ my($n) = @_;
+ my $base = -4;
+ my $result = "";
+ while ($n != 0) {
+ my $i = $n % $base;
+ $n /= $base;
+ if ($i < 0) {
+ $i += abs($base);
+ $n++;
+ }
+ $result = "0".$i.$result; # imaginary is zero
+ }
+ $result =~ s/^0+//;
+ return $result;
+}
+
+sub base2i_to_dec {
+ my($n2i) = @_;
+ my $base = -4;
+ my $result = 0;
+ my @digits = reverse split //, $n2i;
+ for my $i (0..$#digits) {
+ if ($i % 2 == 0) { # real part
+ $result += $digits[$i] * $base ** ($i/2);
+ }
+ elsif ($digits[$i] != 0) { # imaginary part
+ die "number $n2i has imaginary component";
+ }
+ }
+ return $result;
+}
+
+is dec_to_base2i(4), 10300;
+is base2i_to_dec(10300), 4;
+done_testing;
diff --git a/challenge-178/paulo-custodio/perl/ch-2.pl b/challenge-178/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..6e3e10ce37
--- /dev/null
+++ b/challenge-178/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/perl
+
+# Challenge 178
+#
+# Task 2: Business Date
+# Submitted by: Mohammad S Anwar
+#
+# You are given $timestamp (date with time) and $duration in hours.
+#
+# Write a script to find the time that occurs $duration business hours after
+# $timestamp. For the sake of this task, let us assume the working hours is 9am
+# to 6pm, Monday to Friday. Please ignore timezone too.
+#
+# For example,
+#
+# Suppose the given timestamp is 2022-08-01 10:30 and the duration is 4 hours.
+# Then the next business date would be 2022-08-01 14:30.
+#
+# Similar if the given timestamp is 2022-08-01 17:00 and the duration is 3.5 hours.
+# Then the next business date would be 2022-08-02 11:30.
+
+use Modern::Perl;
+use DateTime;
+
+sub next_business_date {
+ my($date_text, $time_text, $hours) = @_;
+ "$date_text $time_text" =~ /^(\d+)-(\d+)-(\d+) (\d+):(\d+)$/
+ or die "invalid date";
+ my $date = DateTime->new(year=>$1, month=>$2, day=>$3, hour=>$4, minute=>$5);
+ my $minutes = $hours*60;
+ while (1) {
+ (my $end_day = $date->clone)->set(hour=>18, minute=>0);
+ my $remain = $end_day->subtract_datetime($date)->in_units('minutes');
+ if ($remain > $minutes) {
+ $date->add(minutes=>$minutes);
+ return $date;
+ }
+ else {
+ $minutes -= $remain;
+ ($date = $end_day)->add(hours=>(24-18)+9);
+ $date->add(days=>1) while $date->day_of_week >= 6; # skip weekend
+ }
+ }
+}
+
+@ARGV==3 or die "usage: ch-2.pl yyyy-mm-dd HH:MM hours\n";
+say next_business_date(@ARGV)->strftime("%Y-%m-%d %H:%M");
diff --git a/challenge-178/paulo-custodio/t/test-1.yaml b/challenge-178/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..5c2d74364b
--- /dev/null
+++ b/challenge-178/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,8 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: |
+ |ok 1
+ |ok 2
+ |1..2
diff --git a/challenge-178/paulo-custodio/t/test-2.yaml b/challenge-178/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..46b4e6467c
--- /dev/null
+++ b/challenge-178/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 2022-08-01 10:30 4
+ input:
+ output: 2022-08-01 14:30
+- setup:
+ cleanup:
+ args: 2022-08-01 17:00 3.5
+ input:
+ output: 2022-08-02 11:30