From 0e15f2d7e6327261f8527ff1ebd91a385259b1f6 Mon Sep 17 00:00:00 2001 From: Noud Date: Fri, 2 Aug 2019 23:36:15 +0200 Subject: Solutions to challenge 19 problem 1 and 2 in perl 6 by Noud --- challenge-019/noud/perl6/ch-1.p6 | 16 ++++++++++++++++ challenge-019/noud/perl6/ch-2.p6 | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 challenge-019/noud/perl6/ch-1.p6 create mode 100644 challenge-019/noud/perl6/ch-2.p6 diff --git a/challenge-019/noud/perl6/ch-1.p6 b/challenge-019/noud/perl6/ch-1.p6 new file mode 100644 index 0000000000..0966ab2fdc --- /dev/null +++ b/challenge-019/noud/perl6/ch-1.p6 @@ -0,0 +1,16 @@ +# 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. + +my %months = .pairs; + +for 1900 .. 2019 -> $year { + for 1 .. 12 -> $month { + my $date = Date.new($year, $month, 1); + # A month can only have 5 Fridays, Saturdays, and Sundays if the month + # contains of 31 days, and the first day of the month is a Friday. + if ($date.days-in-month == 31 and $date.day-of-week == 5) { + say "{$date.year} %months{$month - 1}"; + } + } +} diff --git a/challenge-019/noud/perl6/ch-2.p6 b/challenge-019/noud/perl6/ch-2.p6 new file mode 100644 index 0000000000..ac661d25a9 --- /dev/null +++ b/challenge-019/noud/perl6/ch-2.p6 @@ -0,0 +1,21 @@ +# Write a script that can wrap the given paragraph at a specified column using +# the greedy algorithm. + +sub greedy_wrap($string, $width=79) { + my $line = ""; + for $string.split(/\s+/) -> $word { + if ($line.chars == 0) { + $line = $word; + } elsif ($width >= $line.chars + $word.chars) { + $line = "$line $word"; + } else { + $line.say; + $line = $word; + } + } + if ($line.chars > 0) { + $line.say; + } +} + +greedy_wrap("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.") -- cgit