diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-06 12:55:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-06 12:55:40 +0100 |
| commit | 5687713d337d65d407daf7ad551a2eeecc867d3f (patch) | |
| tree | 740fc430b6919a2390335eff5e565b2e3b7872d2 | |
| parent | 589ec718142059343a9b82712528d260d1028a21 (diff) | |
| parent | 3ee9918b9af3418593c4b8046b8877a15669972a (diff) | |
| download | perlweeklychallenge-club-5687713d337d65d407daf7ad551a2eeecc867d3f.tar.gz perlweeklychallenge-club-5687713d337d65d407daf7ad551a2eeecc867d3f.tar.bz2 perlweeklychallenge-club-5687713d337d65d407daf7ad551a2eeecc867d3f.zip | |
Merge pull request #12476 from jeanluc2020/jeanluc2020-333
Add solution 333.
| -rw-r--r-- | challenge-333/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-333/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-333/jeanluc2020/perl/ch-1.pl | 103 | ||||
| -rwxr-xr-x | challenge-333/jeanluc2020/perl/ch-2.pl | 81 |
4 files changed, 186 insertions, 0 deletions
diff --git a/challenge-333/jeanluc2020/blog-1.txt b/challenge-333/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..f199d8b0de --- /dev/null +++ b/challenge-333/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-333-1.html diff --git a/challenge-333/jeanluc2020/blog-2.txt b/challenge-333/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..774cae42fb --- /dev/null +++ b/challenge-333/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-333-2.html diff --git a/challenge-333/jeanluc2020/perl/ch-1.pl b/challenge-333/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..62b9e25b6b --- /dev/null +++ b/challenge-333/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,103 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-333/#TASK1 +# +# Task 1: Straight Line +# ===================== +# +# You are given a list of co-ordinates. +# +# Write a script to find out if the given points make a straight line. +# +## Example 1 +## +## Input: @list = ([2, 1], [2, 3], [2, 5]) +## Output: true +# +# +## Example 2 +## +## Input: @list = ([1, 4], [3, 4], [10, 4]) +## Output: true +# +# +## Example 3 +## +## Input: @list = ([0, 0], [1, 1], [2, 3]) +## Output: false +# +# +## Example 4 +## +## Input: @list = ([1, 1], [1, 1], [1, 1]) +## Output: true +# +# +## Example 5 +## +## Input: @list = ([1000000, 1000000], [2000000, 2000000], [3000000, 3000000]) +## Output: true +# +############################################################ +## +## discussion +## +############################################################ +# +# Basically we need to calculate the slope of the potential straight line +# from the first two points in the list. Then we need to calculate the slope +# from the first point to each other point in the list and compare that to +# the first slope. If it's always the same all points are on a straight line. +# If it isn't then we found a point that isn't on the same straight line as +# the first two points so we can return "false". +# The rest is special case handling: If a point is the same as the first point +# in the list it's on every straight line that crosses this point, so we are +# fine. And if the x values are the same but the y values aren't then we are +# on a vertical line so we can't calculate the slope due to division by zero. +# While at it, we also do special handling for the case of a horizontal line +# even though that wouldn't be necessary as the slope would just be 0. + +use v5.36; + +straight_line([2, 1], [2, 3], [2, 5]); +straight_line([1, 4], [3, 4], [10, 4]); +straight_line([0, 0], [1, 1], [2, 3]); +straight_line([1, 1], [1, 1], [1, 1]); +straight_line([1000000, 1000000], [2000000, 2000000], [3000000, 3000000]); + +sub straight_line(@list) { + say "Input: (" . join(", ", map { "[$_->[0], $_->[1]]" } @list) . ")"; + my $direction = ""; + my $start = shift @list; + my $slope; + foreach my $point (@list) { + if($point->[0] == $start->[0] and $point->[1] == $start->[1]) { + # the same point again - it's always on the same line + next; + } + if(defined($slope)) { + if($slope eq "v" and $point->[0] == $start->[0]) { + # x value is the same for a vertical slope + next; + } + if($slope eq "h" and $point->[1] == $start->[1]) { + # y value is the same for a horizontal slope + next; + } + if($slope == ($point->[1] - $start->[1])/($point->[0] - $start->[0]) ) { + # slope to this point is the same as before, so we're on the same straight line + next; + } + return say "Output: false"; + } else { + if($point->[0] == $start->[0]) { + $slope = "v"; + } elsif($point->[1] == $start->[1]) { + $slope = "h"; + } else { + $slope = ($point->[1] - $start->[1])/($point->[0] - $start->[0]); + } + } + } + say "Output: true"; +} + diff --git a/challenge-333/jeanluc2020/perl/ch-2.pl b/challenge-333/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..c30f1ba284 --- /dev/null +++ b/challenge-333/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,81 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-333/#TASK2 +# +# Task 2: Duplicate Zeros +# ======================= +# +# You are given an array of integers. +# +# Write a script to duplicate each occurrence of zero, shifting the remaining +# elements to the right. The elements beyond the length of the original array +# are not written. +# +## Example 1 +## +## Input: @ints = (1, 0, 2, 3, 0, 4, 5, 0) +## Output: (1, 0, 0, 2, 3, 0, 0, 4) +## +## Each zero is duplicated. +## Elements beyond the original length (like 5 and last 0) are discarded. +# +# +## Example 2 +## +## Input: @ints = (1, 2, 3) +## Output: (1, 2, 3) +## +## No zeros exist, so the array remains unchanged. +# +# +## Example 3 +## +## Input: @ints = (1, 2, 3, 0) +## Output: (1, 2, 3, 0) +# +# +## Example 4 +## +## Input: @ints = (0, 0, 1, 2) +## Output: (0, 0, 0, 0) +# +# +## Example 5 +## +## Input: @ints = (1, 2, 0, 3, 4) +## Output: (1, 2, 0, 0, 3) +# +############################################################ +## +## discussion +## +############################################################ +# +# We just walk from the left of the array to the right. Whenever +# we see a "0", we replace the rest of the array by a zero followed +# by the rest minus the last element. Of course then we need to skip +# the check for "0" in the next iteration so we keep state in a +# variable $skip. + +use v5.36; + +duplicate_zeros(1, 0, 2, 3, 0, 4, 5, 0); +duplicate_zeros(1, 2, 3); +duplicate_zeros(1, 2, 3, 0); +duplicate_zeros(0, 0, 1, 2); +duplicate_zeros(1, 2, 0, 3, 4); + +sub duplicate_zeros(@ints) { + say "Input: (" . join(", ", @ints) . ")"; + my $skip = 0; + foreach my $i (0..$#ints-1) { + if($skip) { + $skip = 0; + next; + } + if($ints[$i] == 0) { + @ints[$i+1..$#ints] = (0, @ints[$i+1..$#ints-1]); + $skip = 1; + } + } + say "Output: (" . join(", ", @ints) . ")"; +} |
