aboutsummaryrefslogtreecommitdiff
path: root/challenge-019
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-019')
-rw-r--r--challenge-019/duane-powell/perl5/ch-1.pl45
-rw-r--r--challenge-019/duane-powell/perl5/ch-2.pl43
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-019/duane-powell/perl5/ch-1.pl b/challenge-019/duane-powell/perl5/ch-1.pl
new file mode 100644
index 0000000000..9ecc266ec0
--- /dev/null
+++ b/challenge-019/duane-powell/perl5/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+use Modern::Perl;
+use DateTime;
+
+# Find all year/months from 1900 to 2019 with 5 full weekends.
+# Noting that it must be a month with 31 days and begin on a Friday yields:
+my $FRIDAY = 5;
+
+# Popluate a hash with months that have 31 days
+my @mon_val = qw( Jan Mar May Jul Aug Oct Dec );
+my @mon_key = (1,3,5,7,8,10,12);
+my %mon_31_days;
+@mon_31_days{@mon_key} = @mon_val;
+
+foreach my $year (1900 .. 2019) {
+ foreach my $month (keys %mon_31_days) {
+ my $dt = DateTime->new(
+ 'year' => $year,
+ 'month' => $month,
+ 'day' => 1,
+ );
+
+ if ($dt->day_of_week == $FRIDAY) {
+ say "$mon_31_days{$month} $year";
+ }
+ }
+}
+
+__END__
+
+./ch1.pl
+Mar 1901
+Aug 1902
+May 1903
+Jan 1904
+Jul 1904
+Dec 1905
+...
+Mar 2013
+Aug 2014
+May 2015
+Jan 2016
+Jul 2016
+Dec 2017
+Mar 2019
diff --git a/challenge-019/duane-powell/perl5/ch-2.pl b/challenge-019/duane-powell/perl5/ch-2.pl
new file mode 100644
index 0000000000..946df42db5
--- /dev/null
+++ b/challenge-019/duane-powell/perl5/ch-2.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/perl
+use Modern::Perl;
+
+# A script that can wrap the given paragraph at a specified column using the greedy algorithm
+
+my ($max_col,$text) = @ARGV;
+$max_col ||= 45;
+$text ||= "Hello world, it's a very nice day! The sun is shining. Rainbows and Unicorns!";
+
+my @word = split(/ /,$text);
+my %word = map { $_ => length($_) } @word;
+
+my $line = "";
+my $c = 0;
+foreach my $w (@word) {
+ if (($c + $word{$w}) <= $max_col) {
+ $c += $word{$w}+1;
+ $line .= "$w ";
+ } else {
+ say $line;
+ $line = "$w ";
+ $c = $word{$w};
+ }
+}
+say $line if ($line);
+
+__END__
+
+./ch2.pl 15 "Hello world, it's a very nice day! The sun is shining. Rainbows and Unicorns!"
+Hello world,
+it's a very nice
+day! The sun is
+shining.
+Rainbows and
+Unicorns!
+
+./ch2.pl 45 "Hello world, it's a very nice day! The sun is shining. Rainbows and Unicorns!"
+Hello world, it's a very nice day! The sun is
+shining. Rainbows and Unicorns!
+
+./ch2.pl 75 "Hello world, it's a very nice day! The sun is shining. Rainbows and Unicorns!"
+Hello world, it's a very nice day! The sun is shining. Rainbows and
+Unicorns!