diff options
| -rw-r--r-- | challenge-027/lubos-kolouch/perl5/ch-1.pl | 64 | ||||
| -rw-r--r-- | challenge-027/lubos-kolouch/perl5/ch-2.pl | 41 | ||||
| -rw-r--r-- | challenge-027/lubos-kolouch/python/ch27_1.py | 33 |
3 files changed, 138 insertions, 0 deletions
diff --git a/challenge-027/lubos-kolouch/perl5/ch-1.pl b/challenge-027/lubos-kolouch/perl5/ch-1.pl new file mode 100644 index 0000000000..2e6c0130f9 --- /dev/null +++ b/challenge-027/lubos-kolouch/perl5/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: Perl Weekly Challenge - 027 +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-027/ +# +# +# 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). +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: Lubos Kolouch +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 09/28/2019 01:50:52 PM +# REVISION: --- +#=============================================================================== + +use strict; +use warnings; +use feature qw/say/; + +die 'Usage: ch-1.pl a b c d p q r s' unless @ARGV == 8; + +sub get_intersection { + + my ( $a, $b, $c, $d, $p, $q, $r, $s ) = @_; + # x1 y1 x2 y2 x3 y3 x4 y4 + + my $det = ( $a - $c ) * ( $q - $s ) - ( $b - $d ) * ( $p - $r ); + return 0 if $det == 0; + + # Let's assume the lines are infinitely long + my $px = ( $a * $d - $b * $c ) * ( $p - $r ) - ( $a - $c ) * ( $p * $s - $q * $r ) / $det; + + my $py = ( $a * $d - $b * $c ) * ( $q - $s ) - ( $b - $d ) * ( $p * $s - $q * $r ) / $det; + + return ( [ $px, $py ] ); +} + +my ($result) = &get_intersection(@ARGV); +say "Intersection : $result->[0] , $result->[1]"; + +# TESTS +use Test::More; + +is_deeply( \&get_intersection( 0, 0, 2, 0, -4, -5, 8, 1 ), \[ 6, 0 ] ); +is_deeply( \&get_intersection( 0, 0, 2, 0, -4, -5, 8, 0 ), \[ 8, 0 ] ); +is_deeply( \&get_intersection( 0, 0, 2, 1, -4, -5, 2, 1 ), \[ 2, 1 ] ); + +is( &get_intersection( 1, 2, 3, 4, 5, 6, 7, 8 ), 0 ); + +done_testing; + diff --git a/challenge-027/lubos-kolouch/perl5/ch-2.pl b/challenge-027/lubos-kolouch/perl5/ch-2.pl new file mode 100644 index 0000000000..d22227cda0 --- /dev/null +++ b/challenge-027/lubos-kolouch/perl5/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-2.pl +# +# USAGE: ./ch-2.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-027/ +# +# +# +# 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; +# +# +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: Lubos Kolouch +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 09/28/2019 02:48:06 PM +# REVISION: --- +#=============================================================================== + +use strict; +use warnings; +use Data::Dumper; + +my $what = 'my $x = 10; $x = 20; $x -= 5'; +my @history; + +my $x; +for (split /;/msx, $what) { + push @history, eval || die 'error in expression'; +} + +warn Dumper \@history; diff --git a/challenge-027/lubos-kolouch/python/ch27_1.py b/challenge-027/lubos-kolouch/python/ch27_1.py new file mode 100644 index 0000000000..c1bf6589e4 --- /dev/null +++ b/challenge-027/lubos-kolouch/python/ch27_1.py @@ -0,0 +1,33 @@ +#! python3 + +import sys + +def get_intersection(points): + a = int(points[1]) + b = int(points[2]) + c = int(points[3]) + d = int(points[4]) + + p = int(points[5]) + q = int(points[6]) + r = int(points[7]) + s = int(points[8]) + + det = ( a - c ) * ( q - s ) - ( b - d ) * ( p - r ) + + if det == 0: + return 0 + + # Let's assume the lines are infinitely long + px = ( a * d - b * c ) * ( p - r ) - ( a - c ) * ( p * s - q * r ) / det; + py = ( a * d - b * c ) * ( q - s ) - ( b - d ) * ( p * s - q * r ) / det; + + return ( [ px, py ] ); + +if len(sys.argv) != 9: + print('Usage: ch-1.pl a b c d p q r s'); + sys.exit() + +result = get_intersection(sys.argv) + +print(result) |
