CheckIO Striped Words

Our robots are always working to improve their linguistic skills. For this mission, they research the latin alphabet and its applications. 

The alphabet contains both vowel and consonant letters (yes, we divide the letters).
 Vowels -- A E I O U Y
 Consonants -- B C D F G H J K L M N P Q R S T V W X Z 

You are given a block of text with different words. These words are separated by white-spaces and punctuation marks. Numbers are not considered words in this mission (a mix of letters and digits is not a word either). You should count the number of words (striped words) where the vowels with consonants are alternating, that is; words that you count cannot have two consecutive vowels or consonants. The words consisting of a single letter are not striped -- do not count those. Casing is not significant for this mission. 

Input:  A text as a string (unicode) 

Output:  A quantity of striped words as an integer. 


# 个数为1的单词不计
# 单词中含有其它字符的不计
# vowels  consonants连续出现的不计
照例 贴两份代码 两者思路差不多 ,后者更聪明
VOWELS = "AEIOUY"
CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"
SEPARATORS=".,?"

def checkio(text):
    result=0
    print("\nText: " + text)
    for separator in SEPARATORS:
        text=text.replace(separator," ")
    words=text.split()

    # Checking each word
    for word in words:
        if len(word)<2: continue
        i=0
        while i<len(word)-1:
            if VOWELS.count(word[i].upper())==0 and CONSONANTS.count(word[i].upper())==0 or \
               VOWELS.count(word[i].upper())==VOWELS.count(word[i+1].upper()) or \
               CONSONANTS.count(word[i].upper())==CONSONANTS.count(word[i+1].upper()):
                i=len(word)
            i+=1
            if i==len(word)-1:
                result+=1
    return result
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"
    assert checkio("1st 2a ab3er root rate") == 1

 


VOWELS = "AEIOUY"
CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"
PUNCTUATION = ",.!?"
def checkio(text):
    text = text.upper()
    for c in PUNCTUATION:
        text = text.replace(c, " ")
    # 所有vowels“v”代替
    for c in VOWELS:
        text = text.replace(c, "v" )
    # 所有consonants“c”代替
    for c in CONSONANTS:
        text = text.replace(c, "c" )
    words = text.split()
    count = 0
    print(words)
    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

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"
    assert checkio("1st 2a ab3er root rate") == 1











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值