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. 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. 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. php 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. php 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 https://docs.python.org/3/library/itertools.html itertools.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. php 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 . php 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 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. 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