aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-12-19 10:58:54 +0000
committerGitHub <noreply@github.com>2019-12-19 10:58:54 +0000
commitec5dbbf9ab4f7866c89992975bb67a3f79999229 (patch)
tree6f67ea0163d7fecaa727f375c1bc0c298515f36d
parent1aecb81db1df12a11491475bbef8bbeceb16687a (diff)
parenteac580dc0ef725df46001a98f9424262c1fa83a0 (diff)
downloadperlweeklychallenge-club-ec5dbbf9ab4f7866c89992975bb67a3f79999229.tar.gz
perlweeklychallenge-club-ec5dbbf9ab4f7866c89992975bb67a3f79999229.tar.bz2
perlweeklychallenge-club-ec5dbbf9ab4f7866c89992975bb67a3f79999229.zip
Merge pull request #1050 from ndelucca/ch039/ndelucca
Ch039/ndelucca
-rw-r--r--challenge-039/ndelucca/perl5/ch-1.pl68
-rw-r--r--challenge-039/ndelucca/perl5/ch-2.pl30
-rw-r--r--challenge-039/ndelucca/perl5/gb1.txt9
3 files changed, 107 insertions, 0 deletions
diff --git a/challenge-039/ndelucca/perl5/ch-1.pl b/challenge-039/ndelucca/perl5/ch-1.pl
new file mode 100644
index 0000000000..95c968e655
--- /dev/null
+++ b/challenge-039/ndelucca/perl5/ch-1.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-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 strict;
+use warnings;
+
+die "No guestbook selected." unless @ARGV;
+
+my @gb = ();
+my $guests = 0;
+
+# We process the file line by line, and create a cleaned up hash with time and action
+while(<>){
+ chomp;
+ my ( $i, $guest, $in, $in_time, $out, $out_time ) = split;
+ my %record = ();
+
+ push @gb, {
+ time => time_to_minutes($in_time),
+ action => substr($in,0,-1)
+ };
+
+ push @gb, {
+ time => time_to_minutes($out_time),
+ action => substr($out,0,-1)
+ };
+
+}
+
+my $timer_start = 0;
+my $timer = 0;
+
+for ( sort { $a->{time} <=> $b->{time} } @gb ){
+
+ if ($_->{action} eq 'IN'){
+ $guests++;
+ # We need to check if there was someone before
+ # If so we don't care about that action
+ $timer_start = $_->{time} if $guests == 1;
+ }else{
+ $guests--;
+ # When the last person checks out, we sum up the time
+ $timer += $_->{time} - $timer_start if $guests == 0;
+ }
+
+}
+print "The lights where on for $timer minutes\n";
+
+sub time_to_minutes {
+ my $time = shift;
+ my ($h, $m) = split /:/, $time;
+ return $h * 60 + $m;
+}
diff --git a/challenge-039/ndelucca/perl5/ch-2.pl b/challenge-039/ndelucca/perl5/ch-2.pl
new file mode 100644
index 0000000000..3cf6a4b9a4
--- /dev/null
+++ b/challenge-039/ndelucca/perl5/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-039/
+# Task #2
+# Write a script to demonstrate Reverse Polish notation(RPN).
+# Checkout the wiki page for more information about RPN.
+# https://en.wikipedia.org/wiki/Reverse_Polish_notation
+
+use strict;
+use warnings;
+
+die "Input required" unless @ARGV;
+
+my @stack;
+my @rpn = split / /, shift;
+
+while (@rpn) {
+
+ my $in = shift @rpn;
+
+ if ($in =~ /^\d+$/) {
+ push @stack, $in;
+ }else{
+ my $num1 = pop @stack;
+ my $num2 = pop @stack;
+ push @stack, eval "$num2 $in $num1";
+ }
+}
+
+print "Result: ",@stack, "\n";
diff --git a/challenge-039/ndelucca/perl5/gb1.txt b/challenge-039/ndelucca/perl5/gb1.txt
new file mode 100644
index 0000000000..a2467461cd
--- /dev/null
+++ b/challenge-039/ndelucca/perl5/gb1.txt
@@ -0,0 +1,9 @@
+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