Circular Sentence
We need to determine if a given sentence is circular, which means that the last character of each word matches the first character of the next word, and the last character of the last word matches the first character of the first word.
Here’s how you can do this:
- Split the sentence into words.
- Iterate through the words and compare the last character of the current word with the first character of the next word.
- Additionally, compare the last character of the last word with the first character of the first word.
- If any of the comparisons fail, return
False
. - If all comparisons pass, return
True
.
Below is the code that applies these steps:
|
|
Explanation:
words = sentence.split()
splits the sentence into individual words.- The first
for
loop iterates through the words and compares the last character of the current word (words[i][-1]
) with the first character of the next word (words[i + 1][0]
). If they don’t match, the sentence is not circular, so it returnsFalse
. - After the loop, the code checks if the last character of the last word matches the first character of the first word. If they don’t match, it returns
False
. - If all the checks pass, the code returns
True
, indicating that the sentence is circular.