aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-019/steven-wilson/perl5/ch-1.pl30
-rw-r--r--challenge-019/steven-wilson/perl5/ch-2.pl38
2 files changed, 68 insertions, 0 deletions
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 );
+}
+
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";