aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-259/dave-jacoby/perl/ch-1.pl53
-rw-r--r--challenge-259/dave-jacoby/perl/ch-2.pl73
2 files changed, 126 insertions, 0 deletions
diff --git a/challenge-259/dave-jacoby/perl/ch-1.pl b/challenge-259/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..ce49296dde
--- /dev/null
+++ b/challenge-259/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+use DateTime;
+use List::Util qw{ any };
+
+my @examples = (
+
+ {
+ start_date => '2018-06-28',
+ offset => 3,
+ bank_holidays => ['2018-07-03']
+ },
+ { start_date => '2018-06-28', offset => 3 },
+ { start_date => '2019-11-01', offset => 3 },
+);
+
+for my $example (@examples) {
+ my $output = banking_day_offset($example);
+ my $input = '';
+ $input .= qq{\$startdate = $example->{start_date}};
+ $input .= qq{, \$offset = $example->{offset}};
+ $input .=
+ qq{, \$bank_holidays = [}
+ . ( join ', ', map { qq{'$_'} } $example->{bank_holidays}->@* ) . ']'
+ if defined $example->{bank_holidays};
+
+ say <<~"END";
+ Input: $input
+ Output: $output
+ END
+}
+
+sub banking_day_offset ($obj) {
+ my @bank_holidays ;
+ @bank_holidays = $obj->{bank_holidays}->@* if defined $obj->{bank_holidays};
+
+ my ( $y, $m, $d ) = split /-/, $obj->{start_date};
+ my $dt = DateTime->new( year => $y, month => $m, day => $d );
+ my $c = 0;
+ while ( $c < $obj->{offset} ) {
+ $dt->add( days => 1 );
+ next if $dt->day_of_week == 6; # Saturday
+ next if $dt->day_of_week == 7; # Sunday
+ next if any { $dt->ymd eq $_ } @bank_holidays;
+ $c++;
+ }
+
+ return $dt->ymd;
+}
diff --git a/challenge-259/dave-jacoby/perl/ch-2.pl b/challenge-259/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..807c4e0a45
--- /dev/null
+++ b/challenge-259/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+use JSON;
+use List::Util qw{ sum0 };
+
+my $json = JSON->new->pretty->canonical;
+
+my @examples = (
+
+ '{% youtube title="Title with escaped backslash \\" %}',
+ '{% id field1="value1" field2="value2" field3=42 %}',
+ '{% jacoby language1="perl" language2="javascript" hobby="guitar" %}',
+ '{% hansolo ship="falcon" friend="wookie" love="leia" %}',
+ '{% linkedin jobs="multiple words in one line" %}',
+ '{% youtube answer=42 title="Title \"quoted\" done" %}',
+);
+
+for my $example (@examples) {
+ my $output = line_parse($example);
+ my $jo = $json->encode($output);
+
+ say <<~"END";
+ Input: \$line = '$example'
+
+ Output:
+ $jo
+ END
+}
+
+sub line_parse ($line) {
+ my $output = {};
+ while ( $line !~ /^\{\% \s* \%\}/ ) {
+
+ # value matches word="word"
+ if ( $line =~ /^\{\% \s* \w+=\"\w+\"/ ) {
+ my ( $field, $value ) = $line =~ /(\w+)=\"(\w+)\"\s/;
+ $output->{field}{$field} = $value;
+ $line =~ s{(\w+=\"\w+\")\s}{};
+ next;
+ }
+
+ # value matches word=number
+ if ( $line =~ /^\{\% \s* \w+=\d+/ ) {
+ my ( $field, $value ) = $line =~ /(\w+)=(\d+)\s/;
+ $output->{field}{$field} = $value;
+ $line =~ s{(\w+=\d+)\s}{};
+ next;
+ }
+
+ # value matches word="word word word" and also backslash
+ if ( $line =~ /^\{\% \s* \w+=\"[\s\w\\\"]+\"/ ) {
+ my ( $field, $value ) = $line =~ /(\w+)=\"([\s\w\\\"]+)\"\s/;
+ $output->{field}{$field} = $value;
+ $line =~ s{(\w+=\"[\s\w\\\"]+\")\s}{};
+ next;
+ }
+
+ # value matches only word
+ if ( $line =~ /^\{\% \s* \w+/ ) {
+ my ($field) = $line =~ m{(\w+)};
+ $line =~ s{(\w+)}{}mix;
+ if ( $output->{name} ) { $output->{field}{$field} = ''; }
+ else { $output->{name} = $field; }
+ next;
+ }
+ substr( $line, 3, 1 ) = '';
+ }
+ return $output;
+}