diff options
| author | 冯昶 <seaker@qq.com> | 2020-09-21 14:20:42 +0800 |
|---|---|---|
| committer | 冯昶 <seaker@qq.com> | 2020-09-21 14:20:42 +0800 |
| commit | bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb (patch) | |
| tree | 877181cfde26b706346d3468269e4674d75da772 /challenge-078/abigail/Part1 | |
| parent | ec09b571a6f2186fec8870a071a8d5d38596c850 (diff) | |
| parent | 5ac16ac7e9826137e0da5597e954f4992c66205d (diff) | |
| download | perlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.tar.gz perlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.tar.bz2 perlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-078/abigail/Part1')
| -rw-r--r-- | challenge-078/abigail/Part1/input1 | 1 | ||||
| -rw-r--r-- | challenge-078/abigail/Part1/input2 | 1 | ||||
| -rw-r--r-- | challenge-078/abigail/Part1/input3 | 2 | ||||
| -rw-r--r-- | challenge-078/abigail/Part1/output1.exp | 1 | ||||
| -rw-r--r-- | challenge-078/abigail/Part1/output2.exp | 1 | ||||
| -rw-r--r-- | challenge-078/abigail/Part1/output3.exp | 1 | ||||
| -rw-r--r-- | challenge-078/abigail/Part1/solution.js | 52 | ||||
| -rwxr-xr-x | challenge-078/abigail/Part1/solution.pl | 43 |
8 files changed, 102 insertions, 0 deletions
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.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/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; |
