【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 = 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-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 = 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 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 reVOWELS = "AEIOUY" CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"def checkio(text):return len(re.findall(rf'(?:\b(?:[{CONSONANTS}][{VOWELS}])+[{CONSONANTS}]?\b|\b(?:[{VOWELS}][{CONSONANTS}])+[{VOWELS}]?\b)', text.upper()))總結
以上是生活随笔為你收集整理的【Python CheckiO 题解】Striped Words的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Python 数据分析三剑客之 Matp
 - 下一篇: 满满印度风!iPhone 14 Pro全