diff options
| author | Burkhard Nickels <chuck@peregrina> | 2019-12-22 22:14:25 +0100 |
|---|---|---|
| committer | Burkhard Nickels <chuck@peregrina> | 2019-12-22 22:14:25 +0100 |
| commit | e75cf41be3dacd8a99083a382a0500b3934d18d4 (patch) | |
| tree | df6dffe97f156e28da93a250773a6931fd8a7bf4 /challenge-039 | |
| parent | 4246da20a67ce4ffcc2ee25af383b9c134d1a5c7 (diff) | |
| download | perlweeklychallenge-club-e75cf41be3dacd8a99083a382a0500b3934d18d4.tar.gz perlweeklychallenge-club-e75cf41be3dacd8a99083a382a0500b3934d18d4.tar.bz2 perlweeklychallenge-club-e75cf41be3dacd8a99083a382a0500b3934d18d4.zip | |
Solution for PWC 39 from Burkhard Nickels.
Diffstat (limited to 'challenge-039')
| -rw-r--r-- | challenge-039/burkhard-nickels/README | 1 | ||||
| -rw-r--r-- | challenge-039/burkhard-nickels/blogs.txt | 1 | ||||
| -rwxr-xr-x | challenge-039/burkhard-nickels/perl5/ch-1.pl | 251 | ||||
| -rwxr-xr-x | challenge-039/burkhard-nickels/perl5/ch-2.pl | 140 |
4 files changed, 393 insertions, 0 deletions
diff --git a/challenge-039/burkhard-nickels/README b/challenge-039/burkhard-nickels/README new file mode 100644 index 0000000000..f5e16bb98b --- /dev/null +++ b/challenge-039/burkhard-nickels/README @@ -0,0 +1 @@ +Solutions by Burkhard Nickels. diff --git a/challenge-039/burkhard-nickels/blogs.txt b/challenge-039/burkhard-nickels/blogs.txt new file mode 100644 index 0000000000..9cb0ea99b5 --- /dev/null +++ b/challenge-039/burkhard-nickels/blogs.txt @@ -0,0 +1 @@ +http://pearls.dyndnss.net diff --git a/challenge-039/burkhard-nickels/perl5/ch-1.pl b/challenge-039/burkhard-nickels/perl5/ch-1.pl new file mode 100755 index 0000000000..f06498f4ed --- /dev/null +++ b/challenge-039/burkhard-nickels/perl5/ch-1.pl @@ -0,0 +1,251 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use DateTime; +use Data::Dumper qw(Dumper); +$Data::Dumper::Indent=0; + +=head1 Perl Weekly Challenge #39 Task #1: Guest House + +This solution expects the arrive time of each next guest higher than the former. +When the leave time is bigger than the former the end time is set. When the leave +time is smaller then the end time is kept. Time calculation is done with the +C<DateTime> Module. + +=over 3 + +=item * DateTime + +=item * DateTime::Duration + +=back + + +=begin html + +<h2> Download and References </h2> +<b>Download File:</b><a href="ch-1.pl" download>Solution PWC #39 Task #1 ch-1.pl</a><br> +<b>Download File:</b><a href="ch-2.pl" download>Solution PWC #39 Task #2 ch-2.pl</a><br> +<br> + +=end html + +=head1 SYNOPSIS + + # perldoc ch-1.pl - POD + ./ch-1.pl [html|help] + + # ./ch-1.pl - Executes the program + + # ./ch-1.pl html - HTML/CSS in ch-1.html/pwc.css + # ./ch-1.pl help - Usage information + + ./ch-1.pl + ch-1.pl (Version 1.0) PWC #39 Task #1: Guest House + Alex: 09:10 - 09:45 + Arnold: 09:15 - 09:33 + Bob: 09:22 - 09:55 + Charlie: 09:25 - 10:05 + Steve: 09:33 - 10:01 + Roger: 09:44 - 10:12 + David: 09:57 - 10:23 + Neil: 10:01 - 10:19 + Chris: 10:10 - 11:00 + Duration 1:50 (09:10:00 - 11:00:00) + + +=cut + +my (@guest_book); + +print "ch-1.pl (Version 1.0) PWC #39 Task #1: Guest House\n"; + +my $cmd = shift @ARGV; # Read command or text string +if(!$cmd) { $cmd = ""; } +if($cmd eq "html") { html(); exit 0; } +elsif($cmd eq "help") { usage(); exit 0; } +else { + init(); + calculate_light_on(); +} + +# ====================== TASK 1 ============================== + +=head1 Definition Task #1: Guest House + +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 + + +=cut + +# ====================== TASK 1 ============================== + +=head1 Functions + +The C< init() > function first initializes an array with the given guest book data. +Then it creates for the time values an array of DateTime Objects. + +The C< calculate_light_on() > function than calculates the start and end time for +B< light on > and than also the difference of these two timestamps. Means the light +on time. + +=head2 init() + +The array C< @guest_book_tmp > is changed in a C< DateTime > objects array by +iterating over each element of the array. + + sub init { + + my @guest_book_tmp = ( + [ "Alex" ,"09:10","09:45" ], + [ "Arnold" ,"09:15","09:33" ], + [ "Bob" ,"09:22","09:55" ], + [ "Charlie","09:25","10:05" ], + [ "Steve" ,"09:33","10:01" ], + [ "Roger" ,"09:44","10:12" ], + [ "David" ,"09:57","10:23" ], + [ "Neil" ,"10:01","10:19" ], + [ "Chris" ,"10:10","11:00" ] + ); + my %date = ( year=>2019,month=>12,day=>16 ); + foreach my $g (@guest_book_tmp) { + printf(" %8s: %5s - %5s\n", @$g); + my @tmp; + my ($inh,$inm) = split(":",$g->[1]); + my ($ouh,$oum) = split(":",$g->[2]); + push(@tmp, DateTime->new( %date, hour=> $inh, minute=> $inm )); + push(@tmp, DateTime->new( %date, hour=> $ouh, minute=> $oum )); + push(@tmp, $tmp[1] - $tmp[0]); + push(@guest_book,[@tmp]); + } + } + +=cut + +sub init { + + my @guest_book_tmp = ( + [ "Alex" ,"09:10","09:45" ], + [ "Arnold" ,"09:15","09:33" ], + [ "Bob" ,"09:22","09:55" ], + [ "Charlie","09:25","10:05" ], + [ "Steve" ,"09:33","10:01" ], + [ "Roger" ,"09:44","10:12" ], + [ "David" ,"09:57","10:23" ], + [ "Neil" ,"10:01","10:19" ], + [ "Chris" ,"10:10","11:00" ] + ); + my %date = ( year=>2019,month=>12,day=>16 ); + foreach my $g (@guest_book_tmp) { + printf(" %8s: %5s - %5s\n", @$g); + my @tmp; + my ($inh,$inm) = split(":",$g->[1]); + my ($ouh,$oum) = split(":",$g->[2]); + push(@tmp, DateTime->new( %date, hour=> $inh, minute=> $inm )); + push(@tmp, DateTime->new( %date, hour=> $ouh, minute=> $oum )); + push(@tmp, $tmp[1] - $tmp[0]); + push(@guest_book,[@tmp]); + } +} + +=head2 calculate_light_on() + +The ligth_on calculation is done via iteration over the array and only verifying it +next end time is later than stored end time. When the cases are more complicated, in +some gaps inbetween the time ranges, than this example wouldn't work anymore. Than +some more if-statements would be necessary. So this may-be works only for the given +simple example. + +C<hours()> and C<minutes()> are methods from DateTime::Duration to return +the Hours and Minutes of the Duration object. C<hms()> is a methode from DateTime +to return the time in the readable format "hh:mm", i.e. "11:00". + + sub calculate_light_on { + + my $old = $guest_book[0]; + my $start = $old->[0]; + my $end = $old->[1]; + for( my $i=1; $i<=$#guest_book; $i++ ) { + my $t = $guest_book[$i][1]; + if( $t > $end ) { $end = $t; } + } + my $dur = $end - $start; + print "Duration ", $dur->hours, ":", $dur->minutes, " (", + $start->hms, " - ", $end->hms, ")\n"; + } + +=cut + +sub calculate_light_on { + + my $old = $guest_book[0]; + my $start = $old->[0]; + my $end = $old->[1]; + for( my $i=1; $i<=$#guest_book; $i++ ) { + my $t = $guest_book[$i][1]; + if( $t > $end ) { $end = $t; } + } + my $dur = $end - $start; + print "Duration ", $dur->hours, ":", $dur->minutes, " (", $start->hms, " - ", $end->hms, ")\n"; +} + + + +# ================================ Usage ============================ +sub usage { + print "./ch-1.pl [help|html]\n"; + print "\n"; + print " help, Prints out some usage information.\n"; + print " html, Writes HTML and CSS from POD.\n"; + print "\n"; + print " Examples:\n"; + print " # ./ch-1.pl\n"; + print " # perldoc ch-1.pl\n"; + print " # ./ch-1.pl help\n"; + print " # ./ch-1.pl html\n"; +} + +sub html { + # ------------- Create HTML -------------- + qx[ pod2html --header --title \"Perl Weekly Challenge #39 Task #1, Guest Book\" --css \"pwc.css\" ch-1.pl > ch-1.html ]; + + # ------------- Create CSS -------------- + my $CSS =<<CSS; +body { margin-left:auto; margin-right:auto; width:80%; } +h1 { border-bottom:4px solid red; } +h2 { border-bottom:2px solid orange; } +pre { border:2px solid grey; background-color:#eef; padding:10px; } +li { padding:5px; } +a { text-decoration:none; color:black; padding:4px; } +a:hover { background-color: brown; color:white; } +._podblock_ { width:100%; background-color:black; color:white; padding:10px; } +CSS + + open(CSS, ">pwc.css") or die "Cant open pwc.css!\n"; + print CSS $CSS; + close CSS; +} + +=head1 AUTHOR + +Chuck + +=cut + +# ############################## END ############################################# + diff --git a/challenge-039/burkhard-nickels/perl5/ch-2.pl b/challenge-039/burkhard-nickels/perl5/ch-2.pl new file mode 100755 index 0000000000..03b953bf04 --- /dev/null +++ b/challenge-039/burkhard-nickels/perl5/ch-2.pl @@ -0,0 +1,140 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Math::RPN; + +=head1 Perl Weekly Challenge #39 Task #2: RPN + +RPN - Reverse Polish Notation has several Modules on CPAN. I installed C<Math::RPN> +to show RPN. + +=over 3 + +=item * Math::RPN + +=back + + +=begin html + +<h2> Download and References </h2> +<b>Download File:</b><a href="ch-1.pl" download>Solution PWC #39 Task #1 ch-1.pl</a><br> +<b>Download File:</b><a href="ch-2.pl" download>Solution PWC #39 Task #2 ch-2.pl</a><br> +<br> +<a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation"> +https://en.wikipedia.org/wiki/Reverse_Polish_notation</a><br> + +=end html + +=head1 SYNOPSIS + + # perldoc ch-2.pl - POD + ./ch-2.pl [html|help] + # ./ch-2.pl - Executes the program. + # ./ch-2.pl html - HTML/CSS in ch-2.html/pwc.css + # ./ch-2.pl help - Usage information + + ./ch-2.pl + ch-2.pl (Version 1.0) PWC #39 Task #2: RPN + IN: 2,3,+,10,+,5,- + Result: 10 + +=cut + +print "ch-2.pl (Version 1.0) PWC #39 Task #2: RPN\n"; +my $cmd = shift @ARGV; # Read command or text string +if(!$cmd) { $cmd = ""; } +if($cmd eq "html") { html(); exit 0; } +elsif($cmd eq "help") { usage(); exit 0; } +else { + calculate_rpn(); +} + +# ====================== TASK 2 ============================== + +=head1 Definition Task #2: RPN + +Write a script to demonstrate Reverse Polish Notation(RPN). +Checkout the wiki page for more information about RPN. + +=cut + +# ====================== TASK 2 ============================== + +=head1 RPN with Math::RPN + +Reverse Polish Notation is a mathematical calculation with the operator after the operands. +In the example some simple add and substract is used. +The RPN C< my $rpn = "2,3,+,10,+,5,-"; > is doing the mathematical: + + 2 + 3 = 5 + 5 + 10 = 15 + 15 - 5 = 10 + +So the result of these operations is 10. + + use Math::RPN; + + my $rpn = "2,3,+,10,+,5,-"; + my $value = rpn(split(",",$rpn)); + + print "IN: $rpn\n"; + print "Result: $value\n"; + +=cut + +sub calculate_rpn { + + my $rpn = "2,3,+,10,+,5,-"; + my $value = rpn(split(",",$rpn)); + my @array = rpn(split(",",$rpn)); + + print "IN: $rpn\n"; + print "Result: $value\n"; + print "Array: ", join(",",@array), "\n"; +} + +# ================================ Usage ============================ +sub usage { + print "./ch-2.pl [html|help]\n"; + print "\n"; + print " help, Prints out some usage information.\n"; + print " html, Writes HTML and CSS from POD.\n"; + print "\n"; + print " Examples:\n"; + print " # perldoc ch-2.pl\n"; + print " # ./ch-2.pl\n"; + print " # ./ch-2.pl help\n"; + print " # ./ch-2.pl html\n"; +} + +sub html { + # ------------- Create HTML -------------- + qx[ pod2html --header --title \"Perl Weekly Challenge #39 Task #2, RPN\" --css \"pwc.css\" ch-2.pl > ch-2.html ]; + + # ------------- Create CSS -------------- + my $CSS =<<CSS; +body { margin-left:auto; margin-right:auto; width:80%; } +h1 { border-bottom:4px solid red; } +h2 { border-bottom:2px solid orange; } +pre { border:2px solid grey; background-color:#eef; padding:10px; } +li { padding:5px; } +a { text-decoration:none; color:black; padding:4px; } +a:hover { background-color: brown; color:white; } +._podblock_ { width:100%; background-color:black; color:white; padding:10px; } +CSS + + open(CSS, ">pwc.css") or die "Cant open pwc.css!\n"; + print CSS $CSS; + close CSS; +} + +=head1 AUTHOR + +Chuck + +=cut + +# ############################## END ############################################# + |
