aboutsummaryrefslogtreecommitdiff
path: root/challenge-027
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-09-29 09:35:00 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-09-29 09:35:00 +0100
commit63cb54e6fce5c7128ed5d05f94c76bc333ffaa60 (patch)
treee388dc004457f63c1618be984df5fb53d6b687f7 /challenge-027
parent6d1910afd271caaac2fd6c601cdf7850dc858451 (diff)
downloadperlweeklychallenge-club-63cb54e6fce5c7128ed5d05f94c76bc333ffaa60.tar.gz
perlweeklychallenge-club-63cb54e6fce5c7128ed5d05f94c76bc333ffaa60.tar.bz2
perlweeklychallenge-club-63cb54e6fce5c7128ed5d05f94c76bc333ffaa60.zip
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-027')
-rw-r--r--challenge-027/colin-crain/perl5/HistoryThing.pm49
-rw-r--r--challenge-027/colin-crain/perl5/ch-1.pl68
-rw-r--r--challenge-027/colin-crain/perl5/ch-2.pl56
3 files changed, 173 insertions, 0 deletions
diff --git a/challenge-027/colin-crain/perl5/HistoryThing.pm b/challenge-027/colin-crain/perl5/HistoryThing.pm
new file mode 100644
index 0000000000..163478de2f
--- /dev/null
+++ b/challenge-027/colin-crain/perl5/HistoryThing.pm
@@ -0,0 +1,49 @@
+# HistoryThing
+#
+# object
+#
+#
+# (c) 2019 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+package HistoryThing;
+
+use Moo;
+use feature ":5.26";
+our $VERSION = "0.01";
+
+
+## attributes
+has 'x' => (
+ is => 'rw',
+ trigger => sub {
+ my ($self, $new) = @_;
+ push $self->_history->{'x'}->@*, $new;
+ }
+);
+
+has 'y' => (
+ is => 'rw',
+ trigger => sub {
+ my ($self, $new) = @_;
+ push $self->_history->{'y'}->@*, $new;
+ }
+);
+
+## pubic methods
+sub get_hist {
+ my ($self, $var) = @_;
+ return (join ', ', $self->_history->{$var}->@*);
+
+};
+
+## private attribute (hash)
+has _history => (
+ is => 'rw',
+ default => sub {
+ return { 'x' => [], 'y' => [] };
+ },
+);
+
+
+1;
diff --git a/challenge-027/colin-crain/perl5/ch-1.pl b/challenge-027/colin-crain/perl5/ch-1.pl
new file mode 100644
index 0000000000..fb1df3b6f0
--- /dev/null
+++ b/challenge-027/colin-crain/perl5/ch-1.pl
@@ -0,0 +1,68 @@
+#! /opt/local/bin/perl
+#
+# intersection.pl
+#
+# task: Write a script to find the intersection of two straight
+# lines. The co-ordinates of the two lines should be provided
+# as command line parameter. For example:
+# The two ends of Line 1 are represented as co-ordinates (a,b) and (c,d).
+# The two ends of Line 2 are represented as co-ordinates (p,q) and (r,s).
+# The script should print the co-ordinates of point of intersection of the above two lines.
+#
+# usage: ./intersection.pl '(1,1)' '(6,6)' '(0,6)' '(6,0)'
+#
+# V3
+#
+# 2019 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+use Getopt::Long;
+
+use warnings;
+use strict;
+use feature ":5.26";
+
+## ## ## ## ## MAIN
+
+
+my $line1;
+my $line2;
+
+GetOptions( 'line1=s' => \$line1,
+ 'line2=s' => \$line2
+) or die "error with GetOptions: $!";
+
+my (@x, @y);
+if ( $line1 =~ / ([ - \d ]+) # some digits, the x coordinate
+ [ \s , ]+ # a combination of comma and spaces
+ ([ - \d ]+) # some digits, the y coordinate
+ [^ - \d ]+ # parens, spaces, commas or whatnot between pairs
+ ([ - \d ]+) # some digits, the x coordinate
+ [ \s , ]+ # a combination of comma and spaces
+ ([ - \d ]+) # some digits, the y coordinate
+ /xx) {
+ @x = ($1, $3);
+ @y = ($2, $4);
+}
+if ( $line2 =~ /([-\d]+) [\s,]+ ([-\d]+) [^-\d]+ ([-\d]+) [\s,]+ ([-\d]+)/x ) { ## same regex as above
+ push @x, ($1, $3);
+ push @y, ($2, $4);
+}
+
+## if we didn't load 4 points bail out
+for (0..3) {
+ if (! defined $x[$_] or not defined $y[$_]) {
+ die "improper input\nusage: ./intersection3.pl --line1 '(a,b) (c,d)' --line2 '(q,r) (s,t)'\n"
+ }
+}
+
+## extract the intersection using linear algebra determinate
+my $det_x_num = (($x[0] * $y[1] - $y[0] * $x[1]) * ($x[2] - $x[3])) - (($x[0] - $x[1]) * ($x[2] * $y[3] - $y[2] * $x[3]));
+my $det_y_num = (($x[0] * $y[1] - $y[0] * $x[1]) * ($x[2] - $x[3])) - (($y[0] - $y[1]) * ($x[2] * $y[3] - $y[2] * $x[3]));
+my $det_div = (($x[0] - $x[1]) * ($y[2] - $y[3])) - (($y[0] - $y[1]) * ($x[2] - $x[3]));
+
+my ($px, $py) = ($det_x_num/$det_div, $det_y_num/$det_div);
+
+say "\($px, $py\)";
+
+
diff --git a/challenge-027/colin-crain/perl5/ch-2.pl b/challenge-027/colin-crain/perl5/ch-2.pl
new file mode 100644
index 0000000000..c3484a0303
--- /dev/null
+++ b/challenge-027/colin-crain/perl5/ch-2.pl
@@ -0,0 +1,56 @@
+#! /opt/local/bin/perl
+# HistoryThing.pl
+#
+# task: Write a script that allows you to capture/display
+# historical data. It could be an object or a scalar.
+# For example
+#
+# my $x = 10; $x = 20; $x -= 5;
+#
+# After the above operations, it should list $x historical value in order.
+#
+# method: in the script below, with its related package, h is an object that holds
+# two values, x and y. The name and number of values is easily extendable.
+# Altering the values for either variable results in the new value recorded
+# to a historical list of values that is maintained and can be viewed by
+# calling the 'history' method with the name of the variable in question.
+#
+# uses a moo object. I like moo.
+#
+# (c) 2019 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+use lib '.'; ## park HistoryThing.pm alongside
+
+use HistoryThing;
+
+use warnings;
+use strict;
+use feature ":5.26";
+
+## ## ## ## ## MAIN
+
+my $h = new HistoryThing;
+
+## set some values for x
+$h->x(1);
+say "x at start: ", $h->x;
+
+$h->x(2);
+$h->x(-1);
+$h->x('foo');
+say "x is currently: ", $h->x;
+
+## set some values for y
+$h->y(3);
+say "y at start: ", $h->y;
+
+$h->y(4);
+$h->y(-5);
+$h->y('bar');
+say "y is currently: ", $h->y;
+
+say "history of x: ", $h->get_hist('x');
+say "history of y: ", $h->get_hist('y');
+
+