diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-30 16:04:31 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-30 16:04:31 +0100 |
| commit | b0d5d893606d0309a92f521b103eca08c3eb6386 (patch) | |
| tree | 1d0f8ba09e11c35bd102ae76fb461f1efd0d2aa4 /challenge-165 | |
| parent | 178d4e45aa6ff1bcc926830d279bdd1f54a0eaac (diff) | |
| download | perlweeklychallenge-club-b0d5d893606d0309a92f521b103eca08c3eb6386.tar.gz perlweeklychallenge-club-b0d5d893606d0309a92f521b103eca08c3eb6386.tar.bz2 perlweeklychallenge-club-b0d5d893606d0309a92f521b103eca08c3eb6386.zip | |
Add Python solution to challenge 165
Diffstat (limited to 'challenge-165')
| -rw-r--r-- | challenge-165/paulo-custodio/ch-2.svg | 2 | ||||
| -rw-r--r-- | challenge-165/paulo-custodio/perl/ch-1.pl | 10 | ||||
| -rw-r--r-- | challenge-165/paulo-custodio/perl/ch-2.pl | 4 | ||||
| -rw-r--r-- | challenge-165/paulo-custodio/python/ch-1.py | 71 | ||||
| -rw-r--r-- | challenge-165/paulo-custodio/python/ch-2.py | 75 |
5 files changed, 155 insertions, 7 deletions
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 @@ <circle cx="189" cy="154" r="1" stroke="black" /> <circle cx="361" cy="82" r="1" stroke="black" /> <circle cx="363" cy="89" r="1" stroke="black" /> -<line x1="0" y1="200.132272535582" x2="500" y2="50.1540224049662" stroke="black" /> +<line x1="0" y1="200" x2="500" y2="50" stroke="black" /> </svg> 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; <circle cx="$cx" cy="$cy" r="$r" stroke="black" /> END @@ -56,6 +57,7 @@ sub svg_point { sub svg_line { my($x1,$y1,$x2,$y2)=@_; + for($x1,$y1,$x2,$y2) {$_=int($_);} return <<END; <line x1="$x1" y1="$y1" x2="$x2" y2="$y2" stroke="black" /> 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'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> +<svg height="{height}" width="{width}" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> +''' + +def svg_footer(): + return '''</svg> +''' + +def svg_circle(cx, cy, r): + return f'<circle cx="{cx}" cy="{cy}" r="{r}" stroke="black" />\n' + +def svg_point(cx, cy): + return svg_circle(cx, cy, 1) + +def svg_line(x1, y1, x2, y2): + return f'<line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y2}" stroke="black" />\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'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> +<svg height="{height}" width="{width}" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> +''' + +def svg_footer(): + return '''</svg> +''' + +def svg_circle(cx, cy, r): + return f'<circle cx="{int(cx)}" cy="{int(cy)}" r="{int(r)}" stroke="black" />\n' + +def svg_point(cx, cy): + return svg_circle(cx, cy, 1) + +def svg_line(x1, y1, x2, y2): + return f'<line x1="{int(x1)}" y1="{int(y1)}" x2="{int(x2)}" y2="{int(y2)}" stroke="black" />\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()) |
