aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2024-05-14 13:15:23 +0200
committerLubos Kolouch <lubos@kolouch.net>2024-05-14 13:15:23 +0200
commit163ab9b715b400bdd98699d70fd4b7fc52482d4e (patch)
tree7b736635b9137721651e010cd25406dcf2b4cd49
parent02e309b3d451fff60404eb5ab3e539056f99ce0d (diff)
downloadperlweeklychallenge-club-163ab9b715b400bdd98699d70fd4b7fc52482d4e.tar.gz
perlweeklychallenge-club-163ab9b715b400bdd98699d70fd4b7fc52482d4e.tar.bz2
perlweeklychallenge-club-163ab9b715b400bdd98699d70fd4b7fc52482d4e.zip
Challenge 267 LK Perl Python
-rw-r--r--challenge-267/lubos-kolouch/perl/ch-1.pl28
-rw-r--r--challenge-267/lubos-kolouch/perl/ch-2.pl36
-rw-r--r--challenge-267/lubos-kolouch/python/ch-1.py33
-rw-r--r--challenge-267/lubos-kolouch/python/ch-2.py38
4 files changed, 135 insertions, 0 deletions
diff --git a/challenge-267/lubos-kolouch/perl/ch-1.pl b/challenge-267/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..c1b0111a7c
--- /dev/null
+++ b/challenge-267/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+
+sub product_sign {
+ my @ints = @_;
+
+ my $negative_count = 0;
+ foreach my $value (@ints) {
+ return 0 if $value == 0;
+ $negative_count++ if $value < 0;
+ }
+
+ return $negative_count % 2 == 0 ? 1 : -1;
+}
+
+# Test cases
+use Test::More;
+
+is(product_sign(-1, -2, -3, -4, 3, 2, 1), 1, 'Example 1');
+is(product_sign(1, 2, 0, -2, -1), 0, 'Example 2');
+is(product_sign(-1, -1, 1, -1, 2), -1, 'Example 3');
+
+done_testing();
+
+# Print results for visualization
+print "Output Example 1: ", product_sign(-1, -2, -3, -4, 3, 2, 1), "\n";
+print "Output Example 2: ", product_sign(1, 2, 0, -2, -1), "\n";
+print "Output Example 3: ", product_sign(-1, -1, 1, -1, 2), "\n";
diff --git a/challenge-267/lubos-kolouch/perl/ch-2.pl b/challenge-267/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..ec1dabe9db
--- /dev/null
+++ b/challenge-267/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/perl
+use strict;
+use warnings FATAL => 'all';
+
+
+sub line_counts {
+ my ($str, @widths) = @_;
+
+ my $max_width = 100;
+ my $current_width = 0;
+ my $lines = 1;
+
+ foreach my $char (split //, $str) {
+ my $char_width = $widths[ord($char) - ord('a')];
+ if ($current_width + $char_width > $max_width) {
+ $lines++;
+ $current_width = $char_width;
+ } else {
+ $current_width += $char_width;
+ }
+ }
+
+ return ($lines, $current_width);
+}
+
+# Test cases
+use Test::More;
+
+is_deeply([line_counts("abcdefghijklmnopqrstuvwxyz", (10) x 26)], [3, 60], 'Example 1');
+is_deeply([line_counts("bbbcccdddaaa", (4, 10, (10) x 24))], [2, 4], 'Example 2');
+
+done_testing();
+
+# Print results for visualization
+print "Output Example 1: ", join(", ", line_counts("abcdefghijklmnopqrstuvwxyz", (10) x 26)), "\n";
+print "Output Example 2: ", join(", ", line_counts("bbbcccdddaaa", (4, 10, (10) x 24))), "\n";
diff --git a/challenge-267/lubos-kolouch/python/ch-1.py b/challenge-267/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..46a4fe093c
--- /dev/null
+++ b/challenge-267/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,33 @@
+""" Challenge 267 Task 1 LK Python"""
+
+from typing import List
+
+
+def product_sign(ints: List[int]) -> int:
+ """
+ Computes the sign of the product of a list of integers.
+
+ Args:
+ ints (List[int]): A list of integers.
+
+ Returns:
+ int: The sign of the product of the integers (1, -1, or 0).
+ """
+ negative_count = 0
+ for value in ints:
+ if value == 0:
+ return 0
+ elif value < 0:
+ negative_count += 1
+
+ return -1 if negative_count % 2 != 0 else 1
+
+
+# Test cases
+assert product_sign([-1, -2, -3, -4, 3, 2, 1]) == 1
+assert product_sign([1, 2, 0, -2, -1]) == 0
+assert product_sign([-1, -1, 1, -1, 2]) == -1
+
+print(product_sign([-1, -2, -3, -4, 3, 2, 1])) # Output: 1
+print(product_sign([1, 2, 0, -2, -1])) # Output: 0
+print(product_sign([-1, -1, 1, -1, 2])) # Output: -1
diff --git a/challenge-267/lubos-kolouch/python/ch-2.py b/challenge-267/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..850f1ab505
--- /dev/null
+++ b/challenge-267/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,38 @@
+""" Challenge 267 Task 2 LK Python"""
+
+from typing import Tuple, List
+
+
+def line_counts(s: str, widths: List[int]) -> Tuple[int, int]:
+ """
+ Calculates the number of lines and the width of the last line required to display a string.
+
+ Args:
+ s (str): The input string consisting of lowercase English letters.
+ widths (List[int]): A list of 26 integers representing the width of each character from 'a' to 'z'.
+
+ Returns:
+ Tuple[int, int]: A tuple containing the number of lines used and the width of the last line.
+ """
+ max_width = 100
+ current_width = 0
+ lines = 1
+
+ for char in s:
+ char_width = widths[ord(char) - ord("a")]
+ if current_width + char_width > max_width:
+ lines += 1
+ current_width = char_width
+ else:
+ current_width += char_width
+
+ return lines, current_width
+
+
+# Test cases
+assert line_counts("abcdefghijklmnopqrstuvwxyz", [10] * 26) == (3, 60)
+assert line_counts("bbbcccdddaaa", [4, 10] + [10] * 24) == (2, 4)
+
+# Print results for visualization
+print(line_counts("abcdefghijklmnopqrstuvwxyz", [10] * 26)) # Output: (3, 60)
+print(line_counts("bbbcccdddaaa", [4, 10] + [10] * 24)) # Output: (2, 4)