aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-08-11 00:26:35 +0100
committerGitHub <noreply@github.com>2025-08-11 00:26:35 +0100
commit3cabb88986647ea383680c63e2191c67a19788f1 (patch)
treed16af47e934714eee0f8b418c1a0c3a96f8de3bf
parent2ea6d57ecd4b14cd71b4d1f6830e99afb98b1f31 (diff)
parent57fbcb2ab6b2daf24e8b2aec085f468c1259b04e (diff)
downloadperlweeklychallenge-club-3cabb88986647ea383680c63e2191c67a19788f1.tar.gz
perlweeklychallenge-club-3cabb88986647ea383680c63e2191c67a19788f1.tar.bz2
perlweeklychallenge-club-3cabb88986647ea383680c63e2191c67a19788f1.zip
Merge pull request #12497 from mattneleigh/pwc333
Pwc333
-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..b9c88615da
--- /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 calculating the area of
+# the triangle formed by the points and determining whether it is zero; due to
+# round-off error, it is possible that a set of points that are very nearly
+# (but not quite) colinear may be indicated as colinear.
+# 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]
+ );
+
+}
+
+
+