From 9a13a2fc0c93d4ff5de959e4f3b4abf0bec1c7f7 Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Tue, 30 Jul 2019 15:50:00 +0100 Subject: add solution to week 19 task 1 --- challenge-019/steven-wilson/perl5/ch-1.pl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 challenge-019/steven-wilson/perl5/ch-1.pl diff --git a/challenge-019/steven-wilson/perl5/ch-1.pl b/challenge-019/steven-wilson/perl5/ch-1.pl new file mode 100644 index 0000000000..1433da3bd7 --- /dev/null +++ b/challenge-019/steven-wilson/perl5/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +# Author: Steven Wilson +# Date: 2019-07-30 +# Week: 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 DateTime; +use feature qw/ say /; + +# If there are 31 days in a month and the first day of the month is a Friday +# then there are 5 weekends in a month. + +my $dt = DateTime->new( + year => 1900, + month => 1, + day => 1, +); + +while ( $dt->year() < 2020 ) { + if ( $dt->month_length() == 31 && $dt->dow() == 5 ) { + say $dt->year() . " " . $dt->month_name(); + } + $dt->add( months => 1 ); +} + -- cgit From 171b002d09482518b309a5c6a0f70f1621aad74a Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Tue, 30 Jul 2019 16:41:06 +0100 Subject: add solution to week 19 task 2 --- challenge-019/steven-wilson/perl5/ch-2.pl | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 challenge-019/steven-wilson/perl5/ch-2.pl diff --git a/challenge-019/steven-wilson/perl5/ch-2.pl b/challenge-019/steven-wilson/perl5/ch-2.pl new file mode 100644 index 0000000000..78785b9fa3 --- /dev/null +++ b/challenge-019/steven-wilson/perl5/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl +# Author: Steven Wilson +# Date: 2019-07-30 +# Week: 019 +# +# Task #2 +# Write a script that can wrap the given paragraph at a specified column +# using the greedy algorithm. + +use strict; +use warnings; + +my $text = "I saw the best minds of my generation destroyed by madness, +starving hysterical naked, dragging themselves through the negro streets +at dawn looking for an angry fix, Angel-headed hipsters burning for the +ancient heavenly connection to the starry dynamo in the machinery of +night,"; + +$text =~ s/\R/ /g; + +print "specify a column width: "; +chomp( my $line_width = <> ); + +my @words = split / /, $text; + +my $space_left = $line_width; + +for my $word (@words) { + if ( ( ( length $word ) + 1 ) > $space_left ) { + print "\n$word "; + $space_left = $line_width - length $word; + } + else { + print "$word "; + $space_left -= ( ( length $word ) + 1 ); + } +} +print "\n"; -- cgit