From 832a1316bd92d26750ed4ba81d2611d8ed0291ff Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Mon, 29 Jul 2019 15:10:52 -0400 Subject: added perl 5 solutions for week 19 parts 1 and 2 --- challenge-019/walt-mankowski/perl5/ch-1.pl | 41 +++++++++++++++++++++++ challenge-019/walt-mankowski/perl5/ch-2.pl | 52 ++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 challenge-019/walt-mankowski/perl5/ch-1.pl create mode 100644 challenge-019/walt-mankowski/perl5/ch-2.pl diff --git a/challenge-019/walt-mankowski/perl5/ch-1.pl b/challenge-019/walt-mankowski/perl5/ch-1.pl new file mode 100644 index 0000000000..0509d81b26 --- /dev/null +++ b/challenge-019/walt-mankowski/perl5/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +# Perl Weekly Challenge 019-1 +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-019/ +# +# 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 feature qw(:5.30); +use experimental qw(signatures); +use Time::Local; + +# Looking at March 2019 it's clear that this can only happen if the +# month has 31 days and starts on a Friday: +# +# March 2019 +# Su Mo Tu We Th Fr Sa +# 1 2 +# 3 4 5 6 7 8 9 +# 10 11 12 13 14 15 16 +# 17 18 19 20 21 22 23 +# 24 25 26 27 28 29 30 +# 31 + +my @months = (0, 2, 4, 6, 7, 9, 11); + +for my $year (1900..2019) { + for my $month (@months) { + if (starts_on_friday($year, $month)) { + printf "%d/%02d\n", $year, $month+1; + } + } +} + +# does the month starts with a Friday? +sub starts_on_friday($year, $month) { + my $time = timelocal(undef, undef, undef, 1, $month, $year); + return (localtime($time))[6] == 5; +} diff --git a/challenge-019/walt-mankowski/perl5/ch-2.pl b/challenge-019/walt-mankowski/perl5/ch-2.pl new file mode 100644 index 0000000000..590ebe5a1f --- /dev/null +++ b/challenge-019/walt-mankowski/perl5/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl + +# Perl Weekly Challenge 019-2 +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-019/ +# +# Write a script that can wrap the given paragraph at a specified +# column using the greedy algorithm specifed at +# https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap#Minimum_number_of_lines + +use strict; +use warnings; +use feature qw(:5.30); +use experimental qw(signatures); + +my $txt = < $left) { + $para .= "\n$word"; + $left = $width - length($word); + } else { + $para .= " " x $space_width; + $para .= $word; + $left -= $space_width + length($word); + } + } + + return $para; +} -- cgit