From f802fec690a5d1d1bf1ea2033b70e721e7195055 Mon Sep 17 00:00:00 2001 From: "Gustavo L. de M. Chaves" Date: Tue, 30 Jul 2019 11:59:14 -0300 Subject: Gustavo Chaves's solutions to challenge 019 --- challenge-019/gustavo-chaves/perl5/ch-1.pl | 34 ++++++++++++++++++++++++++++++ challenge-019/gustavo-chaves/perl5/ch-2.pl | 32 ++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100755 challenge-019/gustavo-chaves/perl5/ch-1.pl create mode 100755 challenge-019/gustavo-chaves/perl5/ch-2.pl 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 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..f4df867955 --- /dev/null +++ b/challenge-019/gustavo-chaves/perl5/ch-2.pl @@ -0,0 +1,32 @@ +#!/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\n" if length($line); +} -- cgit From 22ab01e2270e02bdb0a0fff564d05b049cde22b5 Mon Sep 17 00:00:00 2001 From: "Gustavo L. de M. Chaves" Date: Tue, 30 Jul 2019 12:03:09 -0300 Subject: Fix insertion of paragraph separator --- challenge-019/gustavo-chaves/perl5/ch-2.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/challenge-019/gustavo-chaves/perl5/ch-2.pl b/challenge-019/gustavo-chaves/perl5/ch-2.pl index f4df867955..90a6847d9f 100755 --- a/challenge-019/gustavo-chaves/perl5/ch-2.pl +++ b/challenge-019/gustavo-chaves/perl5/ch-2.pl @@ -28,5 +28,6 @@ while (<>) { $line = $word; } } - say "$line\n" if length($line); + say $line if length($line); + say "\n"; } -- cgit