From 30ccbf4e84db2cd80fcbb89f991e073b174f8288 Mon Sep 17 00:00:00 2001 From: "Kian-Meng, Ang" Date: Sun, 4 Aug 2019 01:47:27 +0800 Subject: Added task 1 and 2 solution --- challenge-019/kian-meng-ang/perl5/ch-1.pl | 25 ++++++++++++ challenge-019/kian-meng-ang/perl5/ch-2.pl | 67 +++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 challenge-019/kian-meng-ang/perl5/ch-1.pl create mode 100644 challenge-019/kian-meng-ang/perl5/ch-2.pl diff --git a/challenge-019/kian-meng-ang/perl5/ch-1.pl b/challenge-019/kian-meng-ang/perl5/ch-1.pl new file mode 100644 index 0000000000..b29fa569c8 --- /dev/null +++ b/challenge-019/kian-meng-ang/perl5/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +# vi:et:sw=4 ts=4 ft=perl + +use strict; +use warnings; +use utf8; +use feature qw(say); + +use Date::Calc qw(Days_in_Month Day_of_Week); + +# Naive and brute force approach, just exploring. +foreach my $y (1900..2019) { + foreach my $m (1..12) { + my %dow_count = (); + foreach my $d (1..Days_in_Month($y, $m)) { + $dow_count{Day_of_Week($y, $m, $d)}++; + } + + if ($dow_count{5} == 5 && $dow_count{6} == 5 && $dow_count{7} == 5) { + say sprintf '%s-%02d', $y, $m; + } + } +} + +1; diff --git a/challenge-019/kian-meng-ang/perl5/ch-2.pl b/challenge-019/kian-meng-ang/perl5/ch-2.pl new file mode 100644 index 0000000000..8ba055d0d4 --- /dev/null +++ b/challenge-019/kian-meng-ang/perl5/ch-2.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +# vi:et:sw=4 ts=4 ft=perl + +use strict; +use warnings; +use utf8; +use feature qw(say signatures); +no warnings qw(experimental::signatures); + +use Data::Section::Simple qw(get_data_section); + +MAIN: { + say sprintf "Line length: %s\n%s\n", $_, wrap(get_data_section('sample'), $_) + foreach map { 20 * $_ } 1..3; +} + +sub wrap ($text, $line_width = 60) { + my $sep = ' '; + my $space_left = $line_width; + my $space_width = length $sep; + my $wtext = ''; + + foreach my $word (split $sep, $text) { + my $word_len = length $word; + if ($word_len + $space_width > $space_left) { + $wtext .= "\n$word$sep"; + $space_left = $line_width - $word_len; + } else { + $space_left = $space_left - ($word_len + $space_width); + $wtext .= "$word$sep"; + } + } + return $wtext; +} + +1; + +__DATA__ + +@@ sample +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 + +__END__ + +$ perl ch-2.pl +Line length: 20 +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 + +Line length: 40 +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 + +Line length: 60 +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 -- cgit