aboutsummaryrefslogtreecommitdiff
path: root/challenge-218/lubos-kolouch
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-05-23 20:35:24 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-05-23 20:35:24 +0200
commit7626fcb24af562c8f1d10c951f6fc0338a0df26b (patch)
tree1a57de6f5a9b1bb1c80c4324209f47120e98a9aa /challenge-218/lubos-kolouch
parent1fc2447cb62d692c40a3309f57bce0c92435e14f (diff)
downloadperlweeklychallenge-club-7626fcb24af562c8f1d10c951f6fc0338a0df26b.tar.gz
perlweeklychallenge-club-7626fcb24af562c8f1d10c951f6fc0338a0df26b.tar.bz2
perlweeklychallenge-club-7626fcb24af562c8f1d10c951f6fc0338a0df26b.zip
feat(challenge-072,218/lubos-kolouch/perl,python/): Challenges 072 and 218 LK Perl Python
Diffstat (limited to 'challenge-218/lubos-kolouch')
-rw-r--r--challenge-218/lubos-kolouch/perl/ch-1.pl14
-rw-r--r--challenge-218/lubos-kolouch/perl/ch-2.pl47
-rw-r--r--challenge-218/lubos-kolouch/python/ch-1.py16
-rw-r--r--challenge-218/lubos-kolouch/python/ch-2.py34
4 files changed, 111 insertions, 0 deletions
diff --git a/challenge-218/lubos-kolouch/perl/ch-1.pl b/challenge-218/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..332e9dbadc
--- /dev/null
+++ b/challenge-218/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,14 @@
+use strict;
+use warnings;
+use List::Util qw(min max);
+
+sub max_triplet_product {
+ my @nums = sort { $a <=> $b } @_;
+ return max($nums[0] * $nums[1] * $nums[-1], $nums[-1] * $nums[-2] * $nums[-3]);
+}
+
+print max_triplet_product(3, 1, 2), "\n"; # Output: 6
+print max_triplet_product(4, 1, 3, 2), "\n"; # Output: 24
+print max_triplet_product(-1, 0, 1, 3, 1), "\n"; # Output: 3
+print max_triplet_product(-8, 2, -9, 0, -4, 3), "\n"; # Output: 216
+
diff --git a/challenge-218/lubos-kolouch/perl/ch-2.pl b/challenge-218/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..77116a33fa
--- /dev/null
+++ b/challenge-218/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,47 @@
+use strict;
+use warnings;
+
+# Function to flip a row or column
+sub flip {
+ my ($arr_ref) = @_;
+ return [ map { 1 - $_ } @$arr_ref ];
+}
+
+# Function to convert binary to decimal
+sub binary_to_decimal {
+ my ($binary_ref) = @_;
+ return oct("0b" . join('', @$binary_ref));
+}
+
+sub binary_matrix_score {
+ my ($matrix_ref) = @_;
+ my @matrix = @$matrix_ref;
+
+ # First, optimize rows, make sure the first element is 1
+ for my $row_ref (@matrix) {
+ if ($row_ref->[0] == 0) {
+ $row_ref = flip($row_ref);
+ }
+ }
+
+ # Then, optimize columns excluding the first
+ my $cols = @{$matrix[0]};
+ my $rows = scalar @matrix;
+ for my $col (1 .. $cols-1) {
+ if (scalar(grep {$matrix[$_][$col] == 0} 0 .. $rows-1) > $rows / 2) {
+ $_->[$col] = 1 - $_->[$col] for @matrix;
+ }
+ }
+
+ # Calculate score
+ my $score = 0;
+ $score += binary_to_decimal($_) for @matrix;
+
+ return $score;
+}
+
+# Test cases:
+print binary_matrix_score([[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 0, 0]]); # Output: 39
+print "\n";
+print binary_matrix_score([[0]]); # Output: 1
+
diff --git a/challenge-218/lubos-kolouch/python/ch-1.py b/challenge-218/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..fb0b261042
--- /dev/null
+++ b/challenge-218/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from typing import List
+
+
+def max_triplet_product(nums: List[int]) -> int:
+ nums.sort()
+ return max(nums[-1] * nums[-2] * nums[-3], nums[0] * nums[1] * nums[-1])
+
+
+# Test cases:
+print(max_triplet_product([3, 1, 2])) # Output: 6
+print(max_triplet_product([4, 1, 3, 2])) # Output: 24
+print(max_triplet_product([-1, 0, 1, 3, 1])) # Output: 3
+print(max_triplet_product([-8, 2, -9, 0, -4, 3])) # Output: 216
diff --git a/challenge-218/lubos-kolouch/python/ch-2.py b/challenge-218/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..c8d6620bb2
--- /dev/null
+++ b/challenge-218/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+def binary_matrix_score(matrix):
+ # Function to flip a row or column
+ def flip(arr):
+ return [1 - i for i in arr]
+
+ # Function to convert binary to decimal
+ def binary_to_decimal(binary):
+ return int(''.join(map(str, binary)), 2)
+
+ # First, optimize rows, make sure the first element is 1
+ for row in matrix:
+ if row[0] == 0:
+ row[:] = flip(row)
+
+ # Then, optimize columns excluding the first
+ cols = len(matrix[0])
+ rows = len(matrix)
+ for col in range(1, cols):
+ if sum(matrix[row][col] for row in range(rows)) < rows / 2:
+ for row in range(rows):
+ matrix[row][col] = 1 - matrix[row][col]
+
+ # Calculate score
+ score = sum(binary_to_decimal(row) for row in matrix)
+
+ return score
+
+
+# Test cases:
+print(binary_matrix_score(
+ [[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 0, 0]])) # Output: 39
+print(binary_matrix_score([[0]])) # Output: 1