From df4aee15abcc46f8b2844b7ba035b45afa0ff31e Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 15 Sep 2020 13:26:46 +0200 Subject: Perl solutions for Week 78 --- challenge-078/abigail/Part1/input1 | 1 + challenge-078/abigail/Part1/input2 | 1 + challenge-078/abigail/Part1/input3 | 2 ++ challenge-078/abigail/Part1/output1.exp | 1 + challenge-078/abigail/Part1/output2.exp | 1 + challenge-078/abigail/Part1/output3.exp | 1 + challenge-078/abigail/Part1/solution.pl | 43 +++++++++++++++++++++++++++++++++ challenge-078/abigail/Part2/input1 | 2 ++ challenge-078/abigail/Part2/input2 | 2 ++ challenge-078/abigail/Part2/output1.exp | 2 ++ challenge-078/abigail/Part2/output2.exp | 3 +++ challenge-078/abigail/Part2/solution.pl | 42 ++++++++++++++++++++++++++++++++ challenge-078/abigail/test.pl | 31 ++++++++++++++++++++++++ 13 files changed, 132 insertions(+) create mode 100644 challenge-078/abigail/Part1/input1 create mode 100644 challenge-078/abigail/Part1/input2 create mode 100644 challenge-078/abigail/Part1/input3 create mode 100644 challenge-078/abigail/Part1/output1.exp create mode 100644 challenge-078/abigail/Part1/output2.exp create mode 100644 challenge-078/abigail/Part1/output3.exp create mode 100755 challenge-078/abigail/Part1/solution.pl create mode 100644 challenge-078/abigail/Part2/input1 create mode 100644 challenge-078/abigail/Part2/input2 create mode 100644 challenge-078/abigail/Part2/output1.exp create mode 100644 challenge-078/abigail/Part2/output2.exp create mode 100755 challenge-078/abigail/Part2/solution.pl create mode 100755 challenge-078/abigail/test.pl diff --git a/challenge-078/abigail/Part1/input1 b/challenge-078/abigail/Part1/input1 new file mode 100644 index 0000000000..9a2d61d8c5 --- /dev/null +++ b/challenge-078/abigail/Part1/input1 @@ -0,0 +1 @@ +9 10 7 5 6 1 diff --git a/challenge-078/abigail/Part1/input2 b/challenge-078/abigail/Part1/input2 new file mode 100644 index 0000000000..02c7bd24aa --- /dev/null +++ b/challenge-078/abigail/Part1/input2 @@ -0,0 +1 @@ +3 4 5 diff --git a/challenge-078/abigail/Part1/input3 b/challenge-078/abigail/Part1/input3 new file mode 100644 index 0000000000..139597f9cb --- /dev/null +++ b/challenge-078/abigail/Part1/input3 @@ -0,0 +1,2 @@ + + diff --git a/challenge-078/abigail/Part1/output1.exp b/challenge-078/abigail/Part1/output1.exp new file mode 100644 index 0000000000..65f1fa0ab1 --- /dev/null +++ b/challenge-078/abigail/Part1/output1.exp @@ -0,0 +1 @@ +10 7 6 1 diff --git a/challenge-078/abigail/Part1/output2.exp b/challenge-078/abigail/Part1/output2.exp new file mode 100644 index 0000000000..7ed6ff82de --- /dev/null +++ b/challenge-078/abigail/Part1/output2.exp @@ -0,0 +1 @@ +5 diff --git a/challenge-078/abigail/Part1/output3.exp b/challenge-078/abigail/Part1/output3.exp new file mode 100644 index 0000000000..573541ac97 --- /dev/null +++ b/challenge-078/abigail/Part1/output3.exp @@ -0,0 +1 @@ +0 diff --git a/challenge-078/abigail/Part1/solution.pl b/challenge-078/abigail/Part1/solution.pl new file mode 100755 index 0000000000..e74cbbb0c4 --- /dev/null +++ b/challenge-078/abigail/Part1/solution.pl @@ -0,0 +1,43 @@ +#!/opt/perl/bin/perl + +# +# Exercise: +# You are given an array @A containing distinct integers. +# Write a script to find all leader elements in the array @A. +# Print (0) if none found. An element is leader if it is greater +# than all the elements to its right side. +# + +# +# Note: +# - The only way no leader element can be found is if the array is empty. +# - We will read the array from STDIN. +# + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; + +my $max; + +# +# Read the input, extract integers, and store them in @A. +# If the input is empty, print 0 and exit. +# +say (0), exit unless my @A = <> =~ /[0-9]+/g; + +local $, = " "; + +# +# Reverse the array, and extract each element which was larger +# than any seen before (keep state in $max), then reverse it +# again before printing. +# + +say reverse + grep {!defined $max || $_ > $max ? do {$max = $_; 1} : 0} + reverse @A; diff --git a/challenge-078/abigail/Part2/input1 b/challenge-078/abigail/Part2/input1 new file mode 100644 index 0000000000..6ed39f8f4d --- /dev/null +++ b/challenge-078/abigail/Part2/input1 @@ -0,0 +1,2 @@ +10 20 30 40 50 +3 4 diff --git a/challenge-078/abigail/Part2/input2 b/challenge-078/abigail/Part2/input2 new file mode 100644 index 0000000000..fda601bebf --- /dev/null +++ b/challenge-078/abigail/Part2/input2 @@ -0,0 +1,2 @@ +7 4 2 6 3 +1 3 4 diff --git a/challenge-078/abigail/Part2/output1.exp b/challenge-078/abigail/Part2/output1.exp new file mode 100644 index 0000000000..6569215fd4 --- /dev/null +++ b/challenge-078/abigail/Part2/output1.exp @@ -0,0 +1,2 @@ +40 50 10 20 30 +50 10 20 30 40 diff --git a/challenge-078/abigail/Part2/output2.exp b/challenge-078/abigail/Part2/output2.exp new file mode 100644 index 0000000000..819d56ba14 --- /dev/null +++ b/challenge-078/abigail/Part2/output2.exp @@ -0,0 +1,3 @@ +4 2 6 3 7 +6 3 7 4 2 +3 7 4 2 6 diff --git a/challenge-078/abigail/Part2/solution.pl b/challenge-078/abigail/Part2/solution.pl new file mode 100755 index 0000000000..80e8f4907d --- /dev/null +++ b/challenge-078/abigail/Part2/solution.pl @@ -0,0 +1,42 @@ +#!/opt/perl/bin/perl + +# +# Exercise: +# +# You are given array @A containing positive numbers and @B containing +# one or more indices from the array @A. +# Write a script to left rotate @A so that the number at the first index +# of @B becomes the first element in the array. Similary, left rotate @A +# again so that the number at the second index of @B becomes the first +# element in the array. +# + +# +# We will be reading the arrays from STDIN -- @A is one the first +# line, @B is on the second line. +# + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; + +# +# Read @A and @B +# +chomp (my @A = split / / => scalar <>); +chomp (my @B = split / / => scalar <>); + +# +# Print the rotations. +# +foreach my $index (@B) { + local $, = " "; + say @A [$index .. @A - 1, 0 .. $index - 1]; +} + + +__END__ diff --git a/challenge-078/abigail/test.pl b/challenge-078/abigail/test.pl new file mode 100755 index 0000000000..2a0406cf31 --- /dev/null +++ b/challenge-078/abigail/test.pl @@ -0,0 +1,31 @@ +#!/opt/perl/bin/perl + +# +# Test the solutions. Either call it with the directory name you +# want to test in, or call it as "../test.pl" from within the directory. +# + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +chdir shift if @ARGV; + +use experimental 'signatures'; + +use Test::More; + +my @inputs = ; +foreach my $input (@inputs) { + my $output_exp = ($input =~ s/input/output/r) . ".exp"; + my $exp = `cat $output_exp`; + my $got = `./solution.pl < $input`; + is $got, $exp, $input; +} + +done_testing; + + +__END__ -- cgit From ffbd8368ce571dab7b941dd8aa8e50f2f5678b5b Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 15 Sep 2020 14:30:29 +0200 Subject: Modify the test program so it tests Javascript solutions as well. --- challenge-078/abigail/test.pl | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/challenge-078/abigail/test.pl b/challenge-078/abigail/test.pl index 2a0406cf31..115e570c62 100755 --- a/challenge-078/abigail/test.pl +++ b/challenge-078/abigail/test.pl @@ -17,12 +17,25 @@ use experimental 'signatures'; use Test::More; +my %languages = ( + Perl => ["/opt/perl/bin/perl" => 'pl'], + JavaScript => ["/usr/local/bin/node" => 'js'], +); + + my @inputs = ; -foreach my $input (@inputs) { - my $output_exp = ($input =~ s/input/output/r) . ".exp"; - my $exp = `cat $output_exp`; - my $got = `./solution.pl < $input`; - is $got, $exp, $input; + +foreach my $language (sort keys %languages) { + my ($exe, $ext) = @{$languages {$language}}; + next unless -r "solution.$ext"; + subtest $language => sub { + foreach my $input (@inputs) { + my $output_exp = ($input =~ s/input/output/r) . ".exp"; + my $exp = `cat $output_exp`; + my $got = `$exe ./solution.$ext < $input`; + is $got, $exp, $input; + } + } } done_testing; -- cgit From 1801e66819957b886808c2e98508ca190d2b2432 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 15 Sep 2020 14:30:58 +0200 Subject: JavaScript solutions. --- challenge-078/abigail/Part1/solution.js | 52 +++++++++++++++++++++++++++++++++ challenge-078/abigail/Part2/solution.js | 33 +++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 challenge-078/abigail/Part1/solution.js create mode 100644 challenge-078/abigail/Part2/solution.js diff --git a/challenge-078/abigail/Part1/solution.js b/challenge-078/abigail/Part1/solution.js new file mode 100644 index 0000000000..eba11fd3b3 --- /dev/null +++ b/challenge-078/abigail/Part1/solution.js @@ -0,0 +1,52 @@ +// +// Exercise: +// You are given an array @A containing distinct integers. +// Write a script to find all leader elements in the array @A. +// Print (0) if none found. An element is leader if it is greater +// than all the elements to its right side. +// + +// +// Note: +// - The only way no leader element can be found is if the array is empty. +// - We will read the array from STDIN. +// + +// +// Read a line from STDIN, turn it into a string, strip off the +// trailing newline (and any leading or trailing whitespace), +// and then split in on spaces. Store the result into an array "arry". +// +let fs = require ("fs"); +let line = fs . readFileSync (0) . toString () . trim (); +let arry = line . split (" "); + +if (line . length == 0) { + // + // Special case, if the line is empty, output 0 + // + console . log (0); +} +else { + // + // Iterate backwards over the array. Keep track of the largest + // element so far in an array 'out'. If we find a new largest + // element, put this first in 'out' (so, the largest element + // seen so far is always in 'out [0]'. + // + let out = [arry [arry . length - 1]]; + for (let i = arry . length - 2; i >= 0; i --) { + // + // Note that we have strings in arry (and hence, out). + // An unary + casts them to numbers. + // + if (+arry [i] > +out [0]) { + out . unshift (arry [i]); + } + } + + // + // Print the result. + // + console . log (out . join (" ")); +} diff --git a/challenge-078/abigail/Part2/solution.js b/challenge-078/abigail/Part2/solution.js new file mode 100644 index 0000000000..6d76c9aa91 --- /dev/null +++ b/challenge-078/abigail/Part2/solution.js @@ -0,0 +1,33 @@ +// +// Exercise: +// +// You are given array @A containing positive numbers and @B containing +// one or more indices from the array @A. +// Write a script to left rotate @A so that the number at the first index +// of @B becomes the first element in the array. Similary, left rotate @A +// again so that the number at the second index of @B becomes the first +// element in the array. +// +// We will be reading the arrays from STDIN -- @A is one the first +// line, @B is on the second line. +// + +// +// Read two lines from STDIN, turn it into a string, strip off the +// trailing newline (and any leading or trailing whitespace), +// and then split in on spaces. Store the result into an arrays A and B. +// +let fs = require ("fs"); +let lines = fs . readFileSync (0) . toString () . split ("\n"); +let A = lines [0] . trim () . split (" "); +let B = lines [1] . trim () . split (" "); + + +// +// Iterate over the array B, and print the slices of A. +// +for (let i = 0; i < B . length; i ++) { + let index = +B [i]; + console . log (A . slice ( index) . join (" ") + " " + + A . slice (0, index) . join (" ")); +} -- cgit