aboutsummaryrefslogtreecommitdiff
path: root/challenge-165
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-07-10 18:50:43 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-07-10 18:50:43 +0200
commitd0829f697ac3fa9a5791db99ff1686b1acecc4c9 (patch)
tree445a3fe1a5633bbfe8a28eee587edff7bc647f5f /challenge-165
parentdf8e942514dd66e8eab4d1a4946140b58a57f996 (diff)
downloadperlweeklychallenge-club-d0829f697ac3fa9a5791db99ff1686b1acecc4c9.tar.gz
perlweeklychallenge-club-d0829f697ac3fa9a5791db99ff1686b1acecc4c9.tar.bz2
perlweeklychallenge-club-d0829f697ac3fa9a5791db99ff1686b1acecc4c9.zip
feat(challenge-165/lubos-kolouch/perl,python/): Challenge 165 LK Perl Python
Diffstat (limited to 'challenge-165')
-rw-r--r--challenge-165/lubos-kolouch/perl/ch-1.pl42
-rw-r--r--challenge-165/lubos-kolouch/perl/ch-2.pl63
-rw-r--r--challenge-165/lubos-kolouch/python/ch-1.py39
-rw-r--r--challenge-165/lubos-kolouch/python/ch-2.py61
4 files changed, 205 insertions, 0 deletions
diff --git a/challenge-165/lubos-kolouch/perl/ch-1.pl b/challenge-165/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..e4435d5723
--- /dev/null
+++ b/challenge-165/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,42 @@
+use strict;
+use warnings;
+use XML::Writer;
+use IO::File;
+
+my $output = IO::File->new(">output.svg");
+my $writer = XML::Writer->new( OUTPUT => $output );
+
+$writer->startTag(
+ "svg",
+ 'xmlns' => "http://www.w3.org/2000/svg",
+ 'version' => "1.1"
+);
+
+my @points = ( [ 53, 10 ], [ 23, 30 ] );
+my @lines = ( [ 53, 10, 23, 30 ] );
+
+foreach my $point (@points) {
+ $writer->emptyTag(
+ "circle",
+ 'cx' => $point->[0],
+ 'cy' => $point->[1],
+ 'r' => "1",
+ 'stroke' => "black",
+ 'fill' => "black"
+ );
+}
+
+foreach my $line (@lines) {
+ $writer->emptyTag(
+ "line",
+ 'x1' => $line->[0],
+ 'y1' => $line->[1],
+ 'x2' => $line->[2],
+ 'y2' => $line->[3],
+ 'stroke' => "black"
+ );
+}
+
+$writer->endTag("svg");
+$writer->end();
+$output->close();
diff --git a/challenge-165/lubos-kolouch/perl/ch-2.pl b/challenge-165/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..da4d09cc65
--- /dev/null
+++ b/challenge-165/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,63 @@
+use strict;
+use warnings;
+use Statistics::LineFit;
+
+my @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 ]
+);
+
+my ( $x, $y ) = map {
+ [ map { $_->[$_] } @points ]
+} 0, 1;
+my $lineFit = Statistics::LineFit->new();
+$lineFit->setData( $x, $y ) or die "Invalid data";
+my ( $intercept, $slope ) = $lineFit->coefficients();
+
+print "Line of Best Fit: y = $slope * x + $intercept\n";
diff --git a/challenge-165/lubos-kolouch/python/ch-1.py b/challenge-165/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..158b541b3b
--- /dev/null
+++ b/challenge-165/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import xml.etree.ElementTree as ET
+
+
+def generate_svg(points, lines):
+ svg = ET.Element("svg", xmlns="http://www.w3.org/2000/svg", version="1.1")
+
+ for point in points:
+ circle = ET.SubElement(
+ svg,
+ "circle",
+ cx=str(point[0]),
+ cy=str(point[1]),
+ r="1",
+ stroke="black",
+ fill="black",
+ )
+
+ for line in lines:
+ line = ET.SubElement(
+ svg,
+ "line",
+ x1=str(line[0]),
+ y1=str(line[1]),
+ x2=str(line[2]),
+ y2=str(line[3]),
+ stroke="black",
+ )
+
+ tree = ET.ElementTree(svg)
+ tree.write("output.svg")
+
+
+# testing
+points = [(53, 10), (23, 30)]
+lines = [(53, 10, 23, 30)]
+generate_svg(points, lines)
diff --git a/challenge-165/lubos-kolouch/python/ch-2.py b/challenge-165/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..7f82af7731
--- /dev/null
+++ b/challenge-165/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,61 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import numpy as np
+
+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),
+]
+
+x, y = zip(*points)
+A = np.vstack([x, np.ones(len(x))]).T
+m, c = np.linalg.lstsq(A, y, rcond=None)[0]
+
+print(f"Line of Best Fit: y = {m}x + {c}")