aboutsummaryrefslogtreecommitdiff
path: root/challenge-019
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-07-30 18:59:54 +0100
committerGitHub <noreply@github.com>2019-07-30 18:59:54 +0100
commit44d01fe26b41ffce02e12a7eaf1c0eb554fddfeb (patch)
tree8f594a96d1f75221bf94668f888931403e6fc5ac /challenge-019
parent0c497f90df9d41b10ab6f8356748c97c0e27e333 (diff)
parent7e4c9d5f139686805c119c36f474946cc50db908 (diff)
downloadperlweeklychallenge-club-44d01fe26b41ffce02e12a7eaf1c0eb554fddfeb.tar.gz
perlweeklychallenge-club-44d01fe26b41ffce02e12a7eaf1c0eb554fddfeb.tar.bz2
perlweeklychallenge-club-44d01fe26b41ffce02e12a7eaf1c0eb554fddfeb.zip
Merge pull request #448 from choroba/ech19
Add solution to 019 by E. Choroba
Diffstat (limited to 'challenge-019')
-rwxr-xr-xchallenge-019/e-choroba/perl5/ch-1.pl18
-rwxr-xr-xchallenge-019/e-choroba/perl5/ch-2.pl74
2 files changed, 92 insertions, 0 deletions
diff --git a/challenge-019/e-choroba/perl5/ch-1.pl b/challenge-019/e-choroba/perl5/ch-1.pl
new file mode 100755
index 0000000000..f6b0f73fa4
--- /dev/null
+++ b/challenge-019/e-choroba/perl5/ch-1.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature qw{ say };
+
+use Time::Piece;
+use Time::Seconds;
+
+my $date = 'Time::Piece'->strptime(
+ '1900-01-01 12:00:00', '%Y-%m-%d %H:%M:%S');
+
+while ($date->year < 2020) {
+ $date += ONE_DAY until $date->day eq 'Fri';
+ if ($date->mday == 1 && $date->month_last_day == 31) {
+ say $date->strftime('%Y-%m');
+ }
+ $date += ($date->month_last_day - $date->mday + 1) * ONE_DAY;
+}
diff --git a/challenge-019/e-choroba/perl5/ch-2.pl b/challenge-019/e-choroba/perl5/ch-2.pl
new file mode 100755
index 0000000000..d6705639d3
--- /dev/null
+++ b/challenge-019/e-choroba/perl5/ch-2.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+sub wrap_paragraph {
+ my ($paragraph, $width) = @_;
+ $paragraph =~ s/ *\n */ /g;
+ my $out = "";
+ while (length $paragraph) {
+ my $pos;
+ if (length $paragraph <= $width) {
+ $pos = length $paragraph;
+ } else {
+ $pos = $width;
+ --$pos until ' ' eq substr $paragraph, $pos, 1
+ or $pos < 0;
+ $pos = $width if $pos < 0;
+ }
+ $out .= substr $paragraph, 0, $pos, "";
+ $paragraph =~ s/^ +//;
+ $out .= "\n" if length $paragraph;
+ }
+ return $out
+}
+
+use Test::More;
+
+my %expected = (
+ 'a' => "a",
+ 'bcdef' => "bcdef",
+ 'ghijkl' => "ghijk\nl",
+ 'm no pq' => "m no\npq",
+ 'rs tu vw' => "rs tu\nvw",
+ 'xyz ab cd' => "xyz\nab cd",
+ 'efgh ij kl' => "efgh\nij kl",
+ 'mnopq rs tu' => "mnopq\nrs tu",
+ 'vwxyza bc de' => "vwxyz\na bc\nde",
+ 'fghijkl mn op' => "fghij\nkl mn\nop",
+ 'qrstuvwx yz ab' => "qrstu\nvwx\nyz ab",
+ 'cdefghijk lm no' => "cdefg\nhijk\nlm no",
+ 'pqrstuvwxy za bc' => "pqrst\nuvwxy\nza bc",
+ 'defghijklmn op qr' => "defgh\nijklm\nn op\nqr",
+ "s\nt" => "s t",
+ "u\nv\n w" => "u v w",
+ << '__PAR__', << '__EXPECTED__' =~ s/\n$//r);
+ABCDE
+AABBCC
+A BB CC
+AA BB CC
+AAA BB CC
+AAAA BB CC
+AAAAA BB CC
+__PAR__
+ABCDE
+AABBC
+C A
+BB CC
+AA BB
+CC
+AAA
+BB CC
+AAAA
+BB CC
+AAAAA
+BB CC
+__EXPECTED__
+
+for my $in (keys %expected) {
+ is wrap_paragraph($in, 5), $expected{$in}, $in;
+ is wrap_paragraph($expected{$in}, 5), $expected{$in},
+ "idempotent $expected{$in}";
+}
+
+done_testing();