diff options
Diffstat (limited to 'challenge-078/abigail/Part2')
| -rw-r--r-- | challenge-078/abigail/Part2/input1 | 2 | ||||
| -rw-r--r-- | challenge-078/abigail/Part2/input2 | 2 | ||||
| -rw-r--r-- | challenge-078/abigail/Part2/output1.exp | 2 | ||||
| -rw-r--r-- | challenge-078/abigail/Part2/output2.exp | 3 | ||||
| -rw-r--r-- | challenge-078/abigail/Part2/solution.js | 33 | ||||
| -rwxr-xr-x | challenge-078/abigail/Part2/solution.pl | 42 |
6 files changed, 84 insertions, 0 deletions
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.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 (" ")); +} 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__ |
