aboutsummaryrefslogtreecommitdiff
path: root/challenge-165
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-05-17 22:37:21 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-05-17 22:37:21 +0100
commit0aff17755ac6bd66d16362d411dba1dc8cd6b92d (patch)
tree0871a0f56cd3da4e3dd7d2e2a8fe2b9ceb387f77 /challenge-165
parentb4b5157490c15311c02ceae025c53f13084beebf (diff)
downloadperlweeklychallenge-club-0aff17755ac6bd66d16362d411dba1dc8cd6b92d.tar.gz
perlweeklychallenge-club-0aff17755ac6bd66d16362d411dba1dc8cd6b92d.tar.bz2
perlweeklychallenge-club-0aff17755ac6bd66d16362d411dba1dc8cd6b92d.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-165')
-rw-r--r--challenge-165/ulrich-rieke/perl/ch-1.pl44
-rw-r--r--challenge-165/ulrich-rieke/perl/ch-2.pl62
2 files changed, 106 insertions, 0 deletions
diff --git a/challenge-165/ulrich-rieke/perl/ch-1.pl b/challenge-165/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..6e7cf3a017
--- /dev/null
+++ b/challenge-165/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+print "Enter a number of points, separated by , one per line, or lines," ;
+say " made up of two points, separated by a ,!" ;
+say "Enter 0,0 to end!" ;
+my @lines ;
+my $line = <STDIN> ;
+chomp $line ;
+while ( $line ne "0,0" ) {
+ while ( $line !~ /\A\d+\,\d+\z/ && $line !~ /\A\d+\,\d+\,\d+\,\d+\z/ ) {
+ say "Wrong format! Enter either 2 or 4 numbers separated by commas!" ;
+ $line = <STDIN> ;
+ chomp $line ;
+ }
+ push @lines , $line ;
+ $line = <STDIN> ;
+ chomp $line ;
+}
+my @lineInputs = grep { $_ =~ /\A\d+\,\d+\,\d+\,\d+\z/ } @lines ;
+my @pointInputs = grep { $_ =~ /\A\d+\,\d+\z/ } @lines ;
+my $outfile = "myTestfile.svg" ;
+open ( my $fh , ">> $outfile" ) or die "Can't append at $outfile: $!\n" ;
+say $fh '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' ;
+print $fh '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"' ;
+say $fh ' "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">' ;
+print $fh '<svg height="600" width="800" xmlns="http://www.w3.org/2000/svg"' ;
+say $fh ' xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' ;
+say $fh '<g id="lines" stroke="#369" stroke-width="4">' ;
+for my $inputLine ( @lineInputs ) {
+ my ( $x1 , $y1 , $x2 , $y2 ) = split( /\,/ , $inputLine ) ;
+ say $fh " <line x1=\"$x1\" x2=\"$x2\" y1=\"$y1\" y2=\"$y2\" />" ;
+}
+say $fh "</g>" ;
+say $fh '<g fill="#f73" id="points">' ;
+for my $point ( @pointInputs ) {
+ my ( $cx , $cy ) = split( /\,/ , $point ) ;
+ say $fh " <circle cx=\"$cx\" cy=\"$cy\" r=\"3\" />" ;
+}
+say $fh " </g>" ;
+say $fh "</svg>" ;
+close $fh ;
diff --git a/challenge-165/ulrich-rieke/perl/ch-2.pl b/challenge-165/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..e216f55b0b
--- /dev/null
+++ b/challenge-165/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( sum ) ;
+
+sub parseLine {
+ my $line = shift ;
+ my @points ;
+ my @pairs = split (/\s+/ , $line ) ;
+ for my $p ( @pairs ) {
+ my @point = split( /\,/ , $p ) ;
+ push @points , \@point ;
+ }
+ return @points ;
+}
+
+sub findLine {
+ my $passedPoints = shift ;
+ my $sumOfX = sum( map { $_->[0] } @{$passedPoints}) ;
+ my $sumOfY = sum( map { $_->[1] } @{$passedPoints}) ;
+ my $sumOfXY = sum( map { $_->[0] * $_->[1] } @{$passedPoints}) ;
+ my $sumOfXsquare = sum( map { $_->[0] * $_->[0] } @{$passedPoints}) ;
+ my $len = scalar( @{$passedPoints} ) ;
+ my $m = ($len * $sumOfXY - $sumOfX * $sumOfY ) / ( $len *
+ $sumOfXsquare - $sumOfX ** 2 ) ;
+ my $b = ( $sumOfY - $m * $sumOfX ) / $len ;
+ return ( $m , $b ) ;
+}
+
+my @points ;
+say "Please enter a number of points!( 0 to end)" ;
+my $line = <STDIN> ;
+chomp $line ;
+while ( $line ne "0" ) {
+ my @newPoints = parseLine( $line ) ;
+ push @points , @newPoints ;
+ $line = <STDIN> ;
+ chomp $line ;
+}
+my ( $m , $b ) = findLine( \@points ) ;
+say "The points entered are best fitted by a line with the equation y = $m x + $b!" ;
+open ( my $fh , ">> testfile.svg" ) or die "Can't append to testfile.svg! $!\n" ;
+say $fh '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' ;
+print $fh '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"' ;
+say $fh ' "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">' ;
+print $fh '<svg height="300" width="400" xmlns="http://www.w3.org/2000/svg"' ;
+say $fh ' xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' ;
+say $fh '<g id="lines" stroke="#369" stroke-width="4">' ;
+my $startx = 0 ;
+my $endx = 400 ;
+my $starty = $m * $startx + $b ;
+my $endy = $m * $endx + $b ;
+say $fh " <line x1=\"$startx\" x2=\"$endx\" y1=\"$starty\" y2=\"$endy\" />" ;
+say $fh "</g>" ;
+say $fh "<g fill=\"#f73\" id=\"points\">" ;
+for my $p ( @points ) {
+ say $fh " <circle cx=\"$p->[0]\" cy=\"$p->[1]\" r=\"3\" />" ;
+}
+say $fh " </g>" ;
+say $fh "</svg>" ;
+close $fh ;