aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-03-11 00:06:56 +0000
committerGitHub <noreply@github.com>2024-03-11 00:06:56 +0000
commit0a45a6346f85a3b3547af3ff83c109a13c443c62 (patch)
tree060e62d59ad6695395f85603ed8e4295e1fa9863
parent794d1e93f6805188d359be19fcaa1e9237e85f3b (diff)
parent449807c538cb5312ee2f2f256129f491299b3cce (diff)
downloadperlweeklychallenge-club-0a45a6346f85a3b3547af3ff83c109a13c443c62.tar.gz
perlweeklychallenge-club-0a45a6346f85a3b3547af3ff83c109a13c443c62.tar.bz2
perlweeklychallenge-club-0a45a6346f85a3b3547af3ff83c109a13c443c62.zip
Merge pull request #9719 from Util/c259
Add TWC 259 solutions by Bruce Gray, in Raku only.
-rw-r--r--challenge-259/bruce-gray/raku/ch-1.raku62
-rw-r--r--challenge-259/bruce-gray/raku/ch-2.raku52
2 files changed, 114 insertions, 0 deletions
diff --git a/challenge-259/bruce-gray/raku/ch-1.raku b/challenge-259/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..267d4eb4ac
--- /dev/null
+++ b/challenge-259/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,62 @@
+sub task1 ( Date $start, UInt $offset, @holidays --> Date ) {
+
+ sub is_business_day ($d) { $d.day-of-week ~~ 1..5 and $d ∉ @holidays }
+
+ return ($start .. *).grep(&is_business_day)[$offset];
+}
+
+
+use Test; plan +my @tests =
+ ( '2018-07-04'.Date, '2018-06-28'.Date, 3, <2018-07-03>».Date , 'Example 1' ),
+ ( '2018-07-03'.Date, '2018-06-28'.Date, 3, [] , 'Example 2' ),
+
+ # Extra tests from ../e-choroba/perl/
+ ( '2018-07-04'.Date, '2018-06-28'.Date, 2, <2018-07-02 2018-07-03>».Date , 'Land in the middle of holidays' ),
+ ( '2018-07-02'.Date, '2018-06-28'.Date, 2, <2018-07-01 2018-06-30>».Date , 'Holidays on a weekend' ),
+ ( '2025-01-01'.Date, '2024-01-01'.Date, 262, [] , 'Whole year' ),
+ ( '2018-11-20'.Date, '2018-06-28'.Date, 101, <2018-11-16 2018-11-19>».Date , 'Holidays wrap a weekend' ),
+ ( '2013-01-03'.Date, '2012-05-22'.Date, 161, <2012-05-22>».Date , 'Start on a holiday' ),
+ ( '2006-04-05'.Date, '2003-04-20'.Date, 731, <
+ 2001-10-08 2005-02-20 2000-01-07 2003-09-26 2000-08-14
+ 2008-09-22 2003-05-10 2004-05-12 2004-10-12 2008-06-12
+ 2003-06-15 2003-09-20 2006-08-01 2009-02-18 2006-07-11
+ 2008-04-01 2003-10-03 2008-08-07 2008-11-17 2009-02-03
+ 2004-08-18 2005-04-23 2003-08-15 2007-03-22 2004-11-07
+ 2004-08-13 2008-09-04 2003-06-27 2006-07-12 2003-11-06
+ 2000-01-30 2006-11-26 2004-07-05 2007-03-07 2000-12-11
+ 2001-01-17 2007-01-18 2002-05-01 2000-01-06 2000-03-03
+ 2005-09-05 2001-01-03 2005-06-02 2003-08-15 2002-09-13
+ 2006-07-15 2005-06-22 2001-10-27 2005-07-14 2004-09-19
+ 2008-02-10 2003-05-10 2007-08-11 2000-02-05 2002-01-25
+ 2002-03-28 2003-07-26 2007-08-13 2002-03-21 2003-03-09
+ 2006-03-11 2004-03-05 2004-05-08 2006-09-24 2000-10-03
+ 2001-12-19 2003-02-26 2005-10-06 2001-08-23 2004-09-25
+ 2009-12-20 2004-10-10 2005-08-15 2001-11-25 2002-03-11
+ 2007-10-22 2000-10-30 2009-04-14 2009-10-30 2004-09-01
+ 2004-04-11 2000-04-04 2003-11-14 2004-11-16 2001-06-28
+ 2008-11-18 2009-11-16 2006-01-27 2007-08-06 2009-09-14
+ 2000-10-25 2001-09-14 2000-09-17 2007-01-07 2005-02-05
+ 2000-09-20 2002-02-01 2003-05-08 2002-06-03 2006-12-02
+ 2009-08-15 2008-11-22 2002-12-23 2002-06-08 2003-09-27
+ 2004-10-08 2007-12-16 2005-12-19 2003-05-15 2007-10-30
+ 2006-11-13 2005-12-04 2006-09-06 2005-05-08 2007-10-23
+ 2006-05-31 2005-01-16 2009-02-15 2000-05-08 2002-04-13
+ 2000-07-11 2005-05-25 2004-07-03 2007-12-03 2008-07-19
+ 2009-08-27 2004-08-27 2002-03-14 2007-03-29 2005-02-03
+ 2004-10-30 2000-07-14 2004-01-27 2004-12-18 2004-12-08
+ 2005-11-23 2008-04-18 2000-03-06 2009-05-31 2002-08-13
+ 2000-12-25 2008-09-17 2004-06-10 2003-04-29 2003-04-28
+ 2000-04-30 2005-04-18 2003-08-01 2000-05-22 2009-03-18
+ 2002-08-08 2008-11-15 2006-03-17 2003-07-17 2006-10-02
+ 2007-01-17 2009-09-04 2000-04-22 2007-04-23 2006-01-08
+ 2003-08-01 2003-08-11 2003-02-10 2007-04-08 2003-02-26
+ 2002-05-16 2002-11-04 2004-01-07 2001-09-28 2001-11-29
+ 2002-03-19 2009-10-08 2002-08-25 2004-08-22 2003-06-23
+ 2001-05-23 2000-12-02 2000-04-26 2000-05-25 2006-05-15
+ 2006-08-18 2009-12-26 2008-07-31 2009-10-02 2002-07-19
+ 2006-08-01 2000-06-09 2006-04-10
+ >».Date, 'Huge set of holidays' ),
+;
+for @tests -> ( $expected, $start, $offset, @holidays, $desc ) {
+ is task1( $start, $offset, @holidays ), $expected, $desc;
+}
diff --git a/challenge-259/bruce-gray/raku/ch-2.raku b/challenge-259/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..f98a9a6311
--- /dev/null
+++ b/challenge-259/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,52 @@
+grammar Task2_Record {
+ rule TOP { ^ '{%' <id> <field>* '%}' $ }
+ rule field { <field_name> '=' <field_value> }
+ token id { \w+ }
+ token field_name { \w+ }
+ token field_value { <number> | <string> }
+ token number { \d+ }
+
+ # From https://andrewshitov.com/2020/03/29/chapter-6-working-with-strings/
+ token string {
+ '"' ( [
+ | <-["\\]>+
+ | '\\"'
+ | '\\\\'
+ ]* )
+ '"'
+ }
+}
+class Task2_Actions {
+ method TOP ($/) {
+ $/.make: {
+ name => ~$/<id>,
+ fields => $/<field>».made.Hash,
+ };
+ }
+ method field ($/) { $/.make: ~$/<field_name> => $/<field_value>.made }
+ method field_value ($/) { $/.make: ( $/<number> // $/<string> ).made }
+ method string ($/) { $/.make: ~$/[0] }
+ method number ($/) { $/.make: +$/ }
+}
+sub task2 ( Str $s ) {
+ my $p = Task2_Record.parse($s, :actions(Task2_Actions))
+ orelse die;
+ return $p.made;
+}
+
+
+# No time for the Bonus this week.
+my @tests =
+ 「{% id field1="value1" field2="value2" field3=42 %}」,
+ 「{% youtube title="Title \"quoted\" done" %}」,
+ 「{% youtube title="Title with escaped backslash \\" %}」,
+;
+my @expected =
+ { name => 'id' , fields => { field1 => 'value1', field2 => 'value2', field3 => 42 } },
+ { name => 'youtube' , fields => { title => 'Title \"quoted\" done' }, },
+ { name => 'youtube' , fields => { title => 「Title with escaped backslash \\」 }, },
+;
+use Test; plan +@tests;
+for @tests Z @expected -> (Str $in, %exp) {
+ is-deeply task2($in), %exp;
+}