From 35b8172b1d093b06bb78b388e5e32b3c71be5000 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 5 Apr 2022 15:28:06 +0100 Subject: Add Perl solution to challenge 039 --- challenge-039/paulo-custodio/Makefile | 2 ++ challenge-039/paulo-custodio/README | 1 + challenge-039/paulo-custodio/perl/ch-1.pl | 42 ++++++++++++++++++++++++++++++ challenge-039/paulo-custodio/perl/ch-2.pl | 27 +++++++++++++++++++ challenge-039/paulo-custodio/t/test-1.yaml | 5 ++++ challenge-039/paulo-custodio/t/test-2.yaml | 20 ++++++++++++++ 6 files changed, 97 insertions(+) create mode 100644 challenge-039/paulo-custodio/Makefile create mode 100644 challenge-039/paulo-custodio/README create mode 100644 challenge-039/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-039/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-039/paulo-custodio/t/test-1.yaml create mode 100644 challenge-039/paulo-custodio/t/test-2.yaml 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 -- cgit