diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-05 11:51:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-05 11:51:35 +0100 |
| commit | 9c5a35578bd6de14a41ab1c7abe21086f6ef4625 (patch) | |
| tree | b5b25bbb60dbd9777745d9d0091516194452d792 | |
| parent | 085fd2e66487b2e1532ab0877919279f3f4c94e7 (diff) | |
| parent | 035a8148a755b8610dccecfdb629fb62ecca72ad (diff) | |
| download | perlweeklychallenge-club-9c5a35578bd6de14a41ab1c7abe21086f6ef4625.tar.gz perlweeklychallenge-club-9c5a35578bd6de14a41ab1c7abe21086f6ef4625.tar.bz2 perlweeklychallenge-club-9c5a35578bd6de14a41ab1c7abe21086f6ef4625.zip | |
Merge pull request #12471 from mauke/challenge-333
add perl solutions for 333
| -rw-r--r-- | challenge-333/mauke/perl/ch-1.pl | 48 | ||||
| -rw-r--r-- | challenge-333/mauke/perl/ch-2.pl | 32 |
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-333/mauke/perl/ch-1.pl b/challenge-333/mauke/perl/ch-1.pl new file mode 100644 index 0000000000..033213aecb --- /dev/null +++ b/challenge-333/mauke/perl/ch-1.pl @@ -0,0 +1,48 @@ +use v5.40; + +sub gcd($x, $y) { + while () { + ($x, $y) = ($y, $x) + if $x > $y; + return $y + if $x == 0; + my $d = $y - $x; + $y = $x; + $x = $d; + } +} + +sub shifted(@points) { + map { + my $vx = $_->[0] - $points[0][0]; + my $vy = $_->[1] - $points[0][1]; + $vx && $vy ? [$vx, $vy] : () + } @points +} + +sub reduce($f) { + my $t = gcd $f->[0], $f->[1]; + [ $f->[0] / $t, $f->[1] / $t ] +} + +sub forms_straight_line(@points) { + my @shifted = shifted @points + or return true; + my $g = reduce shift @shifted; + for my $point (@shifted) { + my $f = reduce $point; + $f->[0] == $g->[0] && $f->[1] == $g->[1] + or return false; + } + true +} + +for my $input ( + [[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]], +) { + say forms_straight_line(@$input) ? "true" : "false"; +} diff --git a/challenge-333/mauke/perl/ch-2.pl b/challenge-333/mauke/perl/ch-2.pl new file mode 100644 index 0000000000..4733a4147b --- /dev/null +++ b/challenge-333/mauke/perl/ch-2.pl @@ -0,0 +1,32 @@ +use v5.36; + +sub dupz(@v) { + my @r = map $_ || (0, 0), @v; + splice @r, @v; + @r +} + +sub dupz2(@v) { + my @r; + for my $v (@v) { + for my $x ($v || (0, 0)) { + push @r, $x; + return @r if @r >= @v; + } + } +} + +for my $input ( + [1, 0, 2, 3, 0, 4, 5, 0], + [1, 2, 3], + [1, 2, 3, 0], + [0, 0, 1, 2], + [1, 2, 0, 3, 4], +) { + my @result = dupz @$input; + my @result2 = dupz2 @$input; + say "(@$input)"; + say "a: (@result)"; + say "b: (@result2)"; + say ""; +} |
