当前位置:首页 >> 编程语言 >> 【Python CheckiO 题解】Striped Words,acer 3810t

【Python CheckiO 题解】Striped Words,acer 3810t

0evadmin 编程语言 1
文件名:【Python CheckiO 题解】Striped Words,acer 3810t 【Python CheckiO 题解】Striped Words

CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思路和实现代码,同时也学习学习其他大神写的代码。

CheckiO 官网:https://checkio.org/

我的 CheckiO 主页:https://py.checkio.org/user/TRHX/

CheckiO 题解系列专栏:https://itrhx.blog.csdn.net/category_9536424.html

CheckiO 所有题解源代码:https://github.com/TRHX/Python-CheckiO-Exercise


题目描述

【Striped Words】:系统会为您提供带有不同单词的文本块,这些单词由空格和标点符号分隔,数字在此任务中不被视为单词(字母和数字的混合体也不是单词),您应该统计辅音和元音交替出现的单词的数量,即:您统计的单词不能有两个连续的元音或辅音,由单个字母组成的单词不计算在内。元音:AEIOUY;辅音:BCDFGHJKLMNPQRSTVWXZ。

【链接】:https://py.checkio.org/mission/striped-words/

【输入】:字符串(unicode)

【输出】:整数

【前提】:文本仅包含ASCII符,0 < len(text) < 105

【范例】:

checkio("My name is ...") == 3checkio("Hello world") == 0checkio("A quantity of striped words.") == 1, "Only of"checkio("Dog,cat,mouse,bird.Human.") == 3 解题思路

首先把所有的 , 和 . 都替换成空格,然后以空格为分隔符将原字符串进行分割,循环访问每一个字符,若相邻两个字符都是元音或辅音,或者字符是数字,或者是单个字符,则表示该单词不符合要求,设置标记符 n,如果不符合要求则为 0,符合要求则为 1,最后返回 n 的和即可。

代码实现 VOWELS = "AEIOUY"CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"def checkio(text):text = text.replace(',', ' ').replace('.', ' ').split()num = 0n = 0for i in text:if len(i) < 2:n = 0for j in range(len(i)-1):if (i[j].upper() in CONSONANTS and i[j+1].upper() in CONSONANTS) or (i[j].upper() in VOWELS and i[j+1].upper() in VOWELS) or i[j].isdigit():n = 0breakelse:n = 1num += nreturn num# These "asserts" using only for self-checking and not necessary for auto-testingif __name__ == '__main__':assert checkio("My name is ...") == 3, "All words are striped"assert checkio("Hello world") == 0, "No one"assert checkio("A quantity of striped words.") == 1, "Only of"assert checkio("Dog,cat,mouse,bird.Human.") == 3, "Dog, cat and human" 大神解答 大神解答 NO.1 VOWELS = "AEIOUY"CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"PUNCTUATION = ",.!?"def checkio(text):text = text.upper()for c in PUNCTUATION:text = text.replace( c, " " )for c in VOWELS:text = text.replace( c, "v" )for c in CONSONANTS:text = text.replace( c, "c" )words = text.split( " " )count = 0for word in words:if len( word ) > 1 and word.isalpha():if word.find( "cc" ) == -1 and word.find( "vv" ) == -1:count += 1return count 大神解答 NO.2 import reVOWELS = "AEIOUY"CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"def checkio(text):return len(re.findall(r'''(?ix) (?#Case-insensitive, verbose)\b( (?#Surrounding word boundaries)(?:(?: [{cons}] [{vow}] )+ [{cons}]?) (?#Word starting with consonant)| (?#Alternative templates)(?:(?: [{vow}] [{cons}] )+ [{vow}]?) (?#Word starting with vowel))\b'''.format(vow=VOWELS, cons=CONSONANTS), text)) 大神解答 NO.3 import recheckio=lambda t:sum(any(all('@'<c and j^(c in'aeiouyAEIOUY')^i&1for i,c in enumerate(w))for j in(0,1))for w in re.findall(r"\w\w+",t)) 大神解答 NO.4 import reVOWELS = "AEIOUY"CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"def checkio(text):return len(re.findall(rf'(?:\b(?:[{CONSONANTS}][{VOWELS}])+[{CONSONANTS}]?\b|\b(?:[{VOWELS}][{CONSONANTS}])+[{VOWELS}]?\b)', text.upper()))
协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接