aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-12-23 19:04:49 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-12-23 19:04:49 +0000
commitb1b9d5071aa120c8594bb2feaa37ac3750c8a052 (patch)
tree34cca21f9ff504c644bdd191de14eac7563c4fe3
parent66708f6ceee6a32096961f5172b5bb204e98db21 (diff)
downloadperlweeklychallenge-club-b1b9d5071aa120c8594bb2feaa37ac3750c8a052.tar.gz
perlweeklychallenge-club-b1b9d5071aa120c8594bb2feaa37ac3750c8a052.tar.bz2
perlweeklychallenge-club-b1b9d5071aa120c8594bb2feaa37ac3750c8a052.zip
- Updated solutions by Adam Russell.
-rw-r--r--challenge-039/adam-russell/perl5/Rpn.yp2
-rw-r--r--challenge-039/adam-russell/perl5/ch-1.pl43
-rw-r--r--challenge-039/adam-russell/perl5/ch-2.pl4
3 files changed, 25 insertions, 24 deletions
diff --git a/challenge-039/adam-russell/perl5/Rpn.yp b/challenge-039/adam-russell/perl5/Rpn.yp
index d27d0b7bcd..d61d952b40 100644
--- a/challenge-039/adam-russell/perl5/Rpn.yp
+++ b/challenge-039/adam-russell/perl5/Rpn.yp
@@ -5,7 +5,7 @@ line: '\n' | rpn '\n' {print $_[1] . "\n"}
;
rpn: NUMBER
- | rpn rpn '+' { $_[1] + $_[2]}
+ | rpn rpn '+' {$_[1] + $_[2]}
| rpn rpn '-' {$_[1] - $_[2]}
| rpn rpn '*' {$_[1] * $_[2]}
| rpn rpn '/' {$_[1] / $_[2]}
diff --git a/challenge-039/adam-russell/perl5/ch-1.pl b/challenge-039/adam-russell/perl5/ch-1.pl
index 861fccd157..027500c438 100644
--- a/challenge-039/adam-russell/perl5/ch-1.pl
+++ b/challenge-039/adam-russell/perl5/ch-1.pl
@@ -6,13 +6,14 @@ use warnings;
# is guest book which tracks all guest in/out time.
# Write a script to find out how long in minutes the light were ON.
##
+use DateTime;
use DateTime::Duration;
my $lights_on = new DateTime::Duration(
hours => 0,
minutes => 0,
seconds => 0
);
-my $start_time;
+my $start_time;
while(my $line = <DATA>){
chomp($line);
@@ -22,31 +23,29 @@ while(my $line = <DATA>){
$in_minute =~ tr/ OUT//d;
$out_hour =~ tr/ //d;
if(!$start_time){
- $start_time = new DateTime::Duration(
- hours => $in_hour,
- minutes => $in_minute,
- seconds => 0
- );
+ $start_time = DateTime -> now();
+ $start_time->set_hour($in_hour);
+ $start_time->set_minute($in_minute);
+ $start_time->set_second(0);
}
- my $in = new DateTime::Duration(
- hours => $in_hour,
- minutes => $in_minute,
- seconds => 0
- );
- my $out = new DateTime::Duration(
- hours => $out_hour,
- minutes => $out_minute,
- seconds => 0
- );
- if(DateTime::Duration->compare($start_time, $in) <= 0){
- $lights_on -> add_duration($out->subtract_duration($in));
+
+ my $in = DateTime -> now();
+ $in->set_hour($in_hour);
+ $in->set_minute($in_minute);
+ $in->set_second(0);
+ my $out = DateTime -> now();
+ $out->set_hour($out_hour);
+ $out->set_minute($out_minute);
+ $out->set_second(0);
+ if(DateTime->compare($start_time, $in) <= 0){
+ $lights_on -> add_duration($out->subtract_datetime($in));
}
- if(DateTime::Duration->compare($start_time, $in) > 0 &&
- DateTime::Duration->compare($start_time, $out) < 0
+ if(DateTime->compare($start_time, $in) > 0 &&
+ DateTime->compare($start_time, $out) < 0
){
- $lights_on -> add_duration($out->subtract_duration($start_time));
+ $lights_on -> add_duration($out->subtract_datetime($start_time));
}
- $start_time = $out;
+ $start_time = $out if (DateTime->compare($start_time, $out) < 0);
}
print $lights_on->hours() . " hours ";
print $lights_on->minutes() . " minutes\n";
diff --git a/challenge-039/adam-russell/perl5/ch-2.pl b/challenge-039/adam-russell/perl5/ch-2.pl
index 52d9a91e10..f1c333496e 100644
--- a/challenge-039/adam-russell/perl5/ch-2.pl
+++ b/challenge-039/adam-russell/perl5/ch-2.pl
@@ -8,11 +8,13 @@ use constant RPN_ADD => "10 8 + \n";
use constant RPN_SUBTRACT => "18 66 - \n";
use constant RPN_MULTIPLY => "10 8 * \n";
use constant RPN_DIVIDE => "52 2 / \n";
+use constant RPN => "2 2 + 3 3 + + 1 7 - + \n";
MAIN:{
my $parser = new Rpn();
$parser->parse(RPN_ADD);
$parser->parse(RPN_SUBTRACT);
$parser->parse(RPN_MULTIPLY);
- $parser->parse(RPN_DIVIDE);
+ $parser->parse(RPN_DIVIDE);
+ $parser->parse(RPN);
}