Weekly Challenge 377
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.
Task 1: Reverse Existence
Task
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.
My solution
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";
}
Examples
$ ./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
Task 2: Prefix Suffix
Task
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.
My solution
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;
}
Examples
$ ./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
Top comments (0)