aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-07-30 19:01:53 +0100
committerGitHub <noreply@github.com>2019-07-30 19:01:53 +0100
commit0e6069c11aed545c885707e07bfb0abe8ef4965e (patch)
tree729205744a0c55f5be52bff0a3da3d044dc6ffac
parent5bd758e78c0d71136b6d9e94f07fa08266c70120 (diff)
parent171b002d09482518b309a5c6a0f70f1621aad74a (diff)
downloadperlweeklychallenge-club-0e6069c11aed545c885707e07bfb0abe8ef4965e.tar.gz
perlweeklychallenge-club-0e6069c11aed545c885707e07bfb0abe8ef4965e.tar.bz2
perlweeklychallenge-club-0e6069c11aed545c885707e07bfb0abe8ef4965e.zip
Merge pull request #449 from oWnOIzRi/week019
add solution to week 19 task 1
-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";