aboutsummaryrefslogtreecommitdiff
path: root/challenge-037
diff options
context:
space:
mode:
authorandrezgz <andrezgz@gmail.com>2019-12-04 08:16:48 -0300
committerandrezgz <andrezgz@gmail.com>2019-12-04 08:16:48 -0300
commitf660049fda7f7adad9380bb03fe4e544628c2013 (patch)
treea31bf4fe0e3f4395e4f89a54c6cb9807374e387c /challenge-037
parentdc7ad12deddf8274342936ece359920e1902d724 (diff)
downloadperlweeklychallenge-club-f660049fda7f7adad9380bb03fe4e544628c2013.tar.gz
perlweeklychallenge-club-f660049fda7f7adad9380bb03fe4e544628c2013.tar.bz2
perlweeklychallenge-club-f660049fda7f7adad9380bb03fe4e544628c2013.zip
challenge-037 andrezgz solution
Diffstat (limited to 'challenge-037')
-rw-r--r--challenge-037/andrezgz/perl5/ch-1.pl34
-rw-r--r--challenge-037/andrezgz/perl5/ch-2.pl40
-rw-r--r--challenge-037/andrezgz/perl5/december.csv31
-rw-r--r--challenge-037/andrezgz/perl5/november.csv30
4 files changed, 135 insertions, 0 deletions
diff --git a/challenge-037/andrezgz/perl5/ch-1.pl b/challenge-037/andrezgz/perl5/ch-1.pl
new file mode 100644
index 0000000000..c815c85d86
--- /dev/null
+++ b/challenge-037/andrezgz/perl5/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-037/
+# Task #1
+# Write a script to calculate the total number of weekdays (Mon-Fri) in each month of the year 2019.
+# Jan: 23 days
+# Feb: 20 days
+# Mar: 21 days
+# Apr: 22 days
+# May: 23 days
+# Jun: 20 days
+# Jul: 23 days
+# Aug: 22 days
+# Sep: 21 days
+# Oct: 23 days
+# Nov: 21 days
+# Dec: 22 days
+
+use strict;
+use warnings;
+
+use DateTime;
+
+my $dt = DateTime->new( year => 2019, month => 1, day => 1 );
+
+my @weekdays;
+
+do { $weekdays[$dt->month]++ if $dt->dow < 6; }
+while ( $dt->add( days => 1 )->year < 2020 );
+
+foreach my $m (1 .. $#weekdays){
+ my $month_name = DateTime->new( year => 2019, month => $m )->month_abbr;
+ printf '%4s: %d days'.$/, $month_name, $weekdays[$m] ;
+}
diff --git a/challenge-037/andrezgz/perl5/ch-2.pl b/challenge-037/andrezgz/perl5/ch-2.pl
new file mode 100644
index 0000000000..653ecd2d58
--- /dev/null
+++ b/challenge-037/andrezgz/perl5/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-037/
+# Task #2
+# Write a script to find out the DayLight gain/loss in the month of December 2019
+# as compared to November 2019 in the city of London.
+# You can find out sunrise and sunset data for November 2019 and December 2019 for London.
+# https://www.timeanddate.com/sun/uk/london?month=11&year=2019
+# https://www.timeanddate.com/sun/uk/london?month=12&year=2019
+
+use strict;
+use warnings;
+
+my $diff = daylight('december.csv') - daylight('november.csv');
+print 'The daylight difference is ', int $diff/60, ':', abs($diff) % 60, $/;
+
+# Modulo operator works funny with negative numbers, that's why I've used abs
+# -1280 % 60 = 40 (not -20)
+# With use integer you get proper and fast libc behavior.
+# See https://stackoverflow.com/a/32090446 for more detail
+
+sub daylight {
+ my $fn = shift;
+ my $daylight = 0;
+ open my $fh, '<', $fn or die "Could not open file '$fn': $!";
+
+ while (<$fh>) {
+ chomp;
+ my ($sunrise_h, $sunrise_m, $sunset_h, $sunset_m) = split /[:;]/;
+ $daylight += ($sunset_h - $sunrise_h) * 60 + ($sunset_m - $sunrise_m);
+ }
+
+ close $fh;
+ return $daylight;
+}
+
+__END__
+
+./ch-2.pl
+The daylight difference is -21:20 <--- almost a day!
diff --git a/challenge-037/andrezgz/perl5/december.csv b/challenge-037/andrezgz/perl5/december.csv
new file mode 100644
index 0000000000..0a2927eedc
--- /dev/null
+++ b/challenge-037/andrezgz/perl5/december.csv
@@ -0,0 +1,31 @@
+7:43;15:55
+7:44;15:54
+7:46;15:53
+7:47;15:53
+7:48;15:53
+7:49;15:52
+7:51;15:52
+7:52;15:51
+7:53;15:51
+7:54;15:51
+7:55;15:51
+7:56;15:51
+7:57;15:51
+7:58;15:51
+7:59;15:51
+8:00;15:51
+8:00;15:51
+8:01;15:52
+8:02;15:52
+8:02;15:52
+8:03;15:53
+8:04;15:53
+8:04;15:54
+8:04;15:54
+8:05;15:55
+8:05;15:56
+8:05;15:57
+8:06;15:57
+8:06;15:58
+8:06;15:59
+8:06;16:00
diff --git a/challenge-037/andrezgz/perl5/november.csv b/challenge-037/andrezgz/perl5/november.csv
new file mode 100644
index 0000000000..beb3cb4710
--- /dev/null
+++ b/challenge-037/andrezgz/perl5/november.csv
@@ -0,0 +1,30 @@
+6:53;16:34
+6:55;16:32
+6:56;16:30
+6:58;16:28
+7:00;16:27
+7:02;16:25
+7:03;16:23
+7:05;16:22
+7:07;16:20
+7:09;16:18
+7:10;16:17
+7:12;16:15
+7:14;16:14
+7:16;16:12
+7:17;16:11
+7:19;16:10
+7:21;16:08
+7:22;16:07
+7:24;16:06
+7:26;16:05
+7:27;16:04
+7:29;16:03
+7:31;16:01
+7:32;16:00
+7:34;15:59
+7:35;15:59
+7:37;15:58
+7:38;15:57
+7:40;15:56
+7:41;15:55