aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-09-09 13:43:29 +0100
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-09-09 13:43:29 +0100
commiteb600b80b75012b8d65778a17851fc9e39efa6d2 (patch)
tree90fdd0741ada2adc51403172811a8c60266ae4a0
parentc2eb58b3f5ed3f20920013dfa7d7a94c767994f6 (diff)
parentd3a276ea3ec81d3aaf94a8e0494b9fd469027cc4 (diff)
downloadperlweeklychallenge-club-eb600b80b75012b8d65778a17851fc9e39efa6d2.tar.gz
perlweeklychallenge-club-eb600b80b75012b8d65778a17851fc9e39efa6d2.tar.bz2
perlweeklychallenge-club-eb600b80b75012b8d65778a17851fc9e39efa6d2.zip
Merge remote-tracking branch 'refs/remotes/origin/master'
-rw-r--r--challenge-334/lubos-kolouch/README47
-rw-r--r--challenge-334/lubos-kolouch/perl/ch-1.pl53
-rw-r--r--challenge-334/lubos-kolouch/perl/ch-2.pl68
-rw-r--r--challenge-334/lubos-kolouch/python/ch-1.py46
-rw-r--r--challenge-334/lubos-kolouch/python/ch-2.py64
5 files changed, 278 insertions, 0 deletions
diff --git a/challenge-334/lubos-kolouch/README b/challenge-334/lubos-kolouch/README
index 921b2d9f4a..9e89a5abbe 100644
--- a/challenge-334/lubos-kolouch/README
+++ b/challenge-334/lubos-kolouch/README
@@ -1 +1,48 @@
Solutions by Lubos Kolouch.
+
+# Perl Weekly Challenge 334
+
+This repository contains solutions for the Perl Weekly Challenge 334, implemented in both Perl and Python. Below is a summary of the tasks and the code written for each.
+
+## Task 1: Range Sum
+
+### Description
+Given a list of integers and two indices, return the sum of integers between the given indices (inclusive).
+
+### Solution
+- **ch-1.pl**: Implements the `range_sum` function that takes an array of integers and two indices, calculates the sum of elements between the indices (inclusive), and returns the result. Input validation ensures valid indices.
+- **ch-1.py**: Implements the `range_sum` function using Python's `sum` function with list slicing for efficiency. Type hints are included for clarity, and input validation ensures valid indices.
+
+### Test Cases
+1. Input: `[-2, 0, 3, -5, 2, -1]`, `x=0`, `y=2` → Output: `1`
+2. Input: `[1, -2, 3, -4, 5]`, `x=1`, `y=3` → Output: `-3`
+3. Input: `[1, 0, 2, -1, 3]`, `x=3`, `y=4` → Output: `2`
+4. Input: `[-5, 4, -3, 2, -1, 0]`, `x=0`, `y=3` → Output: `-2`
+5. Input: `[-1, 0, 2, -3, -2, 1]`, `x=0`, `y=2` → Output: `1`
+
+## Task 2: Nearest Valid Point
+
+### Description
+Given a current location (x, y) and a list of points, find the index of the valid point (sharing x or y coordinate) with the smallest Manhattan distance. Return -1 if no valid points exist. The Manhattan distance is calculated as `|x1 - x2| + |y1 - y2|`. If multiple points tie, return the one with the lowest index.
+
+### Solution
+- **ch-2.pl**: Implements the `nearest_valid_point` function that iterates through the points, checks for validity (same x or y coordinate), calculates the Manhattan distance, and tracks the point with the smallest distance and lowest index.
+- **ch-2.py**: Implements the `nearest_valid_point` function with similar logic, using Python's enumeration for cleaner iteration. Type hints are included for clarity.
+
+### Test Cases
+1. Input: `x=3`, `y=4`, `points=[[1, 2], [3, 1], [2, 4], [2, 3]]` → Output: `2`
+2. Input: `x=2`, `y=5`, `points=[[3, 4], [2, 3], [1, 5], [2, 5]]` → Output: `3`
+3. Input: `x=1`, `y=1`, `points=[[2, 2], [3, 3], [4, 4]]` → Output: `-1`
+4. Input: `x=0`, `y=0`, `points=[[0, 1], [1, 0], [0, 2], [2, 0]]` → Output: `0`
+5. Input: `x=5`, `y=5`, `points=[[5, 6], [6, 5], [5, 4], [4, 5]]` → Output: `0`
+
+## Running the Solutions
+- **Perl**: Run `perl ch-1.pl` and `perl ch-2.pl` to execute the test cases.
+- **Python**: Run `python3 ch-1.py` and `python3 ch-2.py` to execute the test cases.
+- All solutions include the provided test cases, which are executed using `Test::More` (Perl) and `unittest` (Python).
+
+## Notes
+- The Perl solutions use strict and warnings for robustness.
+- The Python solutions use type hints for better code clarity.
+- Both solutions handle edge cases as per the problem requirements.
+- The code has been tested to ensure all provided test cases pass.
diff --git a/challenge-334/lubos-kolouch/perl/ch-1.pl b/challenge-334/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..f8556d070b
--- /dev/null
+++ b/challenge-334/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+
+=pod
+
+=head1 NAME
+
+ch-1.pl - Perl Weekly Challenge 334, Task 1: Range Sum
+
+=head1 DESCRIPTION
+
+Given a list of integers and two indices, return the sum of integers between the given indices (inclusive).
+
+=head1 FUNCTION
+
+=head2 range_sum
+
+Calculates the sum of integers between indices x and y (inclusive).
+
+=over 4
+
+=item * @ints - Array of integers
+=item * $x - Starting index
+=item * $y - Ending index
+
+=back
+
+Returns: Integer sum of elements between indices x and y.
+
+=cut
+
+sub range_sum {
+ my ( $ints, $x, $y ) = @_;
+
+ # Input validation
+ return 0 unless @$ints && $x <= $y && $x >= 0 && $y < @$ints;
+
+ my $sum = 0;
+ $sum += $ints->[$_] for $x .. $y;
+ return $sum;
+}
+
+# Test cases
+is( range_sum( [ -2, 0, 3, -5, 2, -1 ], 0, 2 ), 1, "Test Case 1" );
+is( range_sum( [ 1, -2, 3, -4, 5 ], 1, 3 ), -3, "Test Case 2" );
+is( range_sum( [ 1, 0, 2, -1, 3 ], 3, 4 ), 2, "Test Case 3" );
+is( range_sum( [ -5, 4, -3, 2, -1, 0 ], 0, 3 ), -2, "Test Case 4" );
+is( range_sum( [ -1, 0, 2, -3, -2, 1 ], 0, 2 ), 1, "Test Case 5" );
+
+done_testing();
diff --git a/challenge-334/lubos-kolouch/perl/ch-2.pl b/challenge-334/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..faa0e8fd6a
--- /dev/null
+++ b/challenge-334/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+
+=pod
+
+=head1 NAME
+
+ch-2.pl - Perl Weekly Challenge 334, Task 2: Nearest Valid Point
+
+=head1 DESCRIPTION
+
+Given a current location (x, y) and a list of points, find the index of the valid point
+(sharing x or y coordinate) with the smallest Manhattan distance. Return -1 if no valid points exist.
+
+=head1 FUNCTION
+
+=head2 nearest_valid_point
+
+Finds the index of the valid point with the smallest Manhattan distance.
+
+=over 4
+
+=item * $x - Current x-coordinate
+=item * $y - Current y-coordinate
+=item * @points - Array of points, each point is [x, y]
+
+=back
+
+Returns: Index of the closest valid point, or -1 if none exist.
+
+=cut
+
+sub nearest_valid_point {
+ my ( $x, $y, $points ) = @_;
+
+ my $min_distance = undef;
+ my $min_index = -1;
+
+ for my $i ( 0 .. @$points - 1 ) {
+ my ( $px, $py ) = @{ $points->[$i] };
+
+ # Check if point is valid (shares x or y coordinate)
+ next unless $px == $x || $py == $y;
+
+ # Calculate Manhattan distance
+ my $distance = abs( $px - $x ) + abs( $py - $y );
+
+ # Update min distance and index if this distance is smaller
+ if ( !defined $min_distance || $distance < $min_distance ) {
+ $min_distance = $distance;
+ $min_index = $i;
+ }
+ }
+
+ return $min_index;
+}
+
+# Test cases
+is( nearest_valid_point( 3, 4, [ [ 1, 2 ], [ 3, 1 ], [ 2, 4 ], [ 2, 3 ] ] ), 2, "Test Case 1" );
+is( nearest_valid_point( 2, 5, [ [ 3, 4 ], [ 2, 3 ], [ 1, 5 ], [ 2, 5 ] ] ), 3, "Test Case 2" );
+is( nearest_valid_point( 1, 1, [ [ 2, 2 ], [ 3, 3 ], [ 4, 4 ] ] ), -1, "Test Case 3" );
+is( nearest_valid_point( 0, 0, [ [ 0, 1 ], [ 1, 0 ], [ 0, 2 ], [ 2, 0 ] ] ), 0, "Test Case 4" );
+is( nearest_valid_point( 5, 5, [ [ 5, 6 ], [ 6, 5 ], [ 5, 4 ], [ 4, 5 ] ] ), 0, "Test Case 5" );
+
+done_testing();
diff --git a/challenge-334/lubos-kolouch/python/ch-1.py b/challenge-334/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..399ffad4fd
--- /dev/null
+++ b/challenge-334/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+"""
+Perl Weekly Challenge 334, Task 1: Range Sum
+
+Given a list of integers and two indices, return the sum of integers between the given indices (inclusive).
+
+Args:
+ ints (List[int]): List of integers
+ x (int): Starting index
+ y (int): Ending index
+
+Returns:
+ int: Sum of elements between indices x and y
+"""
+
+import unittest
+
+
+def range_sum(ints: list[int], x: int, y: int) -> int:
+ # Input validation
+ if not ints or x > y or x < 0 or y >= len(ints):
+ return 0
+
+ return sum(ints[x:y + 1])
+
+
+class TestRangeSum(unittest.TestCase):
+
+ def test_example1(self):
+ self.assertEqual(range_sum([-2, 0, 3, -5, 2, -1], 0, 2), 1)
+
+ def test_example2(self):
+ self.assertEqual(range_sum([1, -2, 3, -4, 5], 1, 3), -3)
+
+ def test_example3(self):
+ self.assertEqual(range_sum([1, 0, 2, -1, 3], 3, 4), 2)
+
+ def test_example4(self):
+ self.assertEqual(range_sum([-5, 4, -3, 2, -1, 0], 0, 3), -2)
+
+ def test_example5(self):
+ self.assertEqual(range_sum([-1, 0, 2, -3, -2, 1], 0, 2), 1)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/challenge-334/lubos-kolouch/python/ch-2.py b/challenge-334/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..64bc6d3353
--- /dev/null
+++ b/challenge-334/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,64 @@
+#!/usr/bin/env python3
+"""
+Perl Weekly Challenge 334, Task 2: Nearest Valid Point
+
+Given a current location (x, y) and a list of points, find the index of the valid point
+(sharing x or y coordinate) with the smallest Manhattan distance. Return -1 if no valid points exist.
+
+Args:
+ x (int): Current x-coordinate
+ y (int): Current y-coordinate
+ points (List[List[int]]): List of points, each point is [x, y]
+
+Returns:
+ int: Index of the closest valid point, or -1 if none exist
+"""
+
+import unittest
+
+
+def nearest_valid_point(x: int, y: int, points: list[list[int]]) -> int:
+ min_distance = float('inf')
+ min_index = -1
+
+ for i, point in enumerate(points):
+ px, py = point
+
+ # Check if point is valid (shares x or y coordinate)
+ if px == x or py == y:
+ # Calculate Manhattan distance
+ distance = abs(px - x) + abs(py - y)
+
+ # Update min distance and index if this distance is smaller
+ if distance < min_distance:
+ min_distance = distance
+ min_index = i
+
+ return min_index
+
+
+class TestNearestValidPoint(unittest.TestCase):
+
+ def test_example1(self):
+ self.assertEqual(
+ nearest_valid_point(3, 4, [[1, 2], [3, 1], [2, 4], [2, 3]]), 2)
+
+ def test_example2(self):
+ self.assertEqual(
+ nearest_valid_point(2, 5, [[3, 4], [2, 3], [1, 5], [2, 5]]), 3)
+
+ def test_example3(self):
+ self.assertEqual(nearest_valid_point(1, 1, [[2, 2], [3, 3], [4, 4]]),
+ -1)
+
+ def test_example4(self):
+ self.assertEqual(
+ nearest_valid_point(0, 0, [[0, 1], [1, 0], [0, 2], [2, 0]]), 0)
+
+ def test_example5(self):
+ self.assertEqual(
+ nearest_valid_point(5, 5, [[5, 6], [6, 5], [5, 4], [4, 5]]), 0)
+
+
+if __name__ == '__main__':
+ unittest.main()