aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-04-05 16:37:11 +0100
committerGitHub <noreply@github.com>2022-04-05 16:37:11 +0100
commitbb4e0f00f0d939b78352ffb3428c0b35ea3249d9 (patch)
treed276c6e0e853ef91b2a4824f2c0042dc97e96370
parent3b548b1e84f1e0b43b74d41481a661f7508373dd (diff)
parent35b8172b1d093b06bb78b388e5e32b3c71be5000 (diff)
downloadperlweeklychallenge-club-bb4e0f00f0d939b78352ffb3428c0b35ea3249d9.tar.gz
perlweeklychallenge-club-bb4e0f00f0d939b78352ffb3428c0b35ea3249d9.tar.bz2
perlweeklychallenge-club-bb4e0f00f0d939b78352ffb3428c0b35ea3249d9.zip
Merge pull request #5889 from pauloscustodio/master
Add Perl solution to challenge 039
-rw-r--r--challenge-039/paulo-custodio/Makefile2
-rw-r--r--challenge-039/paulo-custodio/README1
-rw-r--r--challenge-039/paulo-custodio/perl/ch-1.pl42
-rw-r--r--challenge-039/paulo-custodio/perl/ch-2.pl27
-rw-r--r--challenge-039/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-039/paulo-custodio/t/test-2.yaml20
6 files changed, 97 insertions, 0 deletions
diff --git a/challenge-039/paulo-custodio/Makefile b/challenge-039/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-039/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-039/paulo-custodio/README b/challenge-039/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-039/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-039/paulo-custodio/perl/ch-1.pl b/challenge-039/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..a402e847ef
--- /dev/null
+++ b/challenge-039/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+
+# Challenge 039
+#
+# TASK #1
+# A guest house had a policy that the light remain ON as long as the at least
+# one guest is in the house. There is guest book which tracks all guest in/out
+# time. Write a script to find out how long in minutes the light were ON.
+# Guest Book
+# 1) Alex IN: 09:10 OUT: 09:45
+# 2) Arnold IN: 09:15 OUT: 09:33
+# 3) Bob IN: 09:22 OUT: 09:55
+# 4) Charlie IN: 09:25 OUT: 10:05
+# 5) Steve IN: 09:33 OUT: 10:01
+# 6) Roger IN: 09:44 OUT: 10:12
+# 7) David IN: 09:57 OUT: 10:23
+# 8) Neil IN: 10:01 OUT: 10:19
+# 9) Chris IN: 10:10 OUT: 11:00
+
+use Modern::Perl;
+use Time::Interval;
+use Date::Parse;
+
+my $min_intervals = coalesce([
+ ['09:10', '09:45'],
+ ['09:15', '09:33'],
+ ['09:22', '09:55'],
+ ['09:25', '10:05'],
+ ['09:33', '10:01'],
+ ['09:44', '10:12'],
+ ['09:57', '10:23'],
+ ['10:01', '10:19'],
+ ['10:10', '11:00'],
+]);
+
+my $minutes = 0;
+for (@$min_intervals) {
+ my $s = str2time($_->[0])/60;
+ my $e = str2time($_->[1])/60;
+ $minutes += $e-$s;
+}
+say $minutes;
diff --git a/challenge-039/paulo-custodio/perl/ch-2.pl b/challenge-039/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..86f2877b47
--- /dev/null
+++ b/challenge-039/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+
+# Challenge 039
+#
+# TASK #2
+# Contributed by Andrezgz
+# Write a script to demonstrate Reverse Polish notation(RPN). Checkout the wiki
+# page for more information about RPN.
+
+use Modern::Perl;
+
+# simple rpn calculator
+my @stack;
+my %dispatch = (
+ '+' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a+$b; },
+ '-' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a-$b; },
+ '*' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a*$b; },
+ '/' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a/$b; },
+ '.' => sub { say pop @stack; },
+);
+
+for (split //, "@ARGV") {
+ if (/\s/) {}
+ elsif (/\d/) { push @stack, $_; }
+ elsif (exists $dispatch{$_}) { $dispatch{$_}->(); }
+ else { die "invalid operation: $_"; }
+}
diff --git a/challenge-039/paulo-custodio/t/test-1.yaml b/challenge-039/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..f3c968762c
--- /dev/null
+++ b/challenge-039/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: 110
diff --git a/challenge-039/paulo-custodio/t/test-2.yaml b/challenge-039/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..746bd8389b
--- /dev/null
+++ b/challenge-039/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 1 3 + .
+ input:
+ output: 4
+- setup:
+ cleanup:
+ args: 1 3 - .
+ input:
+ output: -2
+- setup:
+ cleanup:
+ args: '"3 2 * ."'
+ input:
+ output: 6
+- setup:
+ cleanup:
+ args: 3 2 / .
+ input:
+ output: 1.5