aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-07-30 18:52:59 +0100
committerGitHub <noreply@github.com>2019-07-30 18:52:59 +0100
commit4ee98e1793d80ec052c986aa368596b0f6529f6c (patch)
treee0d5bce85fd122bd85d2b41df402b077a8663750
parent145f59407ae78145d6d16d05cb6039ca05e11f44 (diff)
parent832a1316bd92d26750ed4ba81d2611d8ed0291ff (diff)
downloadperlweeklychallenge-club-4ee98e1793d80ec052c986aa368596b0f6529f6c.tar.gz
perlweeklychallenge-club-4ee98e1793d80ec052c986aa368596b0f6529f6c.tar.bz2
perlweeklychallenge-club-4ee98e1793d80ec052c986aa368596b0f6529f6c.zip
Merge pull request #447 from waltman/branch-for-challenge-019
added perl 5 solutions for week 19 parts 1 and 2
-rw-r--r--challenge-019/walt-mankowski/perl5/ch-1.pl41
-rw-r--r--challenge-019/walt-mankowski/perl5/ch-2.pl52
2 files changed, 93 insertions, 0 deletions
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 = <<EOT;
+Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
+eiusmod tempor incididunt ut labore et dolore magna aliqua. Dolor sed
+viverra ipsum nunc aliquet bibendum enim. In massa tempor nec
+feugiat. Nunc aliquet bibendum enim facilisis gravida. Nisl nunc mi
+ipsum faucibus vitae aliquet nec ullamcorper. Amet luctus venenatis
+lectus magna fringilla. Volutpat maecenas volutpat blandit aliquam
+etiam erat velit scelerisque in. Egestas egestas fringilla phasellus
+faucibus scelerisque eleifend. Sagittis orci a scelerisque purus
+semper eget duis. Nulla pharetra diam sit amet nisl suscipit. Sed
+adipiscing diam donec adipiscing tristique risus nec feugiat in. Fusce
+ut placerat orci nulla. Pharetra vel turpis nunc eget lorem
+dolor. Tristique senectus et netus et malesuada.
+EOT
+
+my $width = 80;
+
+say greedy_wrap($txt, $width);
+
+sub greedy_wrap($txt, $width) {
+ my @words = split /\s+/, $txt;
+ my $para = shift @words;
+ my $space_width = 1;
+
+ my $left = $width - length($para);
+ for my $word (@words) {
+ if (length($word) + $space_width > $left) {
+ $para .= "\n$word";
+ $left = $width - length($word);
+ } else {
+ $para .= " " x $space_width;
+ $para .= $word;
+ $left -= $space_width + length($word);
+ }
+ }
+
+ return $para;
+}