diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-04-01 20:31:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-01 20:31:19 +0100 |
| commit | 0f3ad27835db06ff8ff091167d814d435e4e1edc (patch) | |
| tree | 6b22f3f2403388c248f886547e7f8075f527fc97 | |
| parent | 94c76f89337d76da7cb27601d2b02a5553462bf9 (diff) | |
| parent | 6d1d4fab6f6f2a764988af40d5b00e0eaa4854df (diff) | |
| download | perlweeklychallenge-club-0f3ad27835db06ff8ff091167d814d435e4e1edc.tar.gz perlweeklychallenge-club-0f3ad27835db06ff8ff091167d814d435e4e1edc.tar.bz2 perlweeklychallenge-club-0f3ad27835db06ff8ff091167d814d435e4e1edc.zip | |
Merge pull request #5866 from pauloscustodio/master
Add Perl solution to challenge 158
| -rw-r--r-- | challenge-037/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-037/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-037/paulo-custodio/perl/ch-1.pl | 50 | ||||
| -rw-r--r-- | challenge-037/paulo-custodio/perl/ch-2.pl | 107 | ||||
| -rw-r--r-- | challenge-037/paulo-custodio/t/test-1.yaml | 17 | ||||
| -rw-r--r-- | challenge-037/paulo-custodio/t/test-2.yaml | 5 | ||||
| -rw-r--r-- | challenge-158/paulo-custodio/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-158/paulo-custodio/perl/ch-2.pl | 34 | ||||
| -rw-r--r-- | challenge-158/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-158/paulo-custodio/t/test-2.yaml | 5 |
10 files changed, 259 insertions, 0 deletions
diff --git a/challenge-037/paulo-custodio/Makefile b/challenge-037/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-037/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-037/paulo-custodio/README b/challenge-037/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-037/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-037/paulo-custodio/perl/ch-1.pl b/challenge-037/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..8d7182b0aa --- /dev/null +++ b/challenge-037/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +# 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 Modern::Perl; +use DateTime; + +say_work_days(2019); + +sub say_work_days { + my($year) = @_; + for my $month (1..12) { + say month_abbrev($month),": ", month_work_days($year, $month), " days"; + } +} + +sub month_work_days { + my($year, $month) = @_; + my $work_days = 0; + my $dt = DateTime->new(year => $year, month => $month, day => 1); + my $month_length = $dt->month_length; + for my $day (1 .. $month_length) { + my $dow = $dt->day_of_week; + $work_days++ if $dow < 6; # mon(1) to fri(5), not sat(6), sun(7) + $dt->add(days => 1); + } + return $work_days; +} + +sub month_abbrev { + my($month) = @_; + my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); + return $months[$month-1]; +} diff --git a/challenge-037/paulo-custodio/perl/ch-2.pl b/challenge-037/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..45340c7c08 --- /dev/null +++ b/challenge-037/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,107 @@ +#!/usr/bin/env perl + +# 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. + +use Modern::Perl; + +my $november_data = <<'END'; +2019 Sunrise/Sunset Daylength Astronomical Twilight Nautical Twilight Civil Twilight Solar Noon +Nov Sunrise Sunset Length Diff. Start End Start End Start End Time Mil. km +1 06:53 ? 16:34 ? 9:40:44 -3:35 05:00 18:27 05:38 17:48 06:18 17:09 11:44 148.491 +2 06:55 ? 16:32 ? 9:37:10 -3:33 05:01 18:25 05:40 17:47 06:19 17:07 11:44 148.451 +3 06:56 ? 16:30 ? 9:33:38 -3:32 05:03 18:24 05:41 17:45 06:21 17:05 11:44 148.412 +4 06:58 ? 16:28 ? 9:30:07 -3:30 05:04 18:22 05:43 17:43 06:23 17:04 11:44 148.373 +5 07:00 ? 16:27 ? 9:26:38 -3:29 05:06 18:21 05:45 17:42 06:24 17:02 11:44 148.334 +6 07:02 ? 16:25 ? 9:23:11 -3:27 05:07 18:19 05:46 17:40 06:26 17:01 11:44 148.296 +7 07:03 ? 16:23 ? 9:19:45 -3:25 05:09 18:18 05:48 17:39 06:28 16:59 11:44 148.258 +8 07:05 ? 16:22 ? 9:16:22 -3:23 05:10 18:16 05:49 17:38 06:29 16:58 11:44 148.221 +9 07:07 ? 16:20 ? 9:13:01 -3:21 05:12 18:15 05:51 17:36 06:31 16:56 11:44 148.184 +10 07:09 ? 16:18 ? 9:09:42 -3:18 05:13 18:14 05:52 17:35 06:32 16:55 11:44 148.148 +11 07:10 ? 16:17 ? 9:06:25 -3:16 05:15 18:13 05:54 17:34 06:34 16:53 11:44 148.112 +12 07:12 ? 16:15 ? 9:03:11 -3:14 05:16 18:11 05:55 17:32 06:36 16:52 11:44 148.077 +13 07:14 ? 16:14 ? 8:59:59 -3:11 05:18 18:10 05:57 17:31 06:37 16:51 11:44 148.043 +14 07:16 ? 16:12 ? 8:56:50 -3:09 05:19 18:09 05:58 17:30 06:39 16:49 11:44 148.009 +15 07:17 ? 16:11 ? 8:53:44 -3:06 05:20 18:08 06:00 17:29 06:40 16:48 11:45 147.976 +16 07:19 ? 16:10 ? 8:50:40 -3:03 05:22 18:07 06:01 17:28 06:42 16:47 11:45 147.944 +17 07:21 ? 16:08 ? 8:47:39 -3:00 05:23 18:06 06:03 17:26 06:44 16:46 11:45 147.912 +18 07:22 ? 16:07 ? 8:44:42 -2:57 05:25 18:05 06:04 17:25 06:45 16:44 11:45 147.881 +19 07:24 ? 16:06 ? 8:41:48 -2:54 05:26 18:04 06:06 17:24 06:47 16:43 11:45 147.850 +20 07:26 ? 16:05 ? 8:38:57 -2:50 05:27 18:03 06:07 17:24 06:48 16:42 11:46 147.820 +21 07:27 ? 16:04 ? 8:36:09 -2:47 05:29 18:02 06:08 17:23 06:50 16:41 11:46 147.791 +22 07:29 ? 16:03 ? 8:33:25 -2:43 05:30 18:01 06:10 17:22 06:51 16:40 11:46 147.762 +23 07:31 ? 16:01 ? 8:30:45 -2:40 05:31 18:01 06:11 17:21 06:53 16:39 11:46 147.733 +24 07:32 ? 16:00 ? 8:28:09 -2:36 05:33 18:00 06:13 17:20 06:54 16:39 11:47 147.705 +25 07:34 ? 15:59 ? 8:25:36 -2:32 05:34 17:59 06:14 17:19 06:56 16:38 11:47 147.677 +26 07:35 ? 15:59 ? 8:23:08 -2:28 05:35 17:59 06:15 17:19 06:57 16:37 11:47 147.650 +27 07:37 ? 15:58 ? 8:20:44 -2:23 05:37 17:58 06:17 17:18 06:58 16:36 11:47 147.623 +28 07:38 ? 15:57 ? 8:18:24 -2:19 05:38 17:57 06:18 17:17 07:00 16:35 11:48 147.596 +29 07:40 ? 15:56 ? 8:16:09 -2:15 05:39 17:57 06:19 17:17 07:01 16:35 11:48 147.570 +30 07:41 ? 15:55 ? 8:13:59 -2:10 05:40 17:57 06:20 17:16 07:02 16:34 11:49 147.544 +* All times are local time for London. They take into account refraction. Dates are based on the Gregorian calendar. +END + +my $december_data = <<'END'; +2019 Sunrise/Sunset Daylength Astronomical Twilight Nautical Twilight Civil Twilight Solar Noon +Dec Sunrise Sunset Length Diff. Start End Start End Start End Time Mil. km +1 07:43 ? 15:55 ? 8:11:53 -2:05 05:41 17:56 06:22 17:16 07:04 16:34 11:49 147.518 +2 07:44 ? 15:54 ? 8:09:53 -2:00 05:42 17:56 06:23 17:15 07:05 16:33 11:49 147.493 +3 07:46 ? 15:53 ? 8:07:57 -1:55 05:44 17:55 06:24 17:15 07:06 16:33 11:50 147.469 +4 07:47 ? 15:53 ? 8:06:07 -1:50 05:45 17:55 06:25 17:15 07:08 16:32 11:50 147.446 +5 07:48 ? 15:53 ? 8:04:22 -1:45 05:46 17:55 06:26 17:14 07:09 16:32 11:50 147.423 +6 07:49 ? 15:52 ? 8:02:42 -1:39 05:47 17:55 06:27 17:14 07:10 16:32 11:51 147.400 +7 07:51 ? 15:52 ? 8:01:08 -1:34 05:48 17:55 06:28 17:14 07:11 16:31 11:51 147.379 +8 07:52 ? 15:51 ? 7:59:40 -1:28 05:49 17:54 06:29 17:14 07:12 16:31 11:52 147.358 +9 07:53 ? 15:51 ? 7:58:17 -1:22 05:50 17:54 06:30 17:14 07:13 16:31 11:52 147.338 +10 07:54 ? 15:51 ? 7:57:01 -1:16 05:51 17:54 06:31 17:14 07:14 16:31 11:53 147.319 +11 07:55 ? 15:51 ? 7:55:50 -1:10 05:52 17:54 06:32 17:14 07:15 16:31 11:53 147.301 +12 07:56 ? 15:51 ? 7:54:45 -1:04 05:52 17:54 06:33 17:14 07:16 16:31 11:54 147.283 +13 07:57 ? 15:51 ? 7:53:46 -0:58 05:53 17:55 06:34 17:14 07:17 16:31 11:54 147.267 +14 07:58 ? 15:51 ? 7:52:54 -0:52 05:54 17:55 06:35 17:14 07:18 16:31 11:54 147.251 +15 07:59 ? 15:51 ? 7:52:08 -0:46 05:55 17:55 06:36 17:14 07:19 16:31 11:55 147.237 +16 08:00 ? 15:51 ? 7:51:28 -0:39 05:56 17:55 06:36 17:14 07:19 16:31 11:55 147.223 +17 08:00 ? 15:51 ? 7:50:54 -0:33 05:56 17:56 06:37 17:15 07:20 16:32 11:56 147.210 +18 08:01 ? 15:52 ? 7:50:27 -0:27 05:57 17:56 06:38 17:15 07:21 16:32 11:56 147.198 +19 08:02 ? 15:52 ? 7:50:06 -0:20 05:58 17:56 06:38 17:15 07:22 16:32 11:57 147.187 +20 08:02 ? 15:52 ? 7:49:52 -0:14 05:58 17:57 06:39 17:16 07:22 16:33 11:57 147.177 +21 08:03 ? 15:53 ? 7:49:44 -0:07 05:59 17:57 06:40 17:16 07:23 16:33 11:58 147.167 +22 08:04 ? 15:53 ? 7:49:43 -0:01 05:59 17:58 06:40 17:17 07:23 16:34 11:58 147.158 +23 08:04 ? 15:54 ? 7:49:49 +0:05 06:00 17:58 06:41 17:17 07:24 16:34 11:59 147.149 +24 08:04 ? 15:54 ? 7:50:01 +0:11 06:00 17:59 06:41 17:18 07:24 16:35 11:59 147.141 +25 08:05 ? 15:55 ? 7:50:19 +0:18 06:01 17:59 06:41 17:19 07:25 16:35 12:00 147.133 +26 08:05 ? 15:56 ? 7:50:44 +0:24 06:01 18:00 06:42 17:19 07:25 16:36 12:00 147.126 +27 08:05 ? 15:57 ? 7:51:15 +0:31 06:01 18:01 06:42 17:20 07:25 16:37 12:01 147.120 +28 08:06 ? 15:57 ? 7:51:53 +0:37 06:01 18:01 06:42 17:21 07:25 16:38 12:01 147.114 +29 08:06 ? 15:58 ? 7:52:37 +0:44 06:02 18:02 06:42 17:21 07:26 16:38 12:02 147.109 +30 08:06 ? 15:59 ? 7:53:28 +0:50 06:02 18:03 06:43 17:22 07:26 16:39 12:02 147.104 +31 08:06 ? 16:00 ? 7:54:24 +0:56 06:02 18:04 06:43 17:23 07:26 16:40 12:03 147.100 +* All times are local time for London. They take into account refraction. Dates are based on the Gregorian calendar. +END + +my $november_sun_length = parse_sun_length($november_data); +my $december_sun_length = parse_sun_length($december_data); +say(sprintf("%.1f",($december_sun_length-$november_sun_length)/60), " minutes gained"); + +sub parse_sun_length { + my($data) = @_; + my $sun_lenght_seconds; + open(my $fh, "<", \$data) or die; + while (<$fh>) { + next unless /^(\d+)/; + next unless $1 >= 1 && $1 <= 30; # check only 30 days so that periods are same + my @f = split(' ', $_); + my $day_seconds = parse_hms_seconds($f[5]); + $sun_lenght_seconds += $day_seconds; + } + return $sun_lenght_seconds; +} + +sub parse_hms_seconds { + my($hms) = @_; + my($h, $m, $s) = split /\D/, $hms; + return 3600*$h+60*$m+$s; +} diff --git a/challenge-037/paulo-custodio/t/test-1.yaml b/challenge-037/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..5f5445da53 --- /dev/null +++ b/challenge-037/paulo-custodio/t/test-1.yaml @@ -0,0 +1,17 @@ +- setup: + cleanup: + args: + input: + output: | + |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 diff --git a/challenge-037/paulo-custodio/t/test-2.yaml b/challenge-037/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..19c0fa0e86 --- /dev/null +++ b/challenge-037/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: -1750.0 minutes gained diff --git a/challenge-158/paulo-custodio/perl/ch-1.pl b/challenge-158/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..1c23f8c0ff --- /dev/null +++ b/challenge-158/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +# Challenge 158 +# +# TASK #1 › Additive Primes +# Submitted by: Mohammad S Anwar +# Write a script to find out all Additive Primes <= 100. +# +# Additive primes are prime numbers for which the sum of their decimal digits +# are also primes. +# +# +# Output +# 2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89 + +use Modern::Perl; +use List::Util qw( sum ); +use ntheory qw( is_prime next_prime ); + +say join(", ", additive_primes(100)); + +sub additive_primes { + my($limit) = @_; + my @out; + my $prime = 2; + while ($prime < $limit) { + if (is_prime(sum(split(//, $prime)))) { + push @out, $prime; + } + $prime = next_prime($prime); + } + return @out; +} diff --git a/challenge-158/paulo-custodio/perl/ch-2.pl b/challenge-158/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..8f28f63531 --- /dev/null +++ b/challenge-158/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +# Challenge 158 +# +# TASK #2 › First Series Cuban Primes +# Submitted by: Mohammad S Anwar +# Write a script to compute first series Cuban Primes <= 1000. Please refer +# wikipedia page for more informations. +# +# Output +# 7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919. +# +# p=(x^3-y^3)/(x-y), x=y+1, y>0 +# <=> p=3y^2+3y+1, y>0 + +use Modern::Perl; +use ntheory qw( is_prime ); + +say join(", ", cuban_primes(1000)); + +sub cuban_primes { + my($limit) = @_; + my @out; + my $y = 1; + my $p; + do { + $p = 3*$y*$y+3*$y+1; + if (is_prime($p)) { + push @out, $p; + } + $y++; + } while ($p < $limit); + return @out; +} diff --git a/challenge-158/paulo-custodio/t/test-1.yaml b/challenge-158/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..245c490895 --- /dev/null +++ b/challenge-158/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89 diff --git a/challenge-158/paulo-custodio/t/test-2.yaml b/challenge-158/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..19b7f5e476 --- /dev/null +++ b/challenge-158/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919 |
