THE WEEKLY CHALLENGE - 229

https://theweeklychallenge.org/blog/perl-weekly-challenge-229/

Table of Contents

Task 1 - Lexicographic Order

In this post, we'll explore a problem that requires us to delete elements that are not sorted in lexicographic order from a given array of strings. The task is to return the count of deletions.

Problem Statement

Given an array of strings, we are to remove any string that is not sorted lexicographically (either forwards or backwards). The goal is to return the number of deletions made.

Examples

  • Example 1:
    Input: ("abc", "bce", "cae")
    Output: 1
    Explanation: Only "cae" is not sorted lexicographically.
  • Example 2:
    Input: ("yxz", "cba", "mon")
    Output: 2
    Explanation: Both "yxz" and "mon" are not sorted lexicographically.

Perl Solution

Here is the Perl code that performs the required task:

sub count_unsorted {
    my @str = @_;
    my $count = 0;

    for my $word (@str) {
        if ($word ne join('', sort split //, $word) &&
            $word ne join('', reverse sort split //, $word)) {
            $count++;
        }
    }

    return $count;
}

How It Works - Perl

  1. Loop Through Strings: We loop through each string in the array.
  2. Check Sorting Order: We compare the string with both its sorted and reverse sorted forms.
  3. Increment Counter: If the string does not match either sorted form, we increment the counter.
  4. Return Counter: Finally, we return the counter as the result.

Python Solution

Here is the Python code that performs the same task:

def count_deletions(strings: List[str]) -> int:
    return sum(1 for s in strings if s != ''.join(sorted(s)) and s != ''.join(sorted(s, reverse=True)))

How It Works - Python

The Python code follows the same logic as the Perl code, but utilizes Python's built-in sorted function and string joining.

Conclusion

Both Perl and Python provide elegant and concise ways to tackle this lexicographic sorting problem. By using native sorting and string manipulation functions, we are able to quickly filter out strings that don't meet the criteria and count the deletions. Whether we choose Perl's explicit splitting and joining or Python's more concise sorting approach, the underlying logic remains the same, demonstrating the universality of programming logic across different languages.


Task 2 - Two out of Three

The task at hand is to write a script that takes three arrays of integers and returns all the elements that are present in at least two out of the three given arrays.

Problem Statement

Given three arrays of integers, we are required to find the elements that are present in at least two out of the three arrays. The problem is an interesting one and can be solved using various techniques.

Example

Input:

  • @array1 = (1, 1, 2, 4)
  • @array2 = (2, 4)
  • @array3 = (4)

Output:

  • (2, 4)

Perl Solution

Here is the Perl solution for the problem:

sub two_out_of_three {
    my ( $array1, $array2, $array3 ) = @_;
    my %count;

    $count{$_}++ for @$array1;
    $count{$_} += 2 for @$array2;
    $count{$_} += 4 for @$array3;

    return
      sort { $a <=> $b }
      grep { $count{$_} == 1 + 2 || $count{$_} == 2 + 4 || $count{$_} == 1 + 4 || $count{$_} == 1 + 2 + 4 }
      keys %count;
}

Python Solution

The problem can also be solved in Python using a similar approach. Here's the Python code:

def two_out_of_three(array1: List[int], array2: List[int], array3: List[int]) -> List[int]:
    count = {}

    # Count the occurrences of elements in each array using unique values for each array
    for num in array1:
        count[num] = count.get(num, 0) + 1
    for num in array2:
        count[num] = count.get(num, 0) + 2
    for num in array3:
        count[num] = count.get(num, 0) + 4

    # Select elements that appear in at least two arrays
    return sorted(key for key, value in count.items() if value in {1+2, 2+4, 1+4, 1+2+4})

Conclusion

The task of finding elements common to at least two out of three arrays can be solved efficiently using hash tables to count occurrences. The Perl and Python solutions presented here demonstrate this approach, providing concise and effective code to solve the problem.