diff options
| author | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2025-08-04 18:34:01 +0100 |
|---|---|---|
| committer | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2025-08-04 18:34:01 +0100 |
| commit | 94a8bc9f74d84ccf18ae49c8d02443d7a3062036 (patch) | |
| tree | d9c16ca759a0f725b3e933f35e9f676a8a3006af | |
| parent | ce2f933a023e15e5dac73508e56a9aec0e87fae6 (diff) | |
| download | perlweeklychallenge-club-94a8bc9f74d84ccf18ae49c8d02443d7a3062036.tar.gz perlweeklychallenge-club-94a8bc9f74d84ccf18ae49c8d02443d7a3062036.tar.bz2 perlweeklychallenge-club-94a8bc9f74d84ccf18ae49c8d02443d7a3062036.zip | |
Week 333 - Straight zeroes
| -rw-r--r-- | challenge-333/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-333/peter-campbell-smith/perl/ch-1.pl | 69 | ||||
| -rwxr-xr-x | challenge-333/peter-campbell-smith/perl/ch-2.pl | 25 |
3 files changed, 95 insertions, 0 deletions
diff --git a/challenge-333/peter-campbell-smith/blog.txt b/challenge-333/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..fa967134e4 --- /dev/null +++ b/challenge-333/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/333 diff --git a/challenge-333/peter-campbell-smith/perl/ch-1.pl b/challenge-333/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..f3fbcd4bcc --- /dev/null +++ b/challenge-333/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-08-04 +use utf8; # Week 333 - task 1 - Straight line +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +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]); +straight_line([0, 0], [1, 1], [99999999, 100000000]); +straight_line([8, -5], [3, -2], [-2, 1]); +straight_line([8, -5], [3, -2], [-2, 1], [-7, 4], [-12, 7]); +straight_line([0, 0], [1, 0], [2, 1], [3, 2]); + +sub straight_line { + + my (@p, $i, $j, @x, @y, $c, $d, $m, $yy, $same, $vertical, $input, $output); + + # initialise + @p = @_; + $i = 0; + $vertical = $same = 1; + + # loop over points + for $i (0 .. $#p) { + ($x[$i], $y[$i]) = @{$p[$i]}; + $input .= '[' . sprintf('%d', $x[$i]) . ', ' . sprintf('%d', $y[$i]) . '], '; + + # check for points being identical or all the same x + if ($i > 0) { + if ($x[$i] != $x[0] or $y[$i] != $y[0]) { + $same = 0; + $d = $i; # a point with diffferent x from x[0] + } + $vertical = 0 if $x[$i] != $x[0]; + } + } + $output = qq[true: any straight line through ($x[0], $y[0])] if $same; + $output = qq[true: x = $x[0]] if ($vertical and not $same); + + # otherwise calculate gradient and offset (using points 0 and d) + unless ($output) { + $m = 0; + $m = ($y[$d] - $y[0]) / ($x[$d] - $x[0]); + $c = $y[0] - $m * $x[0]; + + # check that all points fall on y = mx + c + for $i (0 .. $#p) { + $yy = $m * $x[$i] + $c; + if (abs($yy - $y[$i]) > 1e-15) { + $output = qq[false: ($x[$i], $y[$i]) is not collinear with points 0 and $d]; + last; + } + } + + # yes they do! + $output = qq[true: y = ] . ($m != 0 ? ($m == -1 ? '-x' : ($m != 1 ? "${m}x " : 'x ')) : '') . + ($m != 0 ? ($c == 0 ? '' : ($c > 0 ? "+ $c" : '- ' . -$c)) : $c) unless $output; + } + + say qq{\nInput: \$list = (} . substr($input, 0, -2) . ')'; + say qq[Output: $output]; +} diff --git a/challenge-333/peter-campbell-smith/perl/ch-2.pl b/challenge-333/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..60e8c0d02a --- /dev/null +++ b/challenge-333/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-08-04 +use utf8; # Week 333 - task 2 - Duplicate zeros +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +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 { + + # map each 0 to 0, 0 + my @output = map($_ == 0 ? (0, 0) : $_, @_); + + # output as many elements in @output as in the input array + say qq[\nInput: (] . join(', ', @_) . ')'; + say qq[Output: (] . join(', ', @output[0 .. $#_]) . ')'; +} |
