【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 ...") == 3
checkio("Hello world") == 0
checkio("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 = 0
    n = 0
    for i in text:
        if len(i) < 2:
            n = 0
        for 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 = 0
                break
            else:
                n = 1
        num += n
    return num


# These "asserts" using only for self-checking and not necessary for auto-testing
if __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 = 0
    for word in words:
        if len( word ) > 1 and word.isalpha():
            if word.find( "cc" ) == -1 and word.find( "vv" ) == -1:
                count += 1

    return count

大神解答 NO.2

import re

VOWELS = "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 re
checkio=lambda t:sum(any(all('@'<c and j^(c in'aeiouyAEIOUY')^i&1
for i,c in enumerate(w))for j in(0,1))for w in re.findall(r"\w\w+",t))

大神解答 NO.4

import re

VOWELS = "AEIOUY"
CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"


def checkio(text):
    return len(re.findall(rf'(?:\b(?:[{CONSONANTS}][{VOWELS}])+[{CONSONANTS}]?\b|\b(?:[{VOWELS}][{CONSONANTS}])+[{VOWELS}]?\b)', text.upper()))
展开阅读全文
©️2020 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值