From 11f68eaadb5c0c9d4ea8f89b57b133abdb1622f1 Mon Sep 17 00:00:00 2001 From: Saif Ahmed <34284663+saiftynet@users.noreply.github.com> Date: Sun, 22 May 2022 22:50:01 +0100 Subject: Saiftynet single file response to two challenges --- challenge-165/saiftynet/ch-1or2.pl | 102 +++++++++++++++++++++++++++++++++++++ challenge-165/saiftynet/in.dat | 6 +++ 2 files changed, 108 insertions(+) create mode 100644 challenge-165/saiftynet/ch-1or2.pl create mode 100644 challenge-165/saiftynet/in.dat diff --git a/challenge-165/saiftynet/ch-1or2.pl b/challenge-165/saiftynet/ch-1or2.pl new file mode 100644 index 0000000000..7ed2680673 --- /dev/null +++ b/challenge-165/saiftynet/ch-1or2.pl @@ -0,0 +1,102 @@ +#!/usr/bin/env perl +# The is a solution to Challenge 165, that actually does both challenges + +# Challenge one reads data either points (two numbers separated by commas) +# or lines 4 numbers separated by commas, one item per line to generate +# an SVG file. The data input is from a file specified by using an option -i +# or from the __DATA__ +# line of best fit is disabled by -l 0 +# e.g. ch-1or2.pl -i in.dat -l 0 + +# Challenge two does the same but also draws the best fit straight line +# using least squares method. this is the default operation and uses data +# provided + +# Usage: ch-1or2.pl Default...just does challenge 2 using data in __DATA__ +# ch-1or2.pl <-i inputFile> <-o outputfile> specify input and output files +# ch-1or2.pl -l 0 Disables line of best fit + +use strict; +use warnings; + +my %params=@ARGV; # read command line params as hash +# input data +my $infilename= ((exists $params{"-i"}) && ( -e $params{"-i"} ))?$params{"-i"}:""; + +# experimental "title" +my $title= $params{"-t"}?$params{"-t"}:($infilename?$infilename:"__DATA__"); + +#initialise +my @data; +my ($maxX,$minX,$maxY,$minY,$sumX,$sumY, $sumX2, $sumXY, $number)=(-1000,1000,-1000,1000,0,0,0,0,0); + +#read data +if ($infilename){ + open (my $infile,"<",$infilename) or die $!; + @data=<$infile>; + close $infile; + } +else{ + @data=; +} + +my $svg=""; # the svg file content +foreach (@data) { + # find and genrate lines + $svg.= " \n" + while (/(^|\s)([\-\=]?[\d]+\.?[\d]*),([\-\=]?[\d]+\.?[\d]*),([\-\=]?[\d]+\.?[\d]*),([\-\=]?[\d]+\.?[\d]*)($|\s)/g); + # find and generate points adding to analysis if needed + while (/(^|\s)([\-\=]?[\d]+\.?[\d]*),([\-\=]?[\d]+\.?[\d]*)($|\s)/g){ + addPoint($2,$3); + $svg.= " \n" + }; + +} + $svg.= bestFitLine(); +# top and tailthe svg contents, and flip the data so that the SVG coordinate system becomes cartesian +$svg="\n". + "\n". # flips the points for a cartesian chart + " \n". + $svg. + "\n\n". + "$title\n". + ""; + +# save the svg file +open (my $file,">",$params{"-o"}//"out.svg") or die $!; +print $file $svg; +close $file; + +# each point found is added to the stats +sub addPoint{ + my ($x,$y)=@_; + $maxX= $x if ($x>$maxX); + $maxY= $y if ($y>$maxY); + $minX= $x if ($x<$minX); + $minY= $y if ($y<$minY); + $sumX+=$x; + $sumY+=$y; + $sumX2+=$x*$x; + $sumXY+=$x*$y; + $number++; + return 1; +} + +## genrating best fit line using least squares method +sub bestFitLine{ + return "" if ((exists $params{"-l"}) && ($params{"-l"}=~/0|off/i)); # dont bother if option -l is 0 + my $gradient= ($number*$sumXY-$sumX*$sumY)/($number*$sumX2-$sumX*$sumX); + my $intercept=($sumY-$gradient*$sumX)/$number; + my $x1=$minX; my $y1=$gradient*$minX+$intercept;#$maxY; + my $x2=$maxX; my $y2=$gradient*$maxX+$intercept;#$minY; + return " \n" +} + +# notice data can be spearted by spaces or new lines +__DATA__ +333,129 39,189 140,156 292,134 393,52 160,166 362,122 13,193 +341,104 320,113 109,177 203,152 343,100 225,110 23,186 282,102 +284,98 205,133 297,114 292,126 339,112 327,79 253,136 61,169 +128,176 346,72 316,103 124,162 65,181 159,137 212,116 337,86 +215,136 153,137 390,104 100,180 76,188 77,181 69,195 92,186 +275,96 250,147 34,174 213,134 186,129 189,154 361,82 363,89 diff --git a/challenge-165/saiftynet/in.dat b/challenge-165/saiftynet/in.dat new file mode 100644 index 0000000000..a4098fc56d --- /dev/null +++ b/challenge-165/saiftynet/in.dat @@ -0,0 +1,6 @@ +333,129 39,189 140,156 292,134 393,52 160,166 362,122 13,193 +341,104 320,113 109,177 203,152 343,100 225,110 23,186 282,102 +284,98 205,133 297,114 292,126 339,112 327,79 253,136 61,169 +128,176 346,72 316,103 124,162 65,181 159,137 212,116 337,86 +215,136 153,137 390,104 100,180 76,188 77,181 69,195 92,186 +275,96 250,147 34,174 213,134 186,129 189,154 361,82 363,89 -- cgit -- cgit From c971e42eedc84048088f4ba19a3f7b29e57dcecf Mon Sep 17 00:00:00 2001 From: Saif Ahmed <34284663+saiftynet@users.noreply.github.com> Date: Sun, 22 May 2022 23:03:06 +0100 Subject: Single solution to two challenges Usage: perl ch-1or2.pl.....solves challenge 1 perl ch-1or2.pl -i -l <0|off> ...uses data from input file, and does not draw line of best fit --- challenge-165/saiftynet/ch-1or2.pl | 4 +++- challenge-165/saiftynet/in.dat | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/challenge-165/saiftynet/ch-1or2.pl b/challenge-165/saiftynet/ch-1or2.pl index 7ed2680673..eb08094600 100644 --- a/challenge-165/saiftynet/ch-1or2.pl +++ b/challenge-165/saiftynet/ch-1or2.pl @@ -10,7 +10,8 @@ # Challenge two does the same but also draws the best fit straight line # using least squares method. this is the default operation and uses data -# provided +# provided. Note if lines are included, they are drawn but not added to the data +# for line of best fit # Usage: ch-1or2.pl Default...just does challenge 2 using data in __DATA__ # ch-1or2.pl <-i inputFile> <-o outputfile> specify input and output files @@ -93,6 +94,7 @@ sub bestFitLine{ } # notice data can be spearted by spaces or new lines +# lines are ignored for data for line of best fit __DATA__ 333,129 39,189 140,156 292,134 393,52 160,166 362,122 13,193 341,104 320,113 109,177 203,152 343,100 225,110 23,186 282,102 diff --git a/challenge-165/saiftynet/in.dat b/challenge-165/saiftynet/in.dat index a4098fc56d..b95799fa50 100644 --- a/challenge-165/saiftynet/in.dat +++ b/challenge-165/saiftynet/in.dat @@ -1,6 +1,8 @@ +data for inputing...this contains some lines as well. +text like this are ignored 333,129 39,189 140,156 292,134 393,52 160,166 362,122 13,193 -341,104 320,113 109,177 203,152 343,100 225,110 23,186 282,102 -284,98 205,133 297,114 292,126 339,112 327,79 253,136 61,169 +341,104 320,113 109,177 203,152 343,100,225,110 23,186 282,102 +284,98,205,133 297,114 292,126 339,112 327,79 253,136 61,169 128,176 346,72 316,103 124,162 65,181 159,137 212,116 337,86 -215,136 153,137 390,104 100,180 76,188 77,181 69,195 92,186 +215,136 153,137,390,104 100,180 76,188 77,181 69,195 92,186 275,96 250,147 34,174 213,134 186,129 189,154 361,82 363,89 -- cgit