From 490a0a66685204df8a6e64bd35cb4d91b415b33c Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 4 Aug 2025 12:27:32 -0400 Subject: Week 333 --- challenge-333/zapwai/perl/ch-1.pl | 41 +++++++++++++++++++++++++++++++++++++++ challenge-333/zapwai/perl/ch-2.pl | 32 ++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 challenge-333/zapwai/perl/ch-1.pl create mode 100644 challenge-333/zapwai/perl/ch-2.pl diff --git a/challenge-333/zapwai/perl/ch-1.pl b/challenge-333/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..79c696e3a2 --- /dev/null +++ b/challenge-333/zapwai/perl/ch-1.pl @@ -0,0 +1,41 @@ +use v5.38; +sub slope($x1, $y1, $x2, $y2) { + return ($y2 - $y1)/($x2-$x1); +} + +sub proc(@l) { + print "Input: "; + my @x; + my @y; + foreach (@l) { + print "[".join(",",@$_)."] "; + push @x, $$_[0]; + push @y, $$_[1]; + } + print "\n"; + + my $dx = $x[1] - $x[0]; + if ($dx == 0) { + if ($x[1] == $x[2]) { + say "Output: true"; + } else { + say "Output: false"; + } + } else { + my $s1 = slope($x[0],$y[0],$x[1],$y[1]); + my $s2 = slope($x[1],$y[1],$x[2],$y[2]); + ($s1 == $s2) ? say "Output: true" : say "Output: false"; + } +} + +my @list = ([2, 1], [2, 3], [2, 5]); +proc(@list); +@list = ([1, 4], [3, 4], [10, 4]); +proc(@list); +@list = ([0, 0], [1, 1], [2, 3]); +proc(@list); +@list = ([1, 1], [1, 1], [1, 1]); +proc(@list); +@list = ([1000000, 1000000], [2000000, 2000000], [3000000, 3000000]); +proc(@list); + diff --git a/challenge-333/zapwai/perl/ch-2.pl b/challenge-333/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..46594a4cd4 --- /dev/null +++ b/challenge-333/zapwai/perl/ch-2.pl @@ -0,0 +1,32 @@ +use v5.38; + +sub proc(@ints) { + print "Input : "; + say "(". join(", ", @ints).")"; + my @z; # zero locations + my @o; # output + my $cnt = 0; # length lost to zeros + for my $i (0 .. $#ints) { + my $integer = $ints[$i]; + if ($integer == 0) { + $cnt++; + push @o, (0,0); + } else { + push @o, $integer; + } + last if ($cnt + $i >= $#ints); + } + pop @o if (scalar(@o) > scalar(@ints)); + say "Output: (".join(", ", @o).")"; +} + +my @ints = (1, 0, 2, 3, 0, 4, 5, 0); +proc(@ints); +@ints = (1,2,3); +proc(@ints); +@ints = (1,2,3,0); +proc(@ints); +@ints = (0,0,1,2); +proc(@ints); +@ints = (1, 2, 0, 3, 4); +proc(@ints); -- cgit