Task 1: Circular
"Chain Reaction: Linking Words via Matching Boundaries!"
Determine if a list of words forms a chain where the last character of each word matches the first character of the next word.
The Strategy: Iterate through the list of words using a pair-wise comparison (current word and next word). For each pair, check if the
substr(current, -1, 1) matches substr(next, 0, 1). In Python, this is cleanly done using zip(words, words[1:]) and all(). In Perl, a simple loop through the indices handles the check.
Perl Implementation
sub is_circular () {
return 1 if @$words <= 1;
for my $i ( 0 .. $#$words - 1 ) {
my $a = $words->[$i];
my $b = $words->[ $i + 1 ];
return 0 if substr( $a, -1, 1 ) ne substr( $b, 0, 1 );
}
return 1;
}
Python Implementation
def is_circular(words: Sequence[str]) -> bool:
if len(words) <= 1:
return True
return all(a[-1] == b[0] for a, b in zip(words, words[1:]))