aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-165/cheok-yin-fung/blog.txt1
-rw-r--r--challenge-165/cheok-yin-fung/julia/ch-2.jl85
-rw-r--r--challenge-165/cheok-yin-fung/perl/ch-1.pl51
-rw-r--r--challenge-165/cheok-yin-fung/perl/ch-2.pl58
4 files changed, 195 insertions, 0 deletions
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..f29f55ae76
--- /dev/null
+++ b/challenge-165/cheok-yin-fung/julia/ch-2.jl
@@ -0,0 +1,85 @@
+#!/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,
+# ratio = 1, #optional
+)
+
+savefig("new_julia.svg")
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..1c14bdfc89
--- /dev/null
+++ b/challenge-165/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,51 @@
+#!/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;
+use ARGV::OrDATA;
+
+my $img = SVG->new;
+my @points;
+
+while (<>) {
+ chomp;
+ my @nums = split ",", $_;
+ if (scalar @nums == 2) {
+ 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 {
+ 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');
+
+__DATA__
+53,10
+53,10,23,30
+23,30
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..7d0c7fb658
--- /dev/null
+++ b/challenge-165/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,58 @@
+#!/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) # 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(y) - slope * sum(x)
+ - $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