aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-08-05 00:46:03 +0100
committerGitHub <noreply@github.com>2019-08-05 00:46:03 +0100
commitfbf757f7ead69d52480dacb843d71ddd5af477ea (patch)
treec4f0ed670a30c2fea7e912b0b97ea5f0f5e10050
parent19e4e3dc166940cba24892608e75e115f6a6af17 (diff)
parent83245b1fde8e3f00b2fd9857583343a28afcdf27 (diff)
downloadperlweeklychallenge-club-fbf757f7ead69d52480dacb843d71ddd5af477ea.tar.gz
perlweeklychallenge-club-fbf757f7ead69d52480dacb843d71ddd5af477ea.tar.bz2
perlweeklychallenge-club-fbf757f7ead69d52480dacb843d71ddd5af477ea.zip
Merge pull request #473 from jaldhar/challenge-019
Challenge 19 by Jaldhar H. Vyas.
-rw-r--r--challenge-019/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-019/jaldhar-h-vyas/perl5/ch-1.pl30
-rwxr-xr-xchallenge-019/jaldhar-h-vyas/perl5/ch-2.pl21
-rwxr-xr-xchallenge-019/jaldhar-h-vyas/perl6/ch-1.p618
-rwxr-xr-xchallenge-019/jaldhar-h-vyas/perl6/ch-2.p617
5 files changed, 87 insertions, 0 deletions
diff --git a/challenge-019/jaldhar-h-vyas/blog.txt b/challenge-019/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..ce69c101d5
--- /dev/null
+++ b/challenge-019/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2019/08/perl_weekly_challenge_week_19.html
diff --git a/challenge-019/jaldhar-h-vyas/perl5/ch-1.pl b/challenge-019/jaldhar-h-vyas/perl5/ch-1.pl
new file mode 100755
index 0000000000..1ba38d1120
--- /dev/null
+++ b/challenge-019/jaldhar-h-vyas/perl5/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+use constant FEBRUARY => 2;
+use constant FRIDAY => 4; # 1/1/1900 is a Monday so offset of 4 for Friday.
+use constant WEEK => 7;
+
+sub isLeap {
+ my ($year) = @_;
+
+ # years divisible by 100 are not leap years unless they are divisble by 400.
+ return ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0));
+}
+
+my @days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
+
+my $elapsedDays = -31;
+
+for my $year (1900 .. 2019) {
+ for my $month (0 .. 11) {
+ $elapsedDays += $days[$month];
+ if (isLeap($year) && $month == FEBRUARY) {
+ $elapsedDays++;
+ }
+ if ($days[$month] == 31 && $elapsedDays % WEEK == FRIDAY) {
+ say sprintf("%02d/%d", $month + 1, $year);
+ }
+ }
+}
diff --git a/challenge-019/jaldhar-h-vyas/perl5/ch-2.pl b/challenge-019/jaldhar-h-vyas/perl5/ch-2.pl
new file mode 100755
index 0000000000..3b8c782b0f
--- /dev/null
+++ b/challenge-019/jaldhar-h-vyas/perl5/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+
+sub wordWrap {
+ my ($paragraph, $lineWidth) = @_;
+
+ my $spaceLeft = $lineWidth + 1;
+
+ while ( $paragraph =~ /\G (?<word> \w+)(\W+)? /gcx ) {
+ my $wordWidth = length $+{word};
+ if ($wordWidth + 1 > $spaceLeft) {
+ print "\n";
+ $spaceLeft = $lineWidth - $wordWidth;
+ } else {
+ $spaceLeft -= ($wordWidth + 1);
+ }
+ print "$+{word} ";
+ }
+}
diff --git a/challenge-019/jaldhar-h-vyas/perl6/ch-1.p6 b/challenge-019/jaldhar-h-vyas/perl6/ch-1.p6
new file mode 100755
index 0000000000..e094da478c
--- /dev/null
+++ b/challenge-019/jaldhar-h-vyas/perl6/ch-1.p6
@@ -0,0 +1,18 @@
+#!/usr/bin/perl6
+
+multi sub MAIN() {
+ constant FRIDAY = 5;
+
+ for 1900 .. 2019 -> $year {
+ for 1 .. 12 -> $month {
+
+ my $date = Date.new(year => $year, month => $month, day => 1,
+ formatter => { sprintf("%02d/%d", .month, .year) }
+ );
+
+ if ($date.days-in-month == 31 && $date.day-of-week == FRIDAY) {
+ say $date;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/challenge-019/jaldhar-h-vyas/perl6/ch-2.p6 b/challenge-019/jaldhar-h-vyas/perl6/ch-2.p6
new file mode 100755
index 0000000000..b5c983a3b3
--- /dev/null
+++ b/challenge-019/jaldhar-h-vyas/perl6/ch-2.p6
@@ -0,0 +1,17 @@
+#!/usr/bin/perl6
+
+sub wordWrap(Str $paragraph, Int $lineWidth) {
+ my $spaceLeft = $lineWidth + 1;
+
+ for $paragraph.words -> $word {
+ my $wordWidth = $word.chars;
+ if $wordWidth + 1 > $spaceLeft {
+ print "\n";
+ $spaceLeft = $lineWidth - $wordWidth;
+ } else {
+ $spaceLeft -= ($wordWidth + 1);
+ }
+ print "$word ";
+ }
+ print "\n";
+}