aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-333/mattneleigh/perl/ch-1.pl94
-rwxr-xr-xchallenge-333/mattneleigh/perl/ch-2.pl62
2 files changed, 156 insertions, 0 deletions
diff --git a/challenge-333/mattneleigh/perl/ch-1.pl b/challenge-333/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..722c3ff99a
--- /dev/null
+++ b/challenge-333/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,94 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @coordinate_sets = (
+ [
+ [ 2, 1 ], [ 2, 3 ], [ 2, 5 ]
+ ],
+ [
+ [ 1, 4 ], [ 3, 4 ], [ 10, 4 ]
+ ],
+ [
+ [ 0, 0 ], [ 1, 1 ], [ 2, 3 ]
+ ],
+ [
+ [ 1, 1 ], [ 1, 1 ], [ 1, 1 ]
+ ],
+ [
+ [ 1000000, 1000000 ], [ 2000000, 2000000 ], [ 3000000, 3000000 ]
+ ]
+);
+
+print("\n");
+foreach my $coordinate_set (@coordinate_sets){
+ printf(
+ "Input: \@list = (%s)\nOutput: %s\n\n",
+ join(
+ ", ",
+ map(
+ "[" . join(", ", @{$_}) . "]",
+ @{$coordinate_set}
+ )
+ ),
+ are_colinear(@{$coordinate_set}) ?
+ "true"
+ :
+ "false"
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a set of coordinates that define three points, determine whether these
+# points make a straight line. This is accomplished by etermining whether the
+# triangle formed by the points has an area of zero; due to round-off error, it
+# is possible that a set of points that are very nearly (but not quite)
+# colinear may be determined to be so.
+# Takes one argument:
+# * A list of coordinates to examine (e.g. ([ 1, 4 ], [ 3, 4 ], [ 10, 4 ]) )
+# Returns:
+# * 0 if the points defined by the supplied coordinates do NOT appear to be
+# colinear
+# * 1 if the points defined by the supplied coordinates appear to be colinear
+# (but see the note above regarding round-off error)
+################################################################################
+sub are_colinear{
+
+ # Determine whether the points form a triangle with
+ # an area of zero (or very close to it, considering
+ # round-off error...) in which case they should be
+ # colinear
+ return(
+ (
+ 0.5
+ *
+ abs(
+ $ARG[0][0] * ($ARG[1][1] - $ARG[2][1])
+ +
+ $ARG[1][0] * ($ARG[2][1] - $ARG[0][1])
+ +
+ $ARG[2][0] * ($ARG[0][1] - $ARG[1][1])
+ )
+ ) < 0.000000001 ?
+ 1
+ :
+ 0
+ );
+
+}
+
+
+
diff --git a/challenge-333/mattneleigh/perl/ch-2.pl b/challenge-333/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..faaf3dbe9e
--- /dev/null
+++ b/challenge-333/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 1, 0, 2, 3, 0, 4, 5, 0 ],
+ [ 1, 2, 3 ],
+ [ 1, 2, 3, 0 ],
+ [ 0, 0, 1, 2 ],
+ [ 1, 2, 0, 3, 4 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@ints = (%s)\nOutput: (%s)\n\n",
+ join(", ", @{$integer_list}),
+ join(", ", zero_shift(@{$integer_list}))
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given an array of integers, duplicate each occurrence of zero (0), shifting
+# the remaining elements to the right; the original length of the array is
+# preserved, thus any elements shifted beyond said length are discarded.
+# Takes one argument:
+# * The array to examine (e.g. (1, 0, 2, 3, 0, 4, 5, 0) )
+# Returns:
+# * A copy of the array with elements manipulated as described above (e.g.
+# (1, 0, 0, 2, 3, 0, 0, 4) )
+################################################################################
+sub zero_shift{
+
+ return(
+ # 2) Use an array slice to extract just the elements
+ # that lie within the confines of the original array
+ (
+ # 1) Make a copy of the array with zeros expanded
+ map(
+ $_ ? $_ : (0, 0),
+ @ARG
+ )
+ )[0 .. $#ARG]
+ );
+
+}
+
+
+