From b0d5d893606d0309a92f521b103eca08c3eb6386 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 30 Sep 2024 16:04:31 +0100 Subject: Add Python solution to challenge 165 --- challenge-165/paulo-custodio/ch-2.svg | 2 +- challenge-165/paulo-custodio/perl/ch-1.pl | 10 ++-- challenge-165/paulo-custodio/perl/ch-2.pl | 4 +- challenge-165/paulo-custodio/python/ch-1.py | 71 +++++++++++++++++++++++++++ challenge-165/paulo-custodio/python/ch-2.py | 75 +++++++++++++++++++++++++++++ 5 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 challenge-165/paulo-custodio/python/ch-1.py create mode 100644 challenge-165/paulo-custodio/python/ch-2.py diff --git a/challenge-165/paulo-custodio/ch-2.svg b/challenge-165/paulo-custodio/ch-2.svg index 32091d0113..4e115403f6 100644 --- a/challenge-165/paulo-custodio/ch-2.svg +++ b/challenge-165/paulo-custodio/ch-2.svg @@ -49,5 +49,5 @@ - + diff --git a/challenge-165/paulo-custodio/perl/ch-1.pl b/challenge-165/paulo-custodio/perl/ch-1.pl index 116422d124..e35a965a7e 100644 --- a/challenge-165/paulo-custodio/perl/ch-1.pl +++ b/challenge-165/paulo-custodio/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 165 # @@ -7,13 +7,13 @@ # # Scalable Vector Graphics (SVG) are not made of pixels, but lines, ellipses, # and curves, that can be scaled to any size without any loss of quality. If you -# have ever tried to resize a small JPG or PNG, you know what I mean by “loss of -# quality”! What many people do not know about SVG files is, they are simply XML +# have ever tried to resize a small JPG or PNG, you know what I mean by "loss of +# quality"! What many people do not know about SVG files is, they are simply XML # files, so they can easily be generated programmatically. # -# For this task, you may use external library, such as Perl’s SVG library, +# For this task, you may use external library, such as Perl's SVG library, # maintained in recent years by our very own Mohammad S Anwar. You can instead -# generate the XML yourself; it’s actually quite simple. The source for the +# generate the XML yourself; it's actually quite simple. The source for the # example image for Task #2 might be instructive. # # Your task is to accept a series of points and lines in the following format, diff --git a/challenge-165/paulo-custodio/perl/ch-2.pl b/challenge-165/paulo-custodio/perl/ch-2.pl index df2d904645..160b7f1a77 100644 --- a/challenge-165/paulo-custodio/perl/ch-2.pl +++ b/challenge-165/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 165 # @@ -44,6 +44,7 @@ END sub svg_circle { my($cx, $cy, $r)=@_; + for($cx, $cy, $r) {$_=int($_);} return < END @@ -56,6 +57,7 @@ sub svg_point { sub svg_line { my($x1,$y1,$x2,$y2)=@_; + for($x1,$y1,$x2,$y2) {$_=int($_);} return < END diff --git a/challenge-165/paulo-custodio/python/ch-1.py b/challenge-165/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..523e15afe1 --- /dev/null +++ b/challenge-165/paulo-custodio/python/ch-1.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + +# Challenge 165 +# +# Task 1: Scalable Vector Graphics (SVG) +# Submitted by: Ryan J Thompson +# +# Scalable Vector Graphics (SVG) are not made of pixels, but lines, ellipses, +# and curves, that can be scaled to any size without any loss of quality. If you +# have ever tried to resize a small JPG or PNG, you know what I mean by "loss of +# quality"! What many people do not know about SVG files is, they are simply XML +# files, so they can easily be generated programmatically. +# +# For this task, you may use external library, such as Perl's SVG library, +# maintained in recent years by our very own Mohammad S Anwar. You can instead +# generate the XML yourself; it's actually quite simple. The source for the +# example image for Task #2 might be instructive. +# +# Your task is to accept a series of points and lines in the following format, +# one per line, in arbitrary order: +# +# Point: x,y +# +# Line: x1,y1,x2,y2 +# Example: +# +# 53,10 +# 53,10,23,30 +# 23,30 +# +# Then, generate an SVG file plotting all points, and all lines. If done +# correctly, you can view the output .svg file in your browser. + +def svg_header(width, height): + return f''' + + +''' + +def svg_footer(): + return ''' +''' + +def svg_circle(cx, cy, r): + return f'\n' + +def svg_point(cx, cy): + return svg_circle(cx, cy, 1) + +def svg_line(x1, y1, x2, y2): + return f'\n' + +import sys + +file = sys.argv[1] if len(sys.argv) > 1 else None +if file is None: + raise Exception("usage: ch-1.py file.svg") + +with open(file, "w") as f: + f.write(svg_header(100, 100)) + for line in sys.stdin: + line = line.strip() + p = line.split(',') + p = [int(coord.strip()) for coord in p] + if len(p) == 2: + f.write(svg_point(*p)) + elif len(p) == 4: + f.write(svg_line(*p)) + else: + raise Exception(f"cannot parse: {line}") + f.write(svg_footer()) diff --git a/challenge-165/paulo-custodio/python/ch-2.py b/challenge-165/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..265da63b9f --- /dev/null +++ b/challenge-165/paulo-custodio/python/ch-2.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 + +# Challenge 165 +# +# Task 2: Line of Best Fit +# Submitted by: Ryan J Thompson +# +# When you have a scatter plot of points, a line of best fit is the line that +# best describes the relationship between the points, and is very useful in +# statistics. Otherwise known as linear regression, here is an example of what +# such a line might look like: +# +# Hull +# +# The method most often used is known as the least squares method, as it is +# straightforward and efficient, but you may use any method that generates the +# correct result. +# +# Calculate the line of best fit for the following 48 points: +# +# 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 + +import sys + +def svg_header(width, height): + return f''' + + +''' + +def svg_footer(): + return ''' +''' + +def svg_circle(cx, cy, r): + return f'\n' + +def svg_point(cx, cy): + return svg_circle(cx, cy, 1) + +def svg_line(x1, y1, x2, y2): + return f'\n' + +def least_squares(points): + N = len(points) + sum_x = sum_y = sum_x2 = sum_xy = 0 + for x, y in points: + sum_x += x + sum_y += y + sum_x2 += x * x + sum_xy += x * y + m = (N * sum_xy - sum_x * sum_y) / (N * sum_x2 - sum_x * sum_x) + b = (sum_y - m * sum_x) / N + return m, b + +file = sys.argv[1] if len(sys.argv) > 1 else None +if file is None: + raise Exception("usage: ch-1.py file.svg") + +with open(file, "w") as f: + f.write(svg_header(500, 500)) + points = [] + for line in sys.stdin: + for point in line.split(): + x, y = map(int, point.split(',')) + points.append((x, y)) + f.write(svg_point(x, y)) + m, b = least_squares(points) + f.write(svg_line(0, b, 500, m * 500 + b)) + f.write(svg_footer()) -- cgit