{"slug": "weekly-challenge-existence", "title": "Weekly Challenge: Existence", "summary": "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.", "body_md": "Each week Mohammad S. Anwar sends out [The Weekly Challenge](https://theweeklychallenge.org/), 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.\n\nYou are given a string.\n\nWrite a script to find whether any substring of length 2 is also present in the reverse of the given string.\n\nThis is relatively straight forward. I create a variable `reversed_string`\n\nwhich is the string reversed. I then have a loop called `start_pos`\n\nthat 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.\n\n``` php\ndef reverse_existence(input_string: str) -> bool:\n    reversed_string = input_string[::-1]\n    for start_pos in range(len(input_string)-1):\n        if reversed_string[start_pos:start_pos+2] in input_string:\n            return True\n\n    return False\n```\n\nThe Perl solution follows the same logic.\n\n``` php\nsub main ($input_string) {\n    my $reversed_string = reverse($input_string);\n    foreach my $start_pos ( 0 .. length($input_string) - 2 ) {\n        if (\n            index( $input_string, substr( $reversed_string, $start_pos, 2 ) )\n            != -1 )\n        {\n            say \"true\";\n            return;\n        }\n    }\n\n    say \"false\";\n}\n$ ./ch-1.py abcba\ntrue\n\n$ ./ch-1.py racecar\ntrue\n\n$ ./ch-1.py abcd\nfalse\n\n$ ./ch-1.py banana\ntrue\n\n$ ./ch-1.py hello\ntrue\n```\n\nYou are given an array of strings.\n\nWrite a script to find if the two strings (`str1`\n\n, `str2`\n\n) in the given array such that str1 is prefix and suffix of str2. Return the total count of such pairs.\n\nIn Python, this is a one line solution. I use the [combinations](https://docs.python.org/3/library/itertools.html#itertools.combinations) function from the itertools module to produce all combinations of pairs of `str1`\n\nand `str2`\n\n. For each pair, I use the `startwith`\n\nand `endswith`\n\nfunction on the strings to see if the criteria is met.\n\n``` php\ndef prefix_suffix(array: list[str]) -> int:\n    return sum(\n        1\n        for str1, str2 in combinations(array, 2)\n        if (str1.startswith(str2) and str1.endswith(str2)) or\n           (str2.startswith(str1) and str2.endswith(str1))\n    )\n```\n\nThe Perl solution is a little more complex. I have a function called `is_prefix_suffix`\n\nthat checks that `s1`\n\nstarts with an ends with `s2`\n\n.\n\n``` php\nsub is_prefix_suffix ( $s1, $s2 ) {\n    my $l = length($s2);\n\n    return ( substr( $s1, 0, $l ) eq $s2 and substr( $s1, 0 - $l ) eq $s2 )\n      ? 1\n      : 0;\n\n}\n```\n\nThe main function uses the combinations function from the [Algorithm::Combinatorics](https://metacpan.org/pod/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.\n\n```\nuse Algorithm::Combinatorics 'combinations';\n\nsub main (@array) {\n    my $count = 0;\n\n    my $iter = combinations( \\@array, 2 );\n    while ( my $c = $iter->next ) {\n        my ( $str1, $str2 ) = @$c;\n        if (   is_prefix_suffix( $str1, $str2 )\n            or is_prefix_suffix( $str2, $str1 ) )\n        {\n            ++$count;\n        }\n    }\n\n    say $count;\n}\n$ ./ch-2.py a aba ababa aa\n4\n\n$ ./ch-2.py pa papa ma mama\n2\n\n$ ./ch-2.py abao ab\n0\n\n$ ./ch-2.py abab abab\n1\n\n$ ./ch-2.py ab abab ababab\n3\n```\n\n", "url": "https://wpnews.pro/news/weekly-challenge-existence", "canonical_source": "https://dev.to/simongreennet/weekly-challenge-existence-4531", "published_at": "2026-06-14 12:28:52+00:00", "updated_at": "2026-06-14 12:40:29.826558+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Mohammad S. Anwar", "The Weekly Challenge", "Python", "Perl", "Algorithm::Combinatorics"], "alternates": {"html": "https://wpnews.pro/news/weekly-challenge-existence", "markdown": "https://wpnews.pro/news/weekly-challenge-existence.md", "text": "https://wpnews.pro/news/weekly-challenge-existence.txt", "jsonld": "https://wpnews.pro/news/weekly-challenge-existence.jsonld"}}