aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-019/gustavo-chaves/perl5/ch-1.pl34
-rwxr-xr-xchallenge-019/gustavo-chaves/perl5/ch-2.pl32
2 files changed, 66 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..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);
+}