aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-07-11 09:37:37 +0100
committerGitHub <noreply@github.com>2023-07-11 09:37:37 +0100
commitf6f10ca8fde69577f0cce76aa5b8660d502ed851 (patch)
tree2392c5c5fb57a41ec88a21d98e7b7a688a678875
parent2c757db65679e6165b9eda52ad6a5b25c54a3c75 (diff)
parenta5f6057bc0293d12abb6ad6864a72196fa417d6e (diff)
downloadperlweeklychallenge-club-f6f10ca8fde69577f0cce76aa5b8660d502ed851.tar.gz
perlweeklychallenge-club-f6f10ca8fde69577f0cce76aa5b8660d502ed851.tar.bz2
perlweeklychallenge-club-f6f10ca8fde69577f0cce76aa5b8660d502ed851.zip
Merge pull request #8352 from LubosKolouch/master
Challenge 165 166 225 LK Perl Python
-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
-rw-r--r--challenge-166/lubos-kolouch/perl/ch-1.pl37
-rw-r--r--challenge-166/lubos-kolouch/perl/ch-2.pl20
-rw-r--r--challenge-166/lubos-kolouch/python/ch-1.py27
-rw-r--r--challenge-166/lubos-kolouch/python/ch-2.py29
-rw-r--r--challenge-225/lubos-kolouch/perl/ch-1.pl24
-rw-r--r--challenge-225/lubos-kolouch/perl/ch-2.pl23
-rw-r--r--challenge-225/lubos-kolouch/python/ch-1.py16
-rw-r--r--challenge-225/lubos-kolouch/python/ch-2.py19
12 files changed, 400 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}")
diff --git a/challenge-166/lubos-kolouch/perl/ch-1.pl b/challenge-166/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..4e526699d0
--- /dev/null
+++ b/challenge-166/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,37 @@
+use strict;
+use warnings;
+
+sub hex_words {
+ my ($dictionary_path) = @_;
+ my %hex_map =
+ ( "o" => "0", "l" => "1", "i" => "1", "s" => "5", "t" => "7" );
+ my $valid_chars = "abcdef" . join( "", values %hex_map );
+ my @words;
+
+ open my $fh, '<', $dictionary_path or die $!;
+ while ( my $line = <$fh> ) {
+ chomp $line;
+ $line = lc $line;
+ if ( 2 <= length($line)
+ && length($line) <= 8
+ && $line =~ m/^[${valid_chars}]+$/ )
+ {
+ $line =~ s/(.)/exists($hex_map{$1}) ? $hex_map{$1} : $1/eg;
+ push @words, "0x$line";
+ }
+ }
+ close $fh;
+
+ return @words;
+}
+
+# path to dictionary file
+my $dictionary_path = "../../../data/dictionary.txt";
+
+# get hex words
+my @hex_words = hex_words($dictionary_path);
+
+# print hex words
+for my $word (@hex_words) {
+ print "$word\n";
+}
diff --git a/challenge-166/lubos-kolouch/perl/ch-2.pl b/challenge-166/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..fe9d266b86
--- /dev/null
+++ b/challenge-166/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+use File::Slurp;
+
+my @dirs = ( 'dir_a', 'dir_b', 'dir_c' );
+my %all_files;
+
+foreach my $dir (@dirs) {
+ my @files = read_dir($dir);
+ foreach my $file (@files) {
+ push @{ $all_files{$file} }, $dir;
+ }
+}
+
+foreach my $file ( keys %all_files ) {
+ if ( scalar @{ $all_files{$file} } != scalar @dirs ) {
+ print "$file is missing in ",
+ join( ", ", grep { not $_ ~~ @{ $all_files{$file} } } @dirs ), "\n";
+ }
+}
diff --git a/challenge-166/lubos-kolouch/python/ch-1.py b/challenge-166/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..fef9376358
--- /dev/null
+++ b/challenge-166/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def hex_words(dictionary_path):
+ hex_map = {"o": "0", "l": "1", "i": "1", "s": "5", "t": "7"}
+ valid_chars = set("abcdef" + "".join(hex_map.values()))
+ words = []
+
+ with open(dictionary_path, "r") as f:
+ for line in f:
+ word = line.strip().lower()
+ if 2 <= len(word) <= 8 and set(word).issubset(valid_chars):
+ words.append("0x" + "".join(hex_map.get(c, c) for c in word))
+
+ return words
+
+
+# path to dictionary file
+dictionary_path = "../../../data/dictionary.txt"
+
+# get hex words
+hex_words = hex_words(dictionary_path)
+
+# print hex words
+for word in hex_words:
+ print(word)
diff --git a/challenge-166/lubos-kolouch/python/ch-2.py b/challenge-166/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..48996dd42e
--- /dev/null
+++ b/challenge-166/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import os
+
+
+def k_directory_diff(dirs):
+ all_files = set()
+ dir_files = {}
+
+ for directory in dirs:
+ files = set(os.listdir(directory))
+ all_files |= files
+ dir_files[directory] = files
+
+ missing_files = {f: [] for f in all_files}
+
+ for directory, files in dir_files.items():
+ for file in all_files:
+ if file not in files:
+ missing_files[file].append(directory)
+
+ for file, directories in missing_files.items():
+ if directories:
+ print(file, "is missing in", ", ".join(directories))
+
+
+dirs = ["dir_a", "dir_b", "dir_c"]
+k_directory_diff(dirs)
diff --git a/challenge-225/lubos-kolouch/perl/ch-1.pl b/challenge-225/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..99803d2ae3
--- /dev/null
+++ b/challenge-225/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub max_words {
+ my @list = @_;
+ my $max_count = 0;
+
+ foreach my $sentence (@list) {
+ my $word_count = scalar( split( /\s+/, $sentence ) );
+ $max_count = $word_count if $word_count > $max_count;
+ }
+
+ return $max_count;
+}
+
+# testing
+my @list = (
+ "Perl and Raku belong to the same family.",
+ "I love Perl.",
+ "The Perl and Raku Conference."
+);
+
+print max_words(@list), "\n"; # should print 8
diff --git a/challenge-225/lubos-kolouch/perl/ch-2.pl b/challenge-225/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..6a8219b87f
--- /dev/null
+++ b/challenge-225/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,23 @@
+use strict;
+use warnings;
+use List::Util qw(sum);
+
+sub left_right_sum_diff {
+ my @ints = @_;
+ my @left = map { $_ > 0 ? sum( @ints[ 0 .. $_ - 1 ] ) : 0 } ( 0 .. $#ints );
+ my @right = map { $_ < $#ints ? sum( @ints[ $_ + 1 .. $#ints ] ) : 0 }
+ ( 0 .. $#ints );
+ return [ map { abs( $left[$_] - $right[$_] ) } ( 0 .. $#ints ) ];
+}
+
+# testing
+my @ints = ( 10, 4, 8, 3 );
+print join( ", ", @{ left_right_sum_diff(@ints) } ),
+ "\n"; # should print "15, 1, 11, 22"
+
+@ints = (1);
+print join( ", ", @{ left_right_sum_diff(@ints) } ), "\n"; # should print "0"
+
+@ints = ( 1, 2, 3, 4, 5 );
+print join( ", ", @{ left_right_sum_diff(@ints) } ),
+ "\n"; # should print "14, 11, 6, 1, 10"
diff --git a/challenge-225/lubos-kolouch/python/ch-1.py b/challenge-225/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..6826ff1a30
--- /dev/null
+++ b/challenge-225/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def max_words(lst):
+ return max(len(sentence.split()) for sentence in lst)
+
+
+# testing
+list = [
+ "Perl and Raku belong to the same family.",
+ "I love Perl.",
+ "The Perl and Raku Conference.",
+]
+
+print(max_words(list)) # should print 8
diff --git a/challenge-225/lubos-kolouch/python/ch-2.py b/challenge-225/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..18ed09f29b
--- /dev/null
+++ b/challenge-225/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def left_right_sum_diff(ints):
+ left = [sum(ints[:i]) for i in range(len(ints))]
+ right = [sum(ints[i + 1 :]) for i in range(len(ints))]
+ return [abs(l - r) for l, r in zip(left, right)]
+
+
+# testing
+ints = [10, 4, 8, 3]
+print(left_right_sum_diff(ints)) # should print [15, 1, 11, 22]
+
+ints = [1]
+print(left_right_sum_diff(ints)) # should print [0]
+
+ints = [1, 2, 3, 4, 5]
+print(left_right_sum_diff(ints)) # should print [14, 11, 6, 1, 10]