aboutsummaryrefslogtreecommitdiff
path: root/challenge-039
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-12-19 23:38:31 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-12-19 23:38:31 +0000
commit2033c83ccafc6a44d65153209fea390e44200d4d (patch)
tree2f71df1265ec0fa1e2513f689a88ee9bdb18c43f /challenge-039
parent1d488a37468835a79797d8f6c010146d18eed625 (diff)
downloadperlweeklychallenge-club-2033c83ccafc6a44d65153209fea390e44200d4d.tar.gz
perlweeklychallenge-club-2033c83ccafc6a44d65153209fea390e44200d4d.tar.bz2
perlweeklychallenge-club-2033c83ccafc6a44d65153209fea390e44200d4d.zip
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-039')
-rw-r--r--challenge-039/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-039/laurent-rosenfeld/perl5/ch-1.pl39
-rw-r--r--challenge-039/laurent-rosenfeld/perl5/ch-2.pl38
-rw-r--r--challenge-039/laurent-rosenfeld/perl6/ch-1.p637
-rw-r--r--challenge-039/laurent-rosenfeld/perl6/ch-2.p636
5 files changed, 151 insertions, 0 deletions
diff --git a/challenge-039/laurent-rosenfeld/blog.txt b/challenge-039/laurent-rosenfeld/blog.txt
new file mode 100644
index 0000000000..1e29748974
--- /dev/null
+++ b/challenge-039/laurent-rosenfeld/blog.txt
@@ -0,0 +1 @@
+http://blogs.perl.org/users/laurent_r/2019/12/perl-weekly-challenge-39-guest-house-and-reverse-polish-notation.html
diff --git a/challenge-039/laurent-rosenfeld/perl5/ch-1.pl b/challenge-039/laurent-rosenfeld/perl5/ch-1.pl
new file mode 100644
index 0000000000..cf490f2e1b
--- /dev/null
+++ b/challenge-039/laurent-rosenfeld/perl5/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use feature qw/say/;
+
+my %hm;
+for my $hour (0..23) {
+ $hm{$hour}[$_] = 0 for 0..59;
+}
+while (<DATA>) {
+ next unless /\S/;
+ my ($in_h, $in_m, $out_h, $out_m) = /(\d\d):(\d\d)\D+(\d\d):(\d\d)/;
+ if ($out_h eq $in_h) {
+ $hm{0+$in_h}[$_] = 1 for $in_m..$out_m;
+ } else {
+ $hm{0+$in_h}[$_] = 1 for $in_m..59; # end the first hour
+ for my $hour ($in_h + 1 .. $out_h -1) {
+ $hm{$hour}[$_] = 1 for 0..59; # If several hours
+ }
+ $hm{0+$out_h}[$_] = 1 for 0..$out_m; # Complete last hour
+ }
+}
+my $total_on = 0;
+for my $hour (keys %hm) {
+ $total_on += $hm{$hour}[$_] for 0..59;
+}
+say "Total time on: $total_on minutes.";
+
+__DATA__
+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
+10) Liz IN: 12:07 OUT: 17:05
diff --git a/challenge-039/laurent-rosenfeld/perl5/ch-2.pl b/challenge-039/laurent-rosenfeld/perl5/ch-2.pl
new file mode 100644
index 0000000000..2685cb543a
--- /dev/null
+++ b/challenge-039/laurent-rosenfeld/perl5/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use utf8;
+use Test::More tests => 5;
+
+my %operations = (
+ '+' => sub { return $_[0] + $_[1]; },
+ '-' => sub { return $_[0] - $_[1]; }, # hyphen
+ '−' => sub { return $_[0] - $_[1]; }, # minus
+ 'x' => sub { return $_[0] * $_[1]; },
+ '*' => sub { return $_[0] * $_[1]; },
+ '×' => sub { return $_[0] * $_[1]; },
+ '/' => sub { return $_[0] / $_[1]; },
+ '÷' => sub { return $_[0] / $_[1]; },
+);
+
+sub parse_operation {
+ my @stack;
+ for my $token (split /\s+/, shift) {
+ if ($token =~ /^\d+$/) {
+ push @stack, $token ;
+ } elsif (exists $operations{$token}) {
+ return "Invalid RPN expression" if @stack < 2;
+ my $op2 = pop @stack;
+ my $op1 = pop @stack;
+ push @stack, $operations{$token}->($op1, $op2);
+ } else {
+ die "Invalid token $token.";
+ }
+ }
+ return $stack[0]
+}
+is parse_operation("1 2 +"), 3, "2 operands";
+is parse_operation("1 2 + 4 ×"), 12, "3 operands, a Unicode multiplication operator";
+is parse_operation("1 2 + 4 * 5 + 3 -"), 14, "5 operands";
+is parse_operation("3 4 5 x -"), -17, "Start with 3 operands and then two operators";
+is parse_operation("15 7 1 1 + − ÷ 3 × 2 1 1 + + −"), 5, "8 operands, 4 Unicode operators";
diff --git a/challenge-039/laurent-rosenfeld/perl6/ch-1.p6 b/challenge-039/laurent-rosenfeld/perl6/ch-1.p6
new file mode 100644
index 0000000000..6391e678d6
--- /dev/null
+++ b/challenge-039/laurent-rosenfeld/perl6/ch-1.p6
@@ -0,0 +1,37 @@
+use v6;
+
+my $input =
+ "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
+ 10) Liz IN: 12:07 OUT: 17:05";
+
+my %hm;
+for 0..23 -> $hour {
+ %hm{$hour}[$_] = 0 for 0..59;
+}
+for $input.lines {
+ next unless /\S/;
+ my ($in_h, $in_m, $out_h, $out_m) = map { +$_}, $/[0..3] if /(\d\d)':'(\d\d)\D+(\d\d)':'(\d\d)/;
+ if ($out_h == $in_h) {
+ %hm{$in_h}[$_] = 1 for $in_m..$out_m;
+ } else {
+ %hm{$in_h}[$_] = 1 for $in_m..59; # end the first hour
+ for $in_h + 1 .. $out_h -1 -> $hour {
+ %hm{$hour}[$_] = 1 for 0..59; # If several hours
+ }
+ %hm{$out_h}[$_] = 1 for 0..$out_m; # Complete last hour
+ }
+}
+
+my $total_on = 0;
+for keys %hm -> $hour {
+ $total_on += sum %hm{$hour};
+}
+say "Total time on: $total_on minutes.";
diff --git a/challenge-039/laurent-rosenfeld/perl6/ch-2.p6 b/challenge-039/laurent-rosenfeld/perl6/ch-2.p6
new file mode 100644
index 0000000000..2e5068f572
--- /dev/null
+++ b/challenge-039/laurent-rosenfeld/perl6/ch-2.p6
@@ -0,0 +1,36 @@
+use v6;
+use Test;
+
+my %operations = (
+ '+' => { $^a + $^b; },
+ '-' => { $^a - $^b; }, # hyphen
+ '−' => { $^a - $^b; }, # dash
+ 'x' => { $^a * $^b; },
+ '*' => { $^a * $^b; },
+ '×' => { $^a * $^b; },
+ '/' => { $^a / $^b; },
+ '÷' => { $^a / $^b; },
+);
+
+sub parse_operation (Str $expr) {
+ my @stack;
+ for $expr.split(/\s+/) -> $token {
+ if $token ~~ /^ \d+ $/ {
+ push @stack, $token ;
+ } elsif (%operations{$token}:exists) {
+ return "Invalid RPN expression" if @stack.elems < 2;
+ my $op2 = pop @stack;
+ my $op1 = pop @stack;
+ push @stack, %operations{$token}($op1, $op2);
+ } else {
+ die "Invalid token $token.";
+ }
+ }
+ return @stack[0]
+}
+plan 5;
+is parse_operation("1 2 +"), 3, "2 operands";
+is parse_operation("1 2 + 4 ×"), 12, "3 operands, a Unicode multiplication operator";
+is parse_operation("1 2 + 4 * 5 + 3 -"), 14, "5 operands";
+is parse_operation("3 4 5 x -"), -17, "Start with 3 operands and then two operators";
+is parse_operation("15 7 1 1 + − ÷ 3 × 2 1 1 + + −"), 5, "8 operands, 4 Unicode operators";