aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2025-08-05 11:47:14 +0200
committerE. Choroba <choroba@matfyz.cz>2025-08-05 11:47:14 +0200
commite901c38723bd52d40d5e8b5e5f54b1063eb43c3f (patch)
tree0bb208bebfe2a53e3befba94de7af3232d2b7449
parent085fd2e66487b2e1532ab0877919279f3f4c94e7 (diff)
downloadperlweeklychallenge-club-e901c38723bd52d40d5e8b5e5f54b1063eb43c3f.tar.gz
perlweeklychallenge-club-e901c38723bd52d40d5e8b5e5f54b1063eb43c3f.tar.bz2
perlweeklychallenge-club-e901c38723bd52d40d5e8b5e5f54b1063eb43c3f.zip
Add solutions to 333: Straight Line & Duplicate Zeros by E. Choroba
-rwxr-xr-xchallenge-333/e-choroba/perl/ch-1.pl48
-rwxr-xr-xchallenge-333/e-choroba/perl/ch-2.pl18
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-333/e-choroba/perl/ch-1.pl b/challenge-333/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..97d4dd5010
--- /dev/null
+++ b/challenge-333/e-choroba/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub straight_line(@list) {
+ my ($x, $y) = @{ $list[0] };
+ my ($vx, $vy);
+ for my $point (@list[1 .. $#list]) {
+ next if $point->[0] == $x && $point->[1] == $y;
+
+ my $nvx = $x - $point->[0];
+ my $nvy = $y - $point->[1];
+ if (defined $vx) {
+ return if ($vx != 0 && $nvx != 0 && $vy / $vx != $nvy / $nvx)
+ || ($vx == 0 xor $nvx == 0);
+ } else {
+ ($vx, $vy) = ($nvx, $nvy);
+ }
+ }
+ return 1
+}
+
+use Test2::V0;
+use constant { true => 1, false => 0 };
+plan(5 + 13);
+
+is straight_line([2, 1], [2, 3], [2, 5]), bool(true), 'Example 1';
+is straight_line([1, 4], [3, 4], [10, 4]), bool(true), 'Example 2';
+is straight_line([0, 0], [1, 1], [2, 3]), bool(false), 'Example 3';
+is straight_line([1, 1], [1, 1], [1, 1]), bool(true), 'Example 4';
+is straight_line([1000000, 1000000], [2000000, 2000000], [3000000, 3000000]),
+ bool(true), 'Example 5';
+
+is straight_line([0, 0]), bool(true), 'single point';
+is straight_line([0, 1], [1, 0]), bool(true), 'two points';
+is straight_line([1, 4], [2, 6], [3, 8], [4, 10]), bool(true), 'Four points';
+is straight_line([0, 1], [2, 3], [3, 4], [-5, -4]), bool(true),
+ 'Negative values';
+is straight_line([1, 2], [3, 7], [3, 7]), bool(true), 'B=C';
+is straight_line([1, 2], [3, 7], [1, 2]), bool(true), 'A=C';
+is straight_line([1, 2], [1, 2], [3, 7]), bool(true), 'A=B';
+is straight_line([2, 1], [2, 3], [3, 4]), bool(false), 'Not y';
+is straight_line([2, 1], [3, 3], [2, 4]), bool(false), 'Not y';
+is straight_line([3, 1], [2, 3], [2, 4]), bool(false), 'Not y';
+is straight_line([3, 2], [1, 3], [1, 4]), bool(false), 'Not x';
+is straight_line([1, 2], [3, 3], [1, 4]), bool(false), 'Not x';
+is straight_line([1, 2], [1, 3], [3, 4]), bool(false), 'Not x';
diff --git a/challenge-333/e-choroba/perl/ch-2.pl b/challenge-333/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..97e71d27e4
--- /dev/null
+++ b/challenge-333/e-choroba/perl/ch-2.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub duplicate_zeros(@ints) {
+ (map $_ || (0, 0), @ints)[0 .. $#ints]
+}
+
+use Test2::V0;
+plan(5);
+
+is [duplicate_zeros(1, 0, 2, 3, 0, 4, 5, 0)], [1, 0, 0, 2, 3, 0, 0, 4],
+ 'Example 1';
+is [duplicate_zeros(1, 2, 3)], [1, 2, 3], 'Example 2';
+is [duplicate_zeros(1, 2, 3, 0)], [1, 2, 3, 0], 'Example 3';
+is [duplicate_zeros(0, 0, 1, 2)], [0, 0, 0, 0], 'Example 4';
+is [duplicate_zeros(1, 2, 0, 3, 4)], [1, 2, 0, 0, 3], 'Example 5';