aboutsummaryrefslogtreecommitdiff
path: root/challenge-019/andrezgz
diff options
context:
space:
mode:
authorandrezgz <andrezgz@gmail.com>2019-07-30 17:33:15 -0300
committerandrezgz <andrezgz@gmail.com>2019-07-30 17:33:15 -0300
commita3b2f4a0fc50b93c8bdcb6cd4a62c65a42ae2a62 (patch)
tree343a43de895b16097ecea68a7f18063fc97ce171 /challenge-019/andrezgz
parent1dbcabd8e08dd98a40fe42ee3ce63e5755cdaaef (diff)
downloadperlweeklychallenge-club-a3b2f4a0fc50b93c8bdcb6cd4a62c65a42ae2a62.tar.gz
perlweeklychallenge-club-a3b2f4a0fc50b93c8bdcb6cd4a62c65a42ae2a62.tar.bz2
perlweeklychallenge-club-a3b2f4a0fc50b93c8bdcb6cd4a62c65a42ae2a62.zip
challenge-019 andrezgz solution
Diffstat (limited to 'challenge-019/andrezgz')
-rw-r--r--challenge-019/andrezgz/perl5/ch-1.pl24
-rw-r--r--challenge-019/andrezgz/perl5/ch-2.pl41
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-019/andrezgz/perl5/ch-1.pl b/challenge-019/andrezgz/perl5/ch-1.pl
new file mode 100644
index 0000000000..98bc876736
--- /dev/null
+++ b/challenge-019/andrezgz/perl5/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-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;
+
+my $y = $ARGV[0] || 1900;
+
+# 5 weekends in a month are only possible when
+# the last day of the month is Sunday 31st
+
+do {
+ # loop only through months with 31 days
+ for my $m ( 1, 3, 5, 7, 8, 10, 12 ) {
+ my $d = DateTime->last_day_of_month( year => $y, month => $m );
+ print $d->month_name.' '.$y."\n" if ( $d->dow == 7); #7 is Sunday as day of week
+ }
+} until (++$y > 2019);
diff --git a/challenge-019/andrezgz/perl5/ch-2.pl b/challenge-019/andrezgz/perl5/ch-2.pl
new file mode 100644
index 0000000000..153780a209
--- /dev/null
+++ b/challenge-019/andrezgz/perl5/ch-2.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-019/
+# Task #2
+# Write a script that can wrap the given paragraph at a specified
+# column using the greedy algorithm.
+# https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap#Minimum_number_of_lines
+
+use strict;
+use warnings;
+
+my $columns = $ARGV[0] || 40;
+my $text = $ARGV[1] || example_text();
+
+print wrap($text, $columns);
+
+sub wrap {
+ my ($content, $line_width) = @_;
+
+ my $output = '';
+
+ my $left = $line_width;
+ foreach my $w (split / /,$content){
+ my $l = length $w;
+ if ($l + 1 > $left) {
+ $output .= "\n";
+ $left = $line_width;
+ }
+ $output .= "$w ";
+ $left -= $l + 1;
+ }
+
+ return $output;
+}
+
+sub example_text {
+ return 'A simple way to do word wrapping is to use a greedy algorithm that puts as many words on a line as possible,'
+ .' then moving on to the next line to do the same until there are no more words left to place.'
+ .' This method is used by many modern word processors, such as OpenOffice.org Writer and Microsoft Word.'
+ .' This algorithm always uses the minimum possible number of lines but may lead to lines of widely varying lengths.';
+}