aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2019-12-22 18:56:56 -0500
committerAdam Russell <ac.russell@live.com>2019-12-22 18:56:56 -0500
commit90749682d34a31134faeaff4075467a533bc45ff (patch)
treef4ab70fc5db4b558b63abba49fa534f97631c6fb
parent4c2d24678dbd178aa175f579e5a63c37e38efbff (diff)
downloadperlweeklychallenge-club-90749682d34a31134faeaff4075467a533bc45ff.tar.gz
perlweeklychallenge-club-90749682d34a31134faeaff4075467a533bc45ff.tar.bz2
perlweeklychallenge-club-90749682d34a31134faeaff4075467a533bc45ff.zip
solutions for challenge 039
-rw-r--r--challenge-039/adam-russell/blog.txt1
-rw-r--r--challenge-039/adam-russell/blog1.txt1
-rw-r--r--challenge-039/adam-russell/perl/Rpn.yp47
-rw-r--r--challenge-039/adam-russell/perl/ch-1.pl63
-rw-r--r--challenge-039/adam-russell/perl/ch-2.pl18
5 files changed, 130 insertions, 0 deletions
diff --git a/challenge-039/adam-russell/blog.txt b/challenge-039/adam-russell/blog.txt
new file mode 100644
index 0000000000..b7b12fb77f
--- /dev/null
+++ b/challenge-039/adam-russell/blog.txt
@@ -0,0 +1 @@
+https://adamcrussell.livejournal.com/13288.html
diff --git a/challenge-039/adam-russell/blog1.txt b/challenge-039/adam-russell/blog1.txt
new file mode 100644
index 0000000000..2809b2a2b0
--- /dev/null
+++ b/challenge-039/adam-russell/blog1.txt
@@ -0,0 +1 @@
+https://adamcrussell.livejournal.com/13321.html
diff --git a/challenge-039/adam-russell/perl/Rpn.yp b/challenge-039/adam-russell/perl/Rpn.yp
new file mode 100644
index 0000000000..d27d0b7bcd
--- /dev/null
+++ b/challenge-039/adam-russell/perl/Rpn.yp
@@ -0,0 +1,47 @@
+%token NUMBER
+%%
+
+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]}
+;
+
+%%
+
+sub lexer{
+ my($parser) = @_;
+ $parser->YYData->{INPUT} or return('', undef);
+ $parser->YYData->{INPUT} =~ s/^[ \t]//;
+ ##
+ # send tokens to parser
+ ##
+ for($parser->YYData->{INPUT}){
+ s/^([0-9]+)// and return ("NUMBER", $1);
+ s/^(\+)// and return ("+", $1);
+ s/^(-)// and return ("-", $1);
+ s/^(\*)// and return ("*", $1);
+ s/^(\/)// and return ("/", $1);
+ s/^(\n)// and return ("\n", $1);
+ }
+}
+
+sub error{
+ exists $_[0]->YYData->{ERRMSG}
+ and do{
+ print $_[0]->YYData->{ERRMSG};
+ return;
+ };
+ print "syntax error\n";
+}
+
+sub parse{
+ my($self, $input) = @_;
+ $self->YYData->{INPUT} = $input;
+ my $result = $self->YYParse(yylex => \&lexer, yyerror => \&error);
+ return $result;
+}
diff --git a/challenge-039/adam-russell/perl/ch-1.pl b/challenge-039/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..861fccd157
--- /dev/null
+++ b/challenge-039/adam-russell/perl/ch-1.pl
@@ -0,0 +1,63 @@
+use strict;
+use warnings;
+##
+# 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.
+##
+use DateTime::Duration;
+my $lights_on = new DateTime::Duration(
+ hours => 0,
+ minutes => 0,
+ seconds => 0
+);
+my $start_time;
+
+while(my $line = <DATA>){
+ chomp($line);
+ my @fields = split(/:/, $line);
+ my($in_hour, $in_minute, $out_hour, $out_minute) = @fields[1, 2, 3, 4];
+ $in_hour =~ tr/ //d;
+ $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
+ );
+ }
+ 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));
+ }
+ if(DateTime::Duration->compare($start_time, $in) > 0 &&
+ DateTime::Duration->compare($start_time, $out) < 0
+ ){
+ $lights_on -> add_duration($out->subtract_duration($start_time));
+ }
+ $start_time = $out;
+}
+print $lights_on->hours() . " hours ";
+print $lights_on->minutes() . " minutes\n";
+
+__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
diff --git a/challenge-039/adam-russell/perl/ch-2.pl b/challenge-039/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..52d9a91e10
--- /dev/null
+++ b/challenge-039/adam-russell/perl/ch-2.pl
@@ -0,0 +1,18 @@
+use strict;
+use warnings;
+##
+# Write a script to demonstrate Reverse Polish notation(RPN).
+##
+use Rpn;
+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";
+
+MAIN:{
+ my $parser = new Rpn();
+ $parser->parse(RPN_ADD);
+ $parser->parse(RPN_SUBTRACT);
+ $parser->parse(RPN_MULTIPLY);
+ $parser->parse(RPN_DIVIDE);
+}