diff options
| author | Andreas Mahnke <andreas.mahnke@leuphana.de> | 2025-08-10 16:06:21 +0200 |
|---|---|---|
| committer | Andreas Mahnke <andreas.mahnke@leuphana.de> | 2025-08-10 16:06:21 +0200 |
| commit | af44daaeb7d257a1077fd2095fdf9d69b0649d0b (patch) | |
| tree | 6ab23b0295e409da5f11b6473e0c9aa061c0ec4b | |
| parent | 20df564117e99e12ad1c952aa9c7dd9feb23680c (diff) | |
| download | perlweeklychallenge-club-af44daaeb7d257a1077fd2095fdf9d69b0649d0b.tar.gz perlweeklychallenge-club-af44daaeb7d257a1077fd2095fdf9d69b0649d0b.tar.bz2 perlweeklychallenge-club-af44daaeb7d257a1077fd2095fdf9d69b0649d0b.zip | |
Challenge 333
| -rw-r--r-- | challenge-333/mahnkong/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-333/mahnkong/perl/ch-2.pl | 22 |
2 files changed, 49 insertions, 0 deletions
diff --git a/challenge-333/mahnkong/perl/ch-1.pl b/challenge-333/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..72c77eff46 --- /dev/null +++ b/challenge-333/mahnkong/perl/ch-1.pl @@ -0,0 +1,27 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@coordinates) { + my $diff_x; + my $diff_y; + + return 1 if scalar(@coordinates) < 2; + + my ($x0, $y0) = @{$coordinates[0]}; + my ($x1, $y1) = @{$coordinates[1]}; + + for (my $i = 2; $i < scalar(@coordinates); $i++) { + my ($x, $y) = @{$coordinates[$i]}; + + return 0 if ($y1-$y0)*($x-$x0) != ($y-$y0)*($x1-$x0); + } + return 1; +} + +is(run([2, 1], [2, 3], [2, 5]), 1, "Example 1"); +is(run([1, 4], [3, 4], [10, 4]), 1, "Example 2"); +is(run([0, 0], [1, 1], [2, 3]), 0, "Example 3"); +is(run([1, 1], [1, 1], [1, 1]), 1, "Example 4"); +is(run([1000000, 1000000], [2000000, 2000000], [3000000, 3000000]), 1, "Example 5"); diff --git a/challenge-333/mahnkong/perl/ch-2.pl b/challenge-333/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..cd30b46d4a --- /dev/null +++ b/challenge-333/mahnkong/perl/ch-2.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@ints) { + for (my $i = 0; $i <= $#ints; $i++) { + if ($ints[$i] == 0 && exists($ints[$i + 1])) { + for (my $j = $#ints; $j > $i; $j--) { + $ints[$j] = $ints[$j-1]; + } + $i+=1; + } + } + return \@ints; +} + +is_deeply(run(1, 0, 2, 3, 0, 4, 5, 0), [1, 0, 0, 2, 3, 0, 0, 4], "Example 1"); +is_deeply(run(1, 2, 3), [1, 2, 3], "Example 2"); +is_deeply(run(1, 2, 3, 0), [1, 2, 3, 0], "Example 3"); +is_deeply(run(0, 0, 1, 2), [0, 0, 0, 0], "Example 4"); +is_deeply(run(1, 2, 0, 3, 4), [1, 2, 0, 0, 3], "Example 5"); |
