The Weekly Challenge 310

Arrays Intersection and Sort Odd Even

Task 1: Arrays Intersection

You are given a list of array of integers.

Write a script to return the common elements in all the arrays.

Example 1

Input: $list = ( [1, 2, 3, 4], [4, 5, 6, 1], [4, 2, 1, 3] ) Output: (1, 4)

Example 2

Input: $list = ( [1, 0, 2, 3], [2, 4, 5] ) Output: (2)

Example 3

Input: $list = ( [1, 2, 3], [4, 5], [6] ) Output: ()

Logic

To find the intersection of multiple arrays:

  1. Start with the first array as a set of candidate elements.
  2. For each subsequent array, remove elements from the candidate set that are not present in that array.
  3. After processing all arrays, sort the remaining elements and return them.

This approach efficiently narrows down the common elements by intersecting sets progressively.

Perl Solution

ch-1.pl

package ArraysIntersection; use strict; use warnings; use Test::More; sub arrays_intersection { my @arrays = @_; return () unless @arrays; # Start with first array as set my %common = map { $_ => 1 } @{ $arrays[0] }; # Intersect with remaining arrays foreach my $arr ( @arrays[ 1 .. $#arrays ] ) { my %temp; @temp{@$arr} = (); foreach my $key ( keys %common ) { delete $common{$key} unless exists $temp{$key}; } } my @sorted = sort { $a <=> $b } keys %common; return @sorted; } # Unit tests is_deeply( [ arrays_intersection( [ 1, 2, 3, 4 ], [ 4, 5, 6, 1 ], [ 4, 2, 1, 3 ] ) ], [ 1, 4 ], 'Example 1' ); is_deeply( [ arrays_intersection( [ 1, 0, 2, 3 ], [ 2, 4, 5 ] ) ], [2], 'Example 2' ); is_deeply( [ arrays_intersection( [ 1, 2, 3 ], [ 4, 5 ], [6] ) ], [], 'Example 3' ); is_deeply( [ arrays_intersection() ], [], 'Empty input' ); is_deeply( [ arrays_intersection( [ 1, 2, 3 ] ) ], [ 1, 2, 3 ], 'Single array' ); done_testing(); 1;

Python Solution

ch-1.py

import unittest def arrays_intersection(arrays: list[list[int]]) -> list[int]: """ Find the common elements in all given arrays. Args: arrays: List of integer lists to find intersection of Returns: List of integers that are common to all input arrays """ if not arrays: return [] # Start with first array as set common: set[int] = set(arrays[0]) # Intersect with remaining arrays for arr in arrays[1:]: common.intersection_update(arr) return sorted(common) class TestArraysIntersection(unittest.TestCase): def test_example1(self): self.assertEqual( arrays_intersection([[1, 2, 3, 4], [4, 5, 6, 1], [4, 2, 1, 3]]), [1, 4]) def test_example2(self): self.assertEqual(arrays_intersection([[1, 0, 2, 3], [2, 4, 5]]), [2]) def test_example3(self): self.assertEqual(arrays_intersection([[1, 2, 3], [4, 5], [6]]), []) def test_empty_input(self): self.assertEqual(arrays_intersection([]), []) def test_single_array(self): self.assertEqual(arrays_intersection([[1, 2, 3]]), [1, 2, 3]) if __name__ == '__main__': unittest.main()

Task 2: Sort Odd Even

You are given an array of integers.

Write a script to sort odd index elements in decreasing order and even index elements in increasing order in the given array.

Example 1

Input: @ints = (4, 1, 2, 3) Output: (2, 3, 4, 1) Even index elements: 4, 2 => 2, 4 (increasing order) Odd index elements : 1, 3 => 3, 1 (decreasing order)

Example 2

Input: @ints = (3, 1) Output: (3, 1)

Example 3

Input: @ints = (5, 3, 2, 1, 4) Output: (2, 3, 4, 1, 5) Even index elements: 5, 2, 4 => 2, 4, 5 (increasing order) Odd index elements : 3, 1 => 3, 1 (decreasing order)

Logic

To sort even and odd index elements differently:

  1. Separate the array into two groups: elements at even indices and elements at odd indices.
  2. Sort the even-index elements in ascending order.
  3. Sort the odd-index elements in descending order.
  4. Reconstruct the array by interleaving the sorted groups back into their original positions.

Perl Solution

ch-2.pl

package SortOddEvenIndices; use strict; use warnings; use Test::More; sub sort_odd_even_indices { my @ints = @_; return @ints unless @ints; # Separate even and odd indices my @evens = @ints[ grep { $_ % 2 == 0 } 0 .. $#ints ]; my @odds = @ints[ grep { $_ % 2 == 1 } 0 .. $#ints ]; # Sort them accordingly @evens = sort { $a <=> $b } @evens; @odds = sort { $b <=> $a } @odds; # Reconstruct the array my @result; my ( $e, $o ) = ( 0, 0 ); for my $i ( 0 .. $#ints ) { push @result, ( $i % 2 == 0 ) ? $evens[ $e++ ] : $odds[ $o++ ]; } return @result; } # Unit tests is_deeply( [ sort_odd_even_indices( 4, 1, 2, 3 ) ], [ 2, 3, 4, 1 ], 'Example 1' ); is_deeply( [ sort_odd_even_indices( 3, 1 ) ], [ 3, 1 ], 'Example 2' ); is_deeply( [ sort_odd_even_indices( 5, 3, 2, 1, 4 ) ], [ 2, 3, 4, 1, 5 ], 'Example 3' ); is_deeply( [ sort_odd_even_indices() ], [], 'Empty input' ); is_deeply( [ sort_odd_even_indices(5) ], [5], 'Single element' ); done_testing(); 1;

Python Solution

ch-2.py

import unittest def sort_odd_even_indices(ints: list[int]) -> list[int]: """ Sort even index elements in increasing order and odd index elements in decreasing order. Args: ints: List of integers to be sorted Returns: List with even indices sorted ascending and odd indices sorted descending """ # Separate even and odd indices evens = [ints[i] for i in range(0, len(ints), 2)] odds = [ints[i] for i in range(1, len(ints), 2)] # Sort them accordingly evens.sort() odds.sort(reverse=True) # Reconstruct the array result: list[int] = [] e, o = 0, 0 for i in range(len(ints)): if i % 2 == 0: result.append(evens[e]) e += 1 else: result.append(odds[o]) o += 1 return result class TestSortOddEvenIndices(unittest.TestCase): def test_example1(self): self.assertEqual(sort_odd_even_indices([4, 1, 2, 3]), [2, 3, 4, 1]) def test_example2(self): self.assertEqual(sort_odd_even_indices([3, 1]), [3, 1]) def test_example3(self): self.assertEqual(sort_odd_even_indices([5, 3, 2, 1, 4]), [2, 3, 4, 1, 5]) def test_empty_input(self): self.assertEqual(sort_odd_even_indices([]), []) def test_single_element(self): self.assertEqual(sort_odd_even_indices([5]), [5]) if __name__ == '__main__': unittest.main()