Python 实现【相对开音节】

360影视 动漫周边 2025-04-10 11:58 1

摘要:def is_consonant(c):return c.isalpha and c not in {'a', 'e', 'i', 'o', 'u'}def count_open_syllables(word):count = 0n = len(word)fo

def is_consonant(c):return c.isalpha and c not in {'a', 'e', 'i', 'o', 'u'}def count_open_syllables(word):count = 0n = len(word)for i in range(n - 3):c1 = word[i]c2 = word[i+1]c3 = word[i+2]c4 = word[i+3]if (is_consonant(c1) and (c2 in {'a', 'e', 'i', 'o', 'u'}) and (is_consonant(c3) and c3 != 'r') and (c4 == 'e')):count += 1return countdef reverse_word(word):if word.isalpha:return word[::-1]else:return worddef process_string(s):words = s.splittotal = 0for word in words:reversed_word = reverse_word(word)total += count_open_syllables(reversed_word)return totalif __name__ == "__main__":s = input.stripprint(process_string(s))分割字符串:将输入的字符串按空格分割成多个单词。反转每个单词:对于每个单词,检查是否仅包含字母。如果是,则反转该单词;否则保持不变。统计相对开音节子串:在反转后的每个单词中,统计符合相对开音节结构的子串数量。相对开音节的结构定义为:辅音 + 元音(a, e, i, o, u) + 辅音(不能是 r) + e。

来源:万腾教育

相关推荐