cd /news/developer-tools/weekly-challenge-existence · home topics developer-tools article
[ARTICLE · art-26938] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Weekly Challenge: Existence

A developer solved The Weekly Challenge tasks using Python and Perl. The first task checks if any substring of length 2 appears in both a string and its reverse. The second task counts pairs of strings where one is both a prefix and suffix of the other.

read3 min publishedJun 14, 2026

Each week Mohammad S. Anwar sends out The Weekly Challenge, a chance for all of us to come up with solutions to two weekly tasks. My solutions are written in Python first, and then converted to Perl. Unless otherwise stated, Copilot (and other AI tools) have NOT been used to generate the solution. It's a great way for us all to practice some coding.

You are given a string.

Write a script to find whether any substring of length 2 is also present in the reverse of the given string.

This is relatively straight forward. I create a variable reversed_string

which is the string reversed. I then have a loop called start_pos

that goes from 0 to 2 less than the length of the string. At each position, I check if the two letters at that position are in the original string.

def reverse_existence(input_string: str) -> bool:
    reversed_string = input_string[::-1]
    for start_pos in range(len(input_string)-1):
        if reversed_string[start_pos:start_pos+2] in input_string:
            return True

    return False

The Perl solution follows the same logic.

sub main ($input_string) {
    my $reversed_string = reverse($input_string);
    foreach my $start_pos ( 0 .. length($input_string) - 2 ) {
        if (
            index( $input_string, substr( $reversed_string, $start_pos, 2 ) )
            != -1 )
        {
            say "true";
            return;
        }
    }

    say "false";
}
$ ./ch-1.py abcba
true

$ ./ch-1.py racecar
true

$ ./ch-1.py abcd
false

$ ./ch-1.py banana
true

$ ./ch-1.py hello
true

You are given an array of strings.

Write a script to find if the two strings (str1

, str2

) in the given array such that str1 is prefix and suffix of str2. Return the total count of such pairs.

In Python, this is a one line solution. I use the combinations function from the itertools module to produce all combinations of pairs of str1

and str2

. For each pair, I use the startwith

and endswith

function on the strings to see if the criteria is met.

def prefix_suffix(array: list[str]) -> int:
    return sum(
        1
        for str1, str2 in combinations(array, 2)
        if (str1.startswith(str2) and str1.endswith(str2)) or
           (str2.startswith(str1) and str2.endswith(str1))
    )

The Perl solution is a little more complex. I have a function called is_prefix_suffix

that checks that s1

starts with an ends with s2

.

sub is_prefix_suffix ( $s1, $s2 ) {
    my $l = length($s2);

    return ( substr( $s1, 0, $l ) eq $s2 and substr( $s1, 0 - $l ) eq $s2 )
      ? 1
      : 0;

}

The main function uses the combinations function from the Algorithm::Combinatorics module to generate all possible pairs. It then calls the above function on each order of the pair to see if it is true.

use Algorithm::Combinatorics 'combinations';

sub main (@array) {
    my $count = 0;

    my $iter = combinations( \@array, 2 );
    while ( my $c = $iter->next ) {
        my ( $str1, $str2 ) = @$c;
        if (   is_prefix_suffix( $str1, $str2 )
            or is_prefix_suffix( $str2, $str1 ) )
        {
            ++$count;
        }
    }

    say $count;
}
$ ./ch-2.py a aba ababa aa
4

$ ./ch-2.py pa papa ma mama
2

$ ./ch-2.py abao ab
0

$ ./ch-2.py abab abab
1

$ ./ch-2.py ab abab ababab
3
── more in #developer-tools 4 stories · sorted by recency
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/weekly-challenge-exi…] indexed:0 read:3min 2026-06-14 ·