From 18d5c307feff8dfeddfbc56bf2be26b83e77011f Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Wed, 20 Jan 2021 23:23:11 +0000 Subject: Add Perl solutions to challenge 019 --- challenge-019/paulo-custodio/README | 1 + challenge-019/paulo-custodio/perl/ch-1.pl | 32 ++++++ challenge-019/paulo-custodio/perl/ch-2.pl | 38 +++++++ challenge-019/paulo-custodio/test.pl | 158 ++++++++++++++++++++++++++++++ 4 files changed, 229 insertions(+) create mode 100644 challenge-019/paulo-custodio/README create mode 100644 challenge-019/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-019/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-019/paulo-custodio/test.pl diff --git a/challenge-019/paulo-custodio/README b/challenge-019/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-019/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-019/paulo-custodio/perl/ch-1.pl b/challenge-019/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..2dd314bf0a --- /dev/null +++ b/challenge-019/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +# Challenge 019 +# +# Task #1 +# 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 strict; +use warnings; +use 5.030; +use Date::Calc qw( Nth_Weekday_of_Month_Year ); +use constant { + Friday => 5, + Saturday => 6, + Sunday => 7 +}; + +for my $year (1900 .. 2019) { + for my $month (1 .. 12) { + say sprintf("%04d-%02d", $year, $month) if five_weekends($year, $month); + } +} + + +sub five_weekends { + my($year, $month) = @_; + return unless Nth_Weekday_of_Month_Year($year, $month, Friday, 5); + return unless Nth_Weekday_of_Month_Year($year, $month, Saturday, 5); + return unless Nth_Weekday_of_Month_Year($year, $month, Sunday, 5); + return 1; +} diff --git a/challenge-019/paulo-custodio/perl/ch-2.pl b/challenge-019/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..7eaecbe7b2 --- /dev/null +++ b/challenge-019/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +# Challenge 019 +# +# Task #2 +# Write a script that can wrap the given paragraph at a specified column using the greedy algorithm. + +use strict; +use warnings; +use 5.030; + +my $text = ""; +while (<>) { + $text .= $_; +} +print wrap($text, 72); + + +sub wrap { + my($text, $column) = @_; + $column ||= 72; + + my $output = ""; + my $sep = ""; + my $pos = 0; + for my $word (split(' ', $text)) { + if ($pos + length($sep) + length($word) >= $column) { + $output .= "\n"; + $sep = ""; + $pos = 0; + } + $output .= $sep . $word; + $pos += length($sep) + length($word); + $sep = " "; + } + $output .= "\n"; + return $output; +} diff --git a/challenge-019/paulo-custodio/test.pl b/challenge-019/paulo-custodio/test.pl new file mode 100644 index 0000000000..468b65a361 --- /dev/null +++ b/challenge-019/paulo-custodio/test.pl @@ -0,0 +1,158 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; +use 5.030; +use Path::Tiny; + +is capture("perl perl/ch-1.pl"), <spew(<