From fa16306034b25e220fdbd125f24dda30994bf9dc Mon Sep 17 00:00:00 2001 From: andrezgz Date: Tue, 24 Sep 2019 17:21:00 -0300 Subject: challenge-027 andrezgz solution --- challenge-027/andrezgz/perl5/ch-1.pl | 46 ++++++++++++++++++++++++++++++++++++ challenge-027/andrezgz/perl5/ch-2.pl | 18 ++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 challenge-027/andrezgz/perl5/ch-1.pl create mode 100644 challenge-027/andrezgz/perl5/ch-2.pl diff --git a/challenge-027/andrezgz/perl5/ch-1.pl b/challenge-027/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..154fb49efc --- /dev/null +++ b/challenge-027/andrezgz/perl5/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-027/ +# Task #1 +# 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. + +use strict; +use warnings; + +# From https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection + +my $usage_messsage = <1 || $t<0) ; + +my $u = -1 * ( ($x1-$x2) * ($y1-$y3) - ($y1-$y2) * ($x1-$x3) ) / $d; +die "Intersection point out of second segment" if ($u>1 || $u<0) ; + +my $px = $x1 + $t * ($x2-$x1); +my $py = $y1 + $t * ($y2-$y1); + +printf "Intersection point (%.2f,%.2f)",$px,$py; diff --git a/challenge-027/andrezgz/perl5/ch-2.pl b/challenge-027/andrezgz/perl5/ch-2.pl new file mode 100644 index 0000000000..9e1ed73652 --- /dev/null +++ b/challenge-027/andrezgz/perl5/ch-2.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-027/ +# Task #2 +# 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. + +use strict; +use warnings; + +my $x; +my $x_ops= 'my $x = 10; $x = 20; $x -= 5;'; + +my @log; +push @log, eval $_ for (split /;/, $x_ops); +print join "\n",@log; -- cgit