Perl Weekly Challenge 373

Equal List and List Division

Published: 11 May 2026

Task 1: Equal List

You are given two arrays of strings.

Write a script to return true if the two given array represent the same strings otherwise false.

Logic

To determine if two arrays represent the same strings when concatenated:

  1. Join all elements of the first array into a single string
  2. Join all elements of the second array into a single string
  3. Compare the two resulting strings for equality
  4. Return true if they match, false otherwise

This approach works because if the concatenated strings are identical, then the arrays represent the same sequence of characters regardless of how they're split.

Perl Solution

ch-1.pl

#!/usr/bin/env perl use strict; use warnings; use v5.10; sub equal_list { my ($arr1, $arr2) = @_; my $str1 = join '', @$arr1; my $str2 = join '', @$arr2; return $str1 eq $str2 ?  : ; } # Test cases my @tests = ( [ ["a", "bc"], ["ab", "c"], 1 ], [ ["a", "b", "c"], ["a", "bc"], 1 ], [ ["a", "bc"], ["a", "c", "b"], 0 ], [ ["ab", "c", ""], ["", "a", "bc"], 1 ], [ ["p", "e", "r", "l"], ["perl"], 1 ], ); for my $test (@tests) { my $result = equal_list($test->[0], $test->[1]); my $expected = $test->[2]; say $$result == $expected ? 'ok' : 'not ok'; }

Python Solution

ch-1.py

#!/usr/bin/env python3 """Solution for Perl Weekly Challenge 373 Task 1: Equal List""" def equal_list(arr1, arr2): """Return True if two arrays of strings represent the same concatenated string.""" return ''.join(arr1) == ''.join(arr2) # Test cases if __name__ == "__main__": test_cases = [ (["a", "bc"], ["ab", "c"], True), (["a", "b", "c"], ["a", "bc"], True), (["a", "bc"], ["a", "c", "b"], False), (["ab", "c", ""], ["", "a", "bc"], True), (["p", "e", "r", "l"], ["perl"], True), ] for i, (arr1, arr2, expected) in enumerate(test_cases, 1): result = equal_list(arr1, arr2) status = "OK" if result == expected else "FAILED" print(f"Test {i}: {status}")

Task 2: List Division

You are given a list and a non-negative integer.

Write a script to divide the given list into given non-negative integer equal parts. Return -1 if the integer is more than the size of the list.

Logic

To divide a list into n equal parts with remainder distributed from the front:

  1. If n > length of list, return -1
  2. Calculate base size: floor(length / n)
  3. Calculate remainder: length % n
  4. The first 'remainder' chunks will have size (base_size + 1)
  5. The remaining chunks will have size base_size
  6. Extract chunks from the list accordingly

This ensures that any extra elements are distributed to the beginning chunks, making the distribution as balanced as possible.

Perl Solution

ch-2.pl

#!/usr/bin/env perl use strict; use warnings; use v5.10; sub divide_list { my ($list_ref, $n) = @_; my @list = @$list_ref; my $size = scalar @list; return -1 if $n > $size; my $base_size = int($size / $n); my $remainder = $size % $n; my @result; my $start = 0; for my $i (0 .. $n-1) { my $chunk_size = $base_size + ($i < $remainder ? 1 : 0); my $end = $start + $chunk_size; push @result, [ @list[$start..$end-1] ]; $start = $end; } return \@result; } # Test cases my @tests = ( [ [1,2,3,4,5], 2, [ [1,2,3], [4,5] ] ], [ [1,2,3,4,5,6], 3, [ [1,2], [3,4], [5,6] ] ], [ [1,2,3], 2, [ [1,2], [3] ] ], [ [1,2,3,4,5,6,7,8,9,10], 5, [ [1,2], [3,4], [5,6], [7,8], [9,10] ] ], [ [1,2,3], 4, -1 ], [ [72,57,89,55,36,84,10,95,99,35], 7, [ [72,57], [89,55], [36,84], [10], [95], [99], [35] ] ], ); for my $test (@tests) { my $result = divide_list($test->[0], $test->[1]); my $expected = $test->[2]; if ($expected == -1) { say $$result == -1 ? 'ok' : 'not ok'; } else { # For array comparison my $match = 1; if (ref $result ne 'ARRAY' || ref $expected ne 'ARRAY' || @$result != @$expected) { $match = 0; } else { for my $i (0 .. $#$result) { if (ref $result->[$i] ne 'ARRAY' || ref $expected->[$i] ne 'ARRAY' || @{$result->[$i]} != @{$expected->[$i]}) { $match = 0; last; } for my $j (0 .. @{$result->[$i]}-1) { if ($result->[$i][$j] ne $expected->[$i][$j]) { $match = 0; last; } } last unless $match; } } say $match ? 'ok' : 'not ok'; } }

Python Solution

ch-2.py

#!/usr/bin/env python3 """Solution for Perl Weekly Challenge 373 Task 2: List Division""" def divide_list(lst, n): """Divide list into n equal parts, distributing remainder from front. Returns -1 if n > len(lst). """ size = len(lst) if n > size: return -1 base_size = size // n remainder = size % n result = [] start = 0 for i in range(n): # Add 1 to chunk size for first 'remainder' chunks chunk_size = base_size + (1 if i < remainder else 0) end = start + chunk_size result.append(lst[start:end]) start = end return result # Test cases if __name__ == "__main__": test_cases = [ ([1,2,3,4,5], 2, [[1,2,3], [4,5]]), ([1,2,3,4,5,6], 3, [[1,2], [3,4], [5,6]]), ([1,2,3], 2, [[1,2], [3]]), ([1,2,3,4,5,6,7,8,9,10], 5, [[1,2], [3,4], [5,6], [7,8], [9,10]]), ([1,2,3], 4, -1), ([72,57,89,55,36,84,10,95,99,35], 7, [[72,57], [89,55], [36,84], [10], [95], [99], [35]]), ] for i, (lst, n, expected) in enumerate(test_cases, 1): result = divide_list(lst, n) status = "OK" if result == expected else "FAILED" print(f"Test {i}: {status}")