From 8b554f562d36c27e8c3968cf181c889dc6a09a6b Mon Sep 17 00:00:00 2001 From: e78783 Date: Tue, 17 May 2022 17:10:15 +0800 Subject: Week 165 --- challenge-165/cheok-yin-fung/perl/ch-1.pl | 42 ++++++++++++++++++++++ challenge-165/cheok-yin-fung/perl/ch-2.pl | 59 +++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 challenge-165/cheok-yin-fung/perl/ch-1.pl create mode 100644 challenge-165/cheok-yin-fung/perl/ch-2.pl diff --git a/challenge-165/cheok-yin-fung/perl/ch-1.pl b/challenge-165/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..ec333aae85 --- /dev/null +++ b/challenge-165/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +# The Weekly Challenge 165 +# Task 1 SVG +# Usage: $ ch-1.pl < DATA > IMG_FILE_NAME.svg + +use v5.24.0; +use warnings; +use SVG; + +my $img = SVG->new; + +while () { + chomp; + my @nums = split ",", $_; + if (scalar @nums == 2) { + new_point(@nums); + } + elsif (scalar @nums == 4) { + new_line(@nums); + } +} + + + +sub new_point { + my ($cx, $cy) = ($_[0], $_[1]); + $img->circle(cx => $cx, cy => $cy, r => 2, style => {fill => 'red'}); +} + + + +sub new_line { + my ($x1, $y1, $x2, $y2) = ($_[0], $_[1], $_[2], $_[3]); + $img->line( x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, + style => { + 'fill-opacity' => 0, + 'stroke' => 'green' + }); +} + + +print $img->xmlify(-namespace=>'svg'); diff --git a/challenge-165/cheok-yin-fung/perl/ch-2.pl b/challenge-165/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..4cc8f19c0a --- /dev/null +++ b/challenge-165/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl +# The Weekly Challenge 165 +# Task 2 Line of Best Fit +# Usage: $ ch-2.pl +use v5.24.0; +use warnings; +use List::Util qw/min max sum/; +use ARGV::OrDATA; + +my @points; + +while (<>) { + chomp; + my @new_points = /\S+/g; + @new_points = map { [split ",", $_]} @new_points; + push @points, @new_points; +} + +my $n = scalar @points; + +my $slope = + ( $n * sum(map {$_->[0]*$_->[1]} @points) + - sum(map{$_->[0]} @points) * sum(map{$_->[1]} @points) ) + / ( $n * sum(map{$_->[0]**2} @points) + - (sum( map { $_->[0]} @points ))**2) ; + + +my $intercept = + ( sum( map {$_->[1]} @points ) + - $slope * sum( map {$_->[0]} @points) ) + / $n ; + + +sub y_best_fit { + my $x = $_[0]; + return $slope*$x + $intercept; +} + +my $minx = min( map {$_->[0]} @points ); +my $maxx = max( map {$_->[0]} @points ); + +my $line_of_best_fit = + join ",", $minx,y_best_fit($minx),$maxx,y_best_fit($maxx); + +open FH, "> BEST_FIT_DATA" or die $!; +say FH $line_of_best_fit; +say FH $_->[0],",",$_->[1] for @points; + +system("perl ch-1.pl < BEST_FIT_DATA > new.svg"); # applicable on Linux + + + +__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 -- cgit From 1cf20f45072a719aa11a0f4a21f79f33f93d6124 Mon Sep 17 00:00:00 2001 From: e78783 Date: Tue, 17 May 2022 17:28:25 +0800 Subject: prettier Task 1 --- challenge-165/cheok-yin-fung/perl/ch-1.pl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/challenge-165/cheok-yin-fung/perl/ch-1.pl b/challenge-165/cheok-yin-fung/perl/ch-1.pl index ec333aae85..a6fb13ad2c 100644 --- a/challenge-165/cheok-yin-fung/perl/ch-1.pl +++ b/challenge-165/cheok-yin-fung/perl/ch-1.pl @@ -6,10 +6,11 @@ use v5.24.0; use warnings; use SVG; +use ARGV::OrDATA; my $img = SVG->new; -while () { +while (<>) { chomp; my @nums = split ",", $_; if (scalar @nums == 2) { @@ -40,3 +41,8 @@ sub new_line { print $img->xmlify(-namespace=>'svg'); + +__DATA__ +53,10 +53,10,23,30 +23,30 -- cgit From 1a824ba3aa0d2be05a6cb415c21ae82bd5fa853e Mon Sep 17 00:00:00 2001 From: e78783 Date: Tue, 17 May 2022 17:34:38 +0800 Subject: prettier Task 1 --- challenge-165/cheok-yin-fung/perl/ch-1.pl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/challenge-165/cheok-yin-fung/perl/ch-1.pl b/challenge-165/cheok-yin-fung/perl/ch-1.pl index a6fb13ad2c..1c14bdfc89 100644 --- a/challenge-165/cheok-yin-fung/perl/ch-1.pl +++ b/challenge-165/cheok-yin-fung/perl/ch-1.pl @@ -9,18 +9,21 @@ use SVG; use ARGV::OrDATA; my $img = SVG->new; +my @points; while (<>) { chomp; my @nums = split ",", $_; if (scalar @nums == 2) { - new_point(@nums); + push @points, [@nums]; } elsif (scalar @nums == 4) { new_line(@nums); } } +new_point($_->@*) foreach @points; #so that the lines won't be over points + sub new_point { -- cgit From 57810dc9b1ff8993050bc5e19474d915d47d0d6e Mon Sep 17 00:00:00 2001 From: e78783 Date: Tue, 17 May 2022 20:41:27 +0800 Subject: blogpost, julia soln, edit ch-2.pl --- challenge-165/cheok-yin-fung/blog.txt | 1 + challenge-165/cheok-yin-fung/julia/ch-2.jl | 81 ++++++++++++++++++++++++++++++ challenge-165/cheok-yin-fung/perl/ch-2.pl | 9 ++-- 3 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 challenge-165/cheok-yin-fung/blog.txt create mode 100644 challenge-165/cheok-yin-fung/julia/ch-2.jl diff --git a/challenge-165/cheok-yin-fung/blog.txt b/challenge-165/cheok-yin-fung/blog.txt new file mode 100644 index 0000000000..b5566b2f41 --- /dev/null +++ b/challenge-165/cheok-yin-fung/blog.txt @@ -0,0 +1 @@ +https://e7-87-83.github.io/coding/challenge_165.html diff --git a/challenge-165/cheok-yin-fung/julia/ch-2.jl b/challenge-165/cheok-yin-fung/julia/ch-2.jl new file mode 100644 index 0000000000..bd0113f50d --- /dev/null +++ b/challenge-165/cheok-yin-fung/julia/ch-2.jl @@ -0,0 +1,81 @@ +#!/usr/bin/perl +# The Weekly Challenge 165 +# Task 2 Line of Best Fit +# Julia Solution +# ref: +# https://jahoo.github.io/2021/01/11/simplest_linear_regression_example.html + + +using Plots + +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 , +] + +xs = [] +ys = [] + +for i = 1:2:length(data)-1 + push!(xs,data[i]) +end + +for i = 2:2:length(data) + push!(ys,data[i]) +end + +scatter(xs, ys) + +A = convert(Matrix{Float64}, hcat(xs, ones(length(xs)))) + +slope, intercept = inv(transpose(A)*A)*transpose(A) * ys + +plot!(x -> slope*x + intercept, legend = :none) + +savefig("new_julia.svg") diff --git a/challenge-165/cheok-yin-fung/perl/ch-2.pl b/challenge-165/cheok-yin-fung/perl/ch-2.pl index 4cc8f19c0a..7d0c7fb658 100644 --- a/challenge-165/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-165/cheok-yin-fung/perl/ch-2.pl @@ -19,18 +19,17 @@ while (<>) { my $n = scalar @points; my $slope = - ( $n * sum(map {$_->[0]*$_->[1]} @points) - - sum(map{$_->[0]} @points) * sum(map{$_->[1]} @points) ) - / ( $n * sum(map{$_->[0]**2} @points) + ( $n * sum(map {$_->[0]*$_->[1]} @points) # n sum(xy) - sum(x) sum(y) + - sum(map{$_->[0]} @points) * sum(map{$_->[1]} @points) ) + / ( $n * sum(map{$_->[0]**2} @points) # n sum(sq x) - sq(sum x) - (sum( map { $_->[0]} @points ))**2) ; my $intercept = - ( sum( map {$_->[1]} @points ) + ( sum( map {$_->[1]} @points ) # sum(y) - slope * sum(x) - $slope * sum( map {$_->[0]} @points) ) / $n ; - sub y_best_fit { my $x = $_[0]; return $slope*$x + $intercept; -- cgit From a473815e35238a5fad16bcb4882662a8f26cb880 Mon Sep 17 00:00:00 2001 From: e78783 Date: Tue, 17 May 2022 20:48:22 +0800 Subject: ratio --- challenge-165/cheok-yin-fung/julia/ch-2.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/challenge-165/cheok-yin-fung/julia/ch-2.jl b/challenge-165/cheok-yin-fung/julia/ch-2.jl index bd0113f50d..f29f55ae76 100644 --- a/challenge-165/cheok-yin-fung/julia/ch-2.jl +++ b/challenge-165/cheok-yin-fung/julia/ch-2.jl @@ -76,6 +76,10 @@ A = convert(Matrix{Float64}, hcat(xs, ones(length(xs)))) slope, intercept = inv(transpose(A)*A)*transpose(A) * ys -plot!(x -> slope*x + intercept, legend = :none) +plot!( + x -> slope*x + intercept, + legend = :none, +# ratio = 1, #optional +) savefig("new_julia.svg") -- cgit