diff options
| author | Jaldhar H. Vyas <jaldhar@braincells.com> | 2019-08-04 02:00:03 -0400 |
|---|---|---|
| committer | Jaldhar H. Vyas <jaldhar@braincells.com> | 2019-08-04 18:54:08 -0400 |
| commit | 83245b1fde8e3f00b2fd9857583343a28afcdf27 (patch) | |
| tree | a6b60309dcd615167d03ceb324b491ba3220e0cc | |
| parent | 9e658cecc222fc815cd305794e67941be3377e5e (diff) | |
| download | perlweeklychallenge-club-83245b1fde8e3f00b2fd9857583343a28afcdf27.tar.gz perlweeklychallenge-club-83245b1fde8e3f00b2fd9857583343a28afcdf27.tar.bz2 perlweeklychallenge-club-83245b1fde8e3f00b2fd9857583343a28afcdf27.zip | |
Challenge 19 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-019/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-019/jaldhar-h-vyas/perl5/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-019/jaldhar-h-vyas/perl5/ch-2.pl | 21 | ||||
| -rwxr-xr-x | challenge-019/jaldhar-h-vyas/perl6/ch-1.p6 | 18 | ||||
| -rwxr-xr-x | challenge-019/jaldhar-h-vyas/perl6/ch-2.p6 | 17 |
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"; +} |
