diff options
| -rwxr-xr-x | challenge-019/e-choroba/perl5/ch-1.pl | 18 | ||||
| -rwxr-xr-x | challenge-019/e-choroba/perl5/ch-2.pl | 74 |
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(); |
