From 126470a354832f75436397a2b0dd960a518ad84b Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 28 Sep 2019 15:00:04 +0200 Subject: Solutions #27 LK --- challenge-027/lubos-kolouch/ch-1.pl | 64 +++++++++++++++++++++++++++++++++++++ challenge-027/lubos-kolouch/ch-2.pl | 41 ++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 challenge-027/lubos-kolouch/ch-1.pl create mode 100644 challenge-027/lubos-kolouch/ch-2.pl diff --git a/challenge-027/lubos-kolouch/ch-1.pl b/challenge-027/lubos-kolouch/ch-1.pl new file mode 100644 index 0000000000..2e6c0130f9 --- /dev/null +++ b/challenge-027/lubos-kolouch/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/ch-2.pl b/challenge-027/lubos-kolouch/ch-2.pl new file mode 100644 index 0000000000..d22227cda0 --- /dev/null +++ b/challenge-027/lubos-kolouch/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; -- cgit From 62a4ee40095accc6c18ed726eb633cfce7d5ca7e Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 28 Sep 2019 15:12:09 +0200 Subject: Moving files to right perl5 folder --- challenge-027/lubos-kolouch/ch-1.pl | 64 ---------------------------- challenge-027/lubos-kolouch/ch-2.pl | 41 ------------------ challenge-027/lubos-kolouch/perl5/ch-1.pl | 64 ++++++++++++++++++++++++++++ challenge-027/lubos-kolouch/perl5/ch-2.pl | 41 ++++++++++++++++++ challenge-027/lubos-kolouch/python/ch27_1.py | 33 ++++++++++++++ 5 files changed, 138 insertions(+), 105 deletions(-) delete mode 100644 challenge-027/lubos-kolouch/ch-1.pl delete mode 100644 challenge-027/lubos-kolouch/ch-2.pl create mode 100644 challenge-027/lubos-kolouch/perl5/ch-1.pl create mode 100644 challenge-027/lubos-kolouch/perl5/ch-2.pl create mode 100644 challenge-027/lubos-kolouch/python/ch27_1.py diff --git a/challenge-027/lubos-kolouch/ch-1.pl b/challenge-027/lubos-kolouch/ch-1.pl deleted file mode 100644 index 2e6c0130f9..0000000000 --- a/challenge-027/lubos-kolouch/ch-1.pl +++ /dev/null @@ -1,64 +0,0 @@ -#!/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/ch-2.pl b/challenge-027/lubos-kolouch/ch-2.pl deleted file mode 100644 index d22227cda0..0000000000 --- a/challenge-027/lubos-kolouch/ch-2.pl +++ /dev/null @@ -1,41 +0,0 @@ -#!/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/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) -- cgit