aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2025-10-23 12:20:30 +0200
committerLubos Kolouch <lubos@kolouch.net>2025-10-23 12:20:30 +0200
commit9c43364abb0434e32e49644952de5e48e06fb4a8 (patch)
tree3323f412623e9e18a188aae56ac8cdd58d9b0af1
parent7c0847cc89f19213cb933727c8a775e5409c5d4f (diff)
downloadperlweeklychallenge-club-9c43364abb0434e32e49644952de5e48e06fb4a8.tar.gz
perlweeklychallenge-club-9c43364abb0434e32e49644952de5e48e06fb4a8.tar.bz2
perlweeklychallenge-club-9c43364abb0434e32e49644952de5e48e06fb4a8.zip
Add README for challenge 344 solutions
-rw-r--r--challenge-344/lubos-kolouch/README48
1 files changed, 24 insertions, 24 deletions
diff --git a/challenge-344/lubos-kolouch/README b/challenge-344/lubos-kolouch/README
index dcfc506962..972fc9e0d2 100644
--- a/challenge-344/lubos-kolouch/README
+++ b/challenge-344/lubos-kolouch/README
@@ -1,38 +1,38 @@
# Perl Weekly Challenge Solutions
-This repository contains solutions for the Perl Weekly Challenge tasks in both Perl and Python. Below is a summary of the tasks and the implemented solutions.
+Solutions for Perl Weekly Challenge 344 presented in both Perl and Python.
-## Task 1: Zero Friend
+## Task 1: Array Form Compute
### Description
-Given a list of numbers, find the number closest to zero and return its distance to zero. If zero is present in the list, it is considered the closest, and the distance is 0.
+Given an integer represented as an array of digits (`@ints`) and another integer (`$x`), add them and return the resulting integer in array form.
### Solution
-- **Perl (`ch-1.pl`)**: The `zero_friend` function iterates through the list of numbers, computing the absolute value of each to find the minimum distance to zero. If the list is empty, it returns 0.
-- **Python (`ch-1.py`)**: The `zero_friend` function uses a list comprehension with `min` and `abs` to find the smallest absolute value in the list. Returns 0 for an empty list.
-- **Test Cases**:
- - `(4, 2, -1, 3, -2)` → `1`
- - `(-5, 5, -3, 3, -1, 1)` → `1`
- - `(7, -3, 0, 2, -8)` → `0`
- - `(-2, -5, -1, -8)` → `1`
- - `(-2, 2, -4, 4, -1, 1)` → `1`
+- **Perl (`perl/ch-1.pl`)**: Processes digits from least significant to most significant, maintaining a carry and constructing the resulting digit list.
+- **Python (`python/ch-1.py`)**: Mirrors the Perl approach using an iterative carry-based loop to build the new digit array.
+- **Examples Covered**:
+ - `(1, 2, 3, 4) + 12` → `(1, 2, 4, 6)`
+ - `(2, 7, 4) + 181` → `(4, 5, 5)`
+ - `(9, 9, 9) + 1` → `(1, 0, 0, 0)`
+ - `(1, 0, 0, 0, 0) + 9999` → `(1, 9, 9, 9, 9)`
+ - `(0) + 1000` → `(1, 0, 0, 0)`
-## Task 2: Champion Team
+## Task 2: Array Formation
### Description
-Given a matrix `grid` where `grid[i][j] == 1` means team `i` is stronger than team `j`, and `grid[i][j] == 0` means team `j` is stronger, find the champion team with the most wins. If multiple teams tie for the most wins, return the one that beats the others.
+Determine if the target array (`@target`) can be formed by concatenating subarrays from `@source` without altering the order inside each subarray, though subarrays themselves may be reordered.
### Solution
-- **Perl (`ch-2.pl`)**: The `champion_team` function counts wins for each team by summing the rows of the grid (excluding self-comparisons). It identifies teams with the maximum wins and, if multiple, checks which one beats the others based on the grid.
-- **Python (`ch-2.py`)**: Similar to the Perl solution, it counts wins per team, finds those with the maximum wins, and selects the champion by checking which team beats all other top teams.
-- **Test Cases**:
- - `[[0, 1, 1], [0, 0, 1], [0, 0, 0]]` → `Team 0`
- - `[[0, 1, 0, 0], [0, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0]]` → `Team 3`
- - `[[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 0], [0, 0, 1, 0]]` → `Team 0`
- - `[[0, 1, 1], [0, 0, 0], [0, 1, 0]]` → `Team 0`
- - `[[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 0, 1, 1], [1, 1, 0, 0, 0], [1, 1, 0, 1, 0]]` → `Team 2`
+- **Perl (`perl/ch-2.pl`)**: Iterates through the target, greedily matching unused source subarrays based on their leading element and length, marking each subarray as consumed once used.
+- **Python (`python/ch-2.py`)**: Utilises the same greedy matching strategy, leveraging Python's iteration constructs for clarity.
+- **Examples Covered**:
+ - `([2,3], [1], [4])` → `(1, 2, 3, 4)` → `true`
+ - `([1,3], [2,4])` → `(1, 2, 3, 4)` → `false`
+ - `([9,1], [5,8], [2])` → `(5, 8, 2, 9, 1)` → `true`
+ - `([1], [3])` → `(1, 2, 3)` → `false`
+ - `([7,4,6])` → `(7, 4, 6)` → `true`
## Running the Solutions
-- **Perl**: Run `perl ch-1.pl` or `perl ch-2.pl` to execute the tests.
-- **Python**: Run `python3 ch-1.py` or `python3 ch-2.py` to execute the tests.
-- The solutions have been validated with `flake8`, `pyupgrade`, `bandit` (Python), and `Perl::Critic` (Perl) to ensure code quality.
+- **Perl**: Execute `perl perl/ch-1.pl` or `perl perl/ch-2.pl`.
+- **Python**: Execute `python3 python/ch-1.py` or `python3 python/ch-2.py`.
+- Each script includes embedded unit tests covering all examples from the task specification.