diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-07-30 19:03:37 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-30 19:03:37 +0100 |
| commit | 5ddee5bfd31812f2f3708c1b8e71e6336fd176ee (patch) | |
| tree | a4612977a72053f09ac278c3684a1395465c0e7f | |
| parent | 735ae0344f801f952d5e72d86d64a79959b6e2e5 (diff) | |
| parent | 22ab01e2270e02bdb0a0fff564d05b049cde22b5 (diff) | |
| download | perlweeklychallenge-club-5ddee5bfd31812f2f3708c1b8e71e6336fd176ee.tar.gz perlweeklychallenge-club-5ddee5bfd31812f2f3708c1b8e71e6336fd176ee.tar.bz2 perlweeklychallenge-club-5ddee5bfd31812f2f3708c1b8e71e6336fd176ee.zip | |
Merge pull request #450 from gnustavo/019
Gustavo Chaves's solutions to challenge 019
| -rwxr-xr-x | challenge-019/gustavo-chaves/perl5/ch-1.pl | 34 | ||||
| -rwxr-xr-x | challenge-019/gustavo-chaves/perl5/ch-2.pl | 33 |
2 files changed, 67 insertions, 0 deletions
diff --git a/challenge-019/gustavo-chaves/perl5/ch-1.pl b/challenge-019/gustavo-chaves/perl5/ch-1.pl new file mode 100755 index 0000000000..2cb43ca2c9 --- /dev/null +++ b/challenge-019/gustavo-chaves/perl5/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +# Write a script to display months from the year 1900 to 2019 where you find 5 +# weekends i.e. 5 Friday, 5 Saturday and 5 Sunday. + +use 5.026; +use strict; +use warnings; +use DateTime; + +=pod + +First, let's realize that in order to have 5 Fridays, Saturdays, and Sundays a +month must have 31 days B<and> it's first day must be a Friday. If its first day +is a Friday, the fifth Friday is day 29 and its fifth Sunday is day 31. There is +no other choice. + +So, we need only find which months have 31 days and start in a Friday. +=cut + +my @months_with_31_days = (1, 3, 5, 7, 8, 10, 12); + +for my $year (1900 .. 2019) { + for my $month (@months_with_31_days) { + my $first_day_of_month = DateTime->new( + year => $year, + month => $month, + day => 1, + ); + if ($first_day_of_month->day_of_week() == 5) { + printf "%04d-%02d\n", $year, $month; + } + } +} diff --git a/challenge-019/gustavo-chaves/perl5/ch-2.pl b/challenge-019/gustavo-chaves/perl5/ch-2.pl new file mode 100755 index 0000000000..90a6847d9f --- /dev/null +++ b/challenge-019/gustavo-chaves/perl5/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +# Write a script that can wrap the given paragraph at a specified column using +# the greedy algorithm: +# https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap#Minimum_number_of_lines + +use 5.026; +use strict; +use warnings; + +my $width = shift + or die "usage: $0 WIDTH\n"; + +$width =~ /^\d+$/ + or die "The WIDTH argument must be a number, not '$width'\n"; + +# Read by paragraphs +local $/ = ''; + +while (<>) { + chomp; + my ($line, @words) = split; + foreach my $word (@words) { + if (length($line) + 1 + length($word) <= $width) { + $line .= " $word"; + } else { + say $line; + $line = $word; + } + } + say $line if length($line); + say "\n"; +} |
