aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-259/nelo-tovar/bash/ch-1.sh47
-rw-r--r--challenge-259/nelo-tovar/bash/ch-2.pl46
-rwxr-xr-xchallenge-259/nelo-tovar/bash/ch-2.sh37
-rw-r--r--challenge-259/nelo-tovar/perl/ch-1.pl55
-rw-r--r--challenge-259/nelo-tovar/perl/ch-2.pl45
5 files changed, 230 insertions, 0 deletions
diff --git a/challenge-259/nelo-tovar/bash/ch-1.sh b/challenge-259/nelo-tovar/bash/ch-1.sh
new file mode 100755
index 0000000000..1f360d6a59
--- /dev/null
+++ b/challenge-259/nelo-tovar/bash/ch-1.sh
@@ -0,0 +1,47 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 259 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-259/
+#
+# Task 1 : Banking Day Offset
+
+function banking_day_offset() {
+ local date_offset=$1
+ local offset=$2
+ local bank_holidays=$3
+
+ for (( i = 1; i <= $offset; i++ )); do
+ date_offset=$(date +%Y-%m-%d -d "$date_offset + 1 day")
+
+ while [[ $(date +%u -d $date_offset) =~ [67] ]]; do
+ date_offset=$(date +%Y-%m-%d -d "$date_offset + 1 day")
+ done
+
+ if [[ $bank_holidays && "$date_offset" == *"$bank_holidays"* ]]; then
+ date_offset=$(date +%Y-%m-%d -d "$date_offset + 1 day")
+ fi
+ done
+
+ echo $date_offset
+}
+
+example=('2018-06-28' '2018-06-28')
+example_offset=(3 3)
+example_bank_holidays=('2018-07-03')
+
+for e in 0 1; do
+ start_date=${example[$e]}
+ offset=${example_offset[$e]}
+ bank_holidays=${example_bank_holidays[$e]}
+
+ bdo=$(banking_day_offset "$start_date" $offset $bank_holidays)
+ echo -n "Input : start_date = $start_date, offset = $offset"
+ if [ $bank_holidays ]; then
+ echo -n ", bank_holidays = $bank_holidays"
+ fi
+ echo " "
+ echo "Output : $bdo"
+ echo ""
+done
+
diff --git a/challenge-259/nelo-tovar/bash/ch-2.pl b/challenge-259/nelo-tovar/bash/ch-2.pl
new file mode 100644
index 0000000000..04b31f8881
--- /dev/null
+++ b/challenge-259/nelo-tovar/bash/ch-2.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 259 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-259/
+#
+# Task 2 - Line Parser
+#
+
+use strict;
+use warnings;
+use feature qw/say/;
+use Data::Dump qw/dump/;
+
+my @examples = (
+ '{% youtube title="Title \"quoted\" done" %}',
+ '{% youtube title="Title with escaped backslash \\" %}',
+);
+
+sub line_parser {
+ my $original_line = shift;
+ my %line_parsed;
+
+ if ( $original_line =~ /^{%\s+(\w+)(\s+.+)\s*%}/) {
+ $line_parsed{name} = $1;
+
+ $original_line =~ s/\\"/\034/g;
+
+ while ($original_line =~ /(\w+)="?(\d+|[\w\034\s]+)"?/cg) {
+ my $field_name = $1;
+ my $value = $2;
+ say "name = $field_name value = $value";
+ $value =~ s/\034/"/g;
+ $line_parsed{fields}->{$field_name} = $value;
+ }
+ }
+
+ return \%line_parsed;
+}
+
+for my $elements (@examples) {
+ my $lp = line_parser $elements;
+
+ say dump($lp);
+ say ' ';
+}
diff --git a/challenge-259/nelo-tovar/bash/ch-2.sh b/challenge-259/nelo-tovar/bash/ch-2.sh
new file mode 100755
index 0000000000..cf8c4ab389
--- /dev/null
+++ b/challenge-259/nelo-tovar/bash/ch-2.sh
@@ -0,0 +1,37 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 259 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-259/
+#
+# Task 2 : Line Parser
+
+function line_parser() {
+ local original_line=$1
+ local line_parsed='{\n'
+
+ if [[ "$original_line" =~ ^\{%[[:space:]]+([[:alpha:]]+)[[:space:]]+[[:alpha:]]+[[:space:]]* ]]; then
+ line_parsed+="\tname => ${BASH_REMATCH[1]}\n"
+ line_parsed+="\tfields => {\n"
+
+ while [[ $original_line =~ ([[:alpha:]]+)=\"?([[:alnum:][:space:]\"\\]+) ]]; do
+ line_parsed+="\t\t${BASH_REMATCH[1]} => ${BASH_REMATCH[2]},\n"
+ original_line=${original_line/${BASH_REMATCH[1]}=/}
+ original_line=${original_line/${BASH_REMATCH[2]}/}
+ done
+ line_parsed+="\t}\n"
+ fi
+ line_parsed+='}'
+ echo $line_parsed
+}
+
+examples=(
+ '{% youtube title="Title \"quoted\" done" %}'
+ '{% youtube title="Title with escaped backslash \\" %}'
+ )
+
+for e in 0 1; do
+ lp=$(line_parser "${examples[$e]}")
+ echo -e "$lp"
+done
+
diff --git a/challenge-259/nelo-tovar/perl/ch-1.pl b/challenge-259/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..4c6221d76a
--- /dev/null
+++ b/challenge-259/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 259 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-259/
+#
+# Task 1 - Banking Day Offset
+#
+
+use strict;
+use warnings;
+use v5.28;
+use List::Util qw/any/;
+use DateTime;
+use Data::Dump qw(dump);
+
+my @examples = ( '2018-06-28', '2018-06-28' );
+my @examples_offset = ( 3, 3 );
+my @examples_bank_holidays = (
+ ['2018-07-03'],
+);
+
+sub banking_day_offset {
+ my ($start_date, $offset, $bank_holidays) = @_;
+ my $day_offset = DateTime->new(
+ year => substr($start_date, 0, 4),
+ month => substr($start_date, 5, 2),
+ day => substr($start_date, 8, 2)
+ );
+
+ for (my $i = 1; $i <= $offset; $i++) {
+ $day_offset->add(days => 1);
+
+ $day_offset->add(days => 1) while ($day_offset->day_of_week =~ /[67]/);
+
+ $day_offset->add(days => 1) if (any {$day_offset->ymd eq $_} @$bank_holidays);
+
+
+ }
+
+ return $day_offset->ymd;
+}
+
+for (my $i = 0; $i < scalar @examples; $i++) {
+ my $elements = $examples[$i];
+ my $offset = defined $examples_offset[$i] ? $examples_offset[$i] : 0;
+ my $bank_holidays = defined $examples_bank_holidays[$i] ? $examples_bank_holidays[$i] : undef;
+ my $bdo = banking_day_offset $elements, $offset, $bank_holidays;
+
+ print 'Input : $start_date = ', $elements, ', $offset = ', $offset;
+ print ', $bank_holidays = ', dump(@$bank_holidays) if (defined $bank_holidays);
+ print "\n";
+ say 'Output : ', $bdo;
+ say ' ';
+}
diff --git a/challenge-259/nelo-tovar/perl/ch-2.pl b/challenge-259/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..00172efcec
--- /dev/null
+++ b/challenge-259/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 259 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-259/
+#
+# Task 2 - Line Parser
+#
+
+use strict;
+use warnings;
+use feature qw/say/;
+use Data::Dump qw/dump/;
+
+my @examples = (
+ '{% youtube title="Title \"quoted\" done" %}',
+ '{% youtube title="Title with escaped backslash \\" %}',
+);
+
+sub line_parser {
+ my $original_line = shift;
+ my %line_parsed;
+
+ if ( $original_line =~ /^{%\s+(\w+)(\s+.+)\s*%}/) {
+ $line_parsed{name} = $1;
+
+ $original_line =~ s/\\"/\034/g;
+
+ while ($original_line =~ /(\w+)="?(\d+|[\w\034\s]+)"?/cg) {
+ my $field_name = $1;
+ my $value = $2;
+ $value =~ s/\034/"/g;
+ $line_parsed{fields}->{$field_name} = $value;
+ }
+ }
+
+ return \%line_parsed;
+}
+
+for my $elements (@examples) {
+ my $lp = line_parser $elements;
+
+ say dump($lp);
+ say ' ';
+}