diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-08-04 10:10:01 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-08-04 10:10:01 +0100 |
| commit | 2a666cfcaf61b6708bfcf9df02c31b4e104a4205 (patch) | |
| tree | 75ae9af515c1512bee6a6b8e7cfa06c9fc47c78d /challenge-019 | |
| parent | b2cddc0a03d2e56f93544e940b565efa12eda7c3 (diff) | |
| download | perlweeklychallenge-club-2a666cfcaf61b6708bfcf9df02c31b4e104a4205.tar.gz perlweeklychallenge-club-2a666cfcaf61b6708bfcf9df02c31b4e104a4205.tar.bz2 perlweeklychallenge-club-2a666cfcaf61b6708bfcf9df02c31b4e104a4205.zip | |
- Added solutions by Guillermo Ramos.
Diffstat (limited to 'challenge-019')
| -rw-r--r-- | challenge-019/guillermo-ramos/perl5/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-019/guillermo-ramos/perl5/ch-2.pl | 31 |
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-019/guillermo-ramos/perl5/ch-1.pl b/challenge-019/guillermo-ramos/perl5/ch-1.pl new file mode 100644 index 0000000000..a3acb81cfa --- /dev/null +++ b/challenge-019/guillermo-ramos/perl5/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl +# +# 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 @months; +for my $year (1900 .. 2019) { + for my $month (1 .. 12) { + my $date = DateTime->last_day_of_month(year => $year, month => $month); + my $day = $date->day; + my $dow = $date->day_of_week(); + push @months, $date->strftime("%m/%Y") + if $day == 30 && $dow == 7 || $day == 31 && ($dow == 7 || $dow == 1); + } +} + +print "Months with 5 weekends: @months\n" diff --git a/challenge-019/guillermo-ramos/perl5/ch-2.pl b/challenge-019/guillermo-ramos/perl5/ch-2.pl new file mode 100644 index 0000000000..4f110cf500 --- /dev/null +++ b/challenge-019/guillermo-ramos/perl5/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl +# +# 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 $COLUMN = shift or die "Usage: $0 <column> [file]\n"; + +my @words; +while (<>) { + push @words, split " ", $_; +} + +my $col = 0; +my @out_line; +for my $word (@words) { + my $word_len = length $word; + if ($col + $word_len + 1> $COLUMN && @out_line) { + print "@out_line\n"; + @out_line = (); + $col = 0; + } + push @out_line, $word; + $col += $word_len + 1; +} +print "@out_line\n"; |
