aboutsummaryrefslogtreecommitdiff
path: root/challenge-270
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2024-05-24 15:01:59 +0200
committerLubos Kolouch <lubos@kolouch.net>2024-05-24 15:01:59 +0200
commitf748d4e18be236953c23f293a38b82c57d540b85 (patch)
tree1f0b630953512f999ce4f9acc1da26171497eeaf /challenge-270
parentf7763c07fc15a870408ee9b9e7d551da3cefa239 (diff)
downloadperlweeklychallenge-club-f748d4e18be236953c23f293a38b82c57d540b85.tar.gz
perlweeklychallenge-club-f748d4e18be236953c23f293a38b82c57d540b85.tar.bz2
perlweeklychallenge-club-f748d4e18be236953c23f293a38b82c57d540b85.zip
Challenge 270 LK Perl Python
Diffstat (limited to 'challenge-270')
-rw-r--r--challenge-270/lubos-kolouch/perl/ch-1.pl34
-rw-r--r--challenge-270/lubos-kolouch/perl/ch-2.pl74
-rw-r--r--challenge-270/lubos-kolouch/python/ch-1.py44
-rw-r--r--challenge-270/lubos-kolouch/python/ch-2.py57
4 files changed, 209 insertions, 0 deletions
diff --git a/challenge-270/lubos-kolouch/perl/ch-1.pl b/challenge-270/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..aeee1e3e99
--- /dev/null
+++ b/challenge-270/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+use List::Util qw(all);
+
+sub num_special_positions {
+ my ($matrix) = @_;
+ my $rows = scalar(@$matrix);
+ my $cols = scalar( @{ $matrix->[0] } );
+ my $special_count = 0;
+
+ for my $i ( 0 .. $rows - 1 ) {
+ for my $j ( 0 .. $cols - 1 ) {
+ if ( $matrix->[$i][$j] == 1 ) {
+ my $row_check = all { $_ == 0 }
+ @{ $matrix->[$i] }[ 0 .. $j - 1, $j + 1 .. $cols - 1 ];
+ my $col_check = all { $_ == 0 }
+ map { $matrix->[$_][$j] } grep { $_ != $i } 0 .. $rows - 1;
+ if ( $row_check && $col_check ) {
+ $special_count++;
+ }
+ }
+ }
+ }
+
+ return $special_count;
+}
+
+# Example 1
+my $matrix1 = [ [ 1, 0, 0 ], [ 0, 0, 1 ], [ 1, 0, 0 ] ];
+print num_special_positions($matrix1) == 1 ? "Pass\n" : "Fail\n";
+
+# Example 2
+my $matrix2 = [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ];
+print num_special_positions($matrix2) == 3 ? "Pass\n" : "Fail\n";
diff --git a/challenge-270/lubos-kolouch/perl/ch-2.pl b/challenge-270/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..519759ae8c
--- /dev/null
+++ b/challenge-270/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/perl
+use strict;
+use warnings FATAL => 'all';
+
+sub min_cost_to_equal_elements {
+ my ($ints_ref, $x, $y) = @_;
+ my @ints = @$ints_ref;
+
+ my $min_cost = "inf";
+ my $max_val = max(@ints);
+ my $min_val = min(@ints);
+
+ print "Initial array: (@ints)\n";
+ print "Cost for Level 1 (x): $x\n";
+ print "Cost for Level 2 (y): $y\n";
+
+ for my $target ($min_val .. $max_val) {
+ my $cost = 0;
+ print "\nTrying to make all elements equal to: $target\n";
+
+ for my $num (@ints) {
+ my $diff = abs($target - $num);
+ print "Difference for element $num to target $target: $diff\n";
+
+ my $cost_addition;
+ if ($y < 2 * $x) {
+ my $level_2_ops = int($diff / 2);
+ my $level_1_ops = $diff % 2;
+ $cost_addition = ($level_2_ops * $y) + ($level_1_ops * $x);
+ print "Using Level 2 and Level 1. Cost to adjust $num to $target: $cost_addition\n";
+ } else {
+ $cost_addition = $diff * $x;
+ print "Using only Level 1. Cost to adjust $num to $target: $cost_addition\n";
+ }
+
+ $cost += $cost_addition;
+ }
+
+ print "Total cost to make all elements $target: $cost\n";
+ $min_cost = $cost if $cost < $min_cost;
+ }
+
+ return $min_cost;
+}
+
+sub max {
+ my @array = @_;
+ my $max = $array[0];
+ foreach (@array) {
+ $max = $_ if $_ > $max;
+ }
+ return $max;
+}
+
+sub min {
+ my @array = @_;
+ my $min = $array[0];
+ foreach (@array) {
+ $min = $_ if $_ < $min;
+ }
+ return $min;
+}
+
+# Example 1
+my @ints1 = (4, 1);
+my $x1 = 3;
+my $y1 = 2;
+print "Minimum cost: " . min_cost_to_equal_elements(\@ints1, $x1, $y1) . "\n"; # Expected Output: 9
+
+# Example 2
+my @ints2 = (2, 3, 3, 3, 5);
+my $x2 = 2;
+my $y2 = 1;
+print "Minimum cost: " . min_cost_to_equal_elements(\@ints2, $x2, $y2) . "\n"; # Expected Output: 6
diff --git a/challenge-270/lubos-kolouch/python/ch-1.py b/challenge-270/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..08a785afbd
--- /dev/null
+++ b/challenge-270/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,44 @@
+""" Perl Weekly Challenge 270 LK Task 1 Python """
+
+from typing import List
+
+
+def num_special_positions(matrix: List[List[int]]) -> int:
+ """
+ num_special_positions A position (i, j) is called special if $matrix[i][j] == 1 and all other elements in the row i and column j are 0.
+
+ Args:
+ matrix (List[List[int]]): input matrix
+
+ Returns:
+ int: count of special positions
+ """
+
+ rows = len(matrix)
+ cols = len(matrix[0])
+
+ def is_special(i: int, j: int) -> bool:
+ for x in range(rows):
+ if x != i and matrix[x][j] == 1:
+ return False
+ for y in range(cols):
+ if y != j and matrix[i][y] == 1:
+ return False
+ return True
+
+ special_count = 0
+ for i in range(rows):
+ for j in range(cols):
+ if matrix[i][j] == 1 and is_special(i, j):
+ special_count += 1
+
+ return special_count
+
+
+# Example 1
+matrix1 = [[1, 0, 0], [0, 0, 1], [1, 0, 0]]
+assert num_special_positions(matrix1) == 1
+
+# Example 2
+matrix2 = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
+assert num_special_positions(matrix2) == 3
diff --git a/challenge-270/lubos-kolouch/python/ch-2.py b/challenge-270/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..23a2d5062a
--- /dev/null
+++ b/challenge-270/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,57 @@
+def min_cost_to_equal_elements(ints, x, y):
+ min_cost = float("inf")
+ max_val = max(ints)
+ min_val = min(ints)
+
+ print(f"Initial array: {ints}")
+ print(f"Cost for Level 1 (x): {x}")
+ print(f"Cost for Level 2 (y): {y}")
+
+ # Try making all elements equal to every value between min_val and max_val
+ for target in range(min_val, max_val + 1):
+ cost = 0
+ print(f"\nTrying to make all elements equal to: {target}")
+
+ for num in ints:
+ diff = abs(target - num)
+ print(f"Difference for element {num} to target {target}: {diff}")
+
+ # Calculate cost using level 2 operations first, then level 1 if needed
+ if y < 2 * x:
+ # Using Level 2 operations where possible
+ level_2_ops = diff // 2
+ level_1_ops = diff % 2
+ cost_addition = (level_2_ops * y) + (level_1_ops * x)
+ print(
+ f"Using Level 2 and Level 1. Cost to adjust {num} to {target}: {cost_addition}"
+ )
+ else:
+ # Using only Level 1 operations
+ cost_addition = diff * x
+ print(
+ f"Using only Level 1. Cost to adjust {num} to {target}: {cost_addition}"
+ )
+
+ cost += cost_addition
+
+ print(f"Total cost to make all elements {target}: {cost}")
+ min_cost = min(min_cost, cost)
+
+ return min_cost
+
+
+# Example 1
+ints1 = [4, 1]
+x1 = 3
+y1 = 2
+print(
+ f"Minimum cost: {min_cost_to_equal_elements(ints1, x1, y1)}"
+) # Expected Output: 9
+
+# Example 2
+ints2 = [2, 3, 3, 3, 5]
+x2 = 2
+y2 = 1
+print(
+ f"Minimum cost: {min_cost_to_equal_elements(ints2, x2, y2)}"
+) # Expected Output: 6