aboutsummaryrefslogtreecommitdiff
path: root/challenge-027
diff options
context:
space:
mode:
authorJaldhar H. Vyas <jaldhar@braincells.com>2019-10-13 07:33:59 -0400
committerJaldhar H. Vyas <jaldhar@braincells.com>2019-10-13 07:33:59 -0400
commitba84fdda5ba0efe58cc58ac2b776109172d854db (patch)
tree2dd1d09cb4b7cbeb54d1f5118e0ed5db024f18be /challenge-027
parent9a7a969ebcb0058362faadd2a4df13f8289dc41c (diff)
downloadperlweeklychallenge-club-ba84fdda5ba0efe58cc58ac2b776109172d854db.tar.gz
perlweeklychallenge-club-ba84fdda5ba0efe58cc58ac2b776109172d854db.tar.bz2
perlweeklychallenge-club-ba84fdda5ba0efe58cc58ac2b776109172d854db.zip
challenge 27 by Jaldhar H. Vyas
Diffstat (limited to 'challenge-027')
-rw-r--r--challenge-027/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-027/jaldhar-h-vyas/perl5/ch-1.pl31
-rwxr-xr-xchallenge-027/jaldhar-h-vyas/perl5/ch-2.pl32
-rwxr-xr-xchallenge-027/jaldhar-h-vyas/perl6/ch-1.p621
-rwxr-xr-xchallenge-027/jaldhar-h-vyas/perl6/ch-2.p625
5 files changed, 110 insertions, 0 deletions
diff --git a/challenge-027/jaldhar-h-vyas/blog.txt b/challenge-027/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..5f330590d5
--- /dev/null
+++ b/challenge-027/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2019/10/perl_weekly_challenge_weeks_27-28.html
diff --git a/challenge-027/jaldhar-h-vyas/perl5/ch-1.pl b/challenge-027/jaldhar-h-vyas/perl5/ch-1.pl
new file mode 100755
index 0000000000..ed15d5c387
--- /dev/null
+++ b/challenge-027/jaldhar-h-vyas/perl5/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+
+sub intersection {
+ my ($a, $b, $c, $d, $p, $q, $r, $s) = @_;
+
+ my $denominator = ((($c - $a) * ($s - $q)) - (($r - $p) * ($d - $b)));
+
+ if ($denominator == 0) {
+ say 'Lines do not intersect or intersect at multiple points.';
+ return;
+ }
+
+ my $x = ((($c * $b) - ($a * $d)) * ($r - $p)) -
+ ((($r * $q) - ($p * $s)) * ($c - $a)) /
+ $denominator;
+
+ my $y = ((($c * $b) - ($a * $d)) * ($s - $q)) -
+ ((($r * $q) - ($p * $s)) * ($d - $b)) /
+ $denominator;
+
+ say "($x,$y)";
+}
+
+if (scalar @ARGV != 8) {
+ say 'specify four points as integers: x1 y1 x2 y2 x3 y3 x4 y4';
+} else {
+ intersection(@ARGV);
+} \ No newline at end of file
diff --git a/challenge-027/jaldhar-h-vyas/perl5/ch-2.pl b/challenge-027/jaldhar-h-vyas/perl5/ch-2.pl
new file mode 100755
index 0000000000..0a0e53124a
--- /dev/null
+++ b/challenge-027/jaldhar-h-vyas/perl5/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+
+package Historical {
+ use Tie::Scalar;
+ use parent -norequire => 'Tie::StdScalar';
+
+ our @history;
+
+ sub TIESCALAR {
+ my ($class, $value) = @_;
+ push @history, "Storing <$value> (was [])";
+ return bless \$value, $class;
+ }
+
+ sub STORE {
+ my ($self, $value) = @_;
+ push @history, "Storing <$value> (was [$$self])";
+ $$self = $value;
+ }
+}
+
+package main {
+
+ tie my $x, 'Historical', 10;
+ $x = 20;
+ $x -= 5;
+
+ say join "\n", @Historical::history;
+} \ No newline at end of file
diff --git a/challenge-027/jaldhar-h-vyas/perl6/ch-1.p6 b/challenge-027/jaldhar-h-vyas/perl6/ch-1.p6
new file mode 100755
index 0000000000..466df6ee09
--- /dev/null
+++ b/challenge-027/jaldhar-h-vyas/perl6/ch-1.p6
@@ -0,0 +1,21 @@
+#!/usr/bin/perl6
+
+sub MAIN($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) {
+
+ my $denominator = ((($x2 - $x1) * ($y4 - $y3)) - (($x4 - $x3) * ($y2 - $y1)));
+
+ if ($denominator == 0) {
+ say 'Lines do not intersect or intersect at multiple points.';
+ return;
+ }
+
+ my $x = ((($x2 * $y1) - ($x1 * $y2)) * ($x4 - $x3)) -
+ ((($x4 * $y3) - ($x3 * $y4)) * ($x2 - $x1)) /
+ $denominator;
+
+ my $y = ((($x2 * $y1) - ($x1 * $y2)) * ($y4 - $y3)) -
+ ((($x4 * $y3) - ($x3 * $y4)) * ($y2 - $y1)) /
+ $denominator;
+
+ say "($x,$y)";
+}
diff --git a/challenge-027/jaldhar-h-vyas/perl6/ch-2.p6 b/challenge-027/jaldhar-h-vyas/perl6/ch-2.p6
new file mode 100755
index 0000000000..0820c1ad47
--- /dev/null
+++ b/challenge-027/jaldhar-h-vyas/perl6/ch-2.p6
@@ -0,0 +1,25 @@
+#!/usr/bin/perl6
+
+my @history;
+
+sub historical($value) is rw {
+ my $storage = $value;
+ @history.push("Storing <$value> (was [])");
+
+ Proxy.new(
+ FETCH => method () {
+ return $storage;
+ },
+
+ STORE => method ($new) {
+ @history.push("Storing <$new> (was [$storage])");
+ $storage = $new;
+ },
+ )
+}
+
+my $x := historical(10);
+$x = 20;
+$x -= 5;
+
+ @history.join("\n").say; \ No newline at end of file