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.

Example:

?
1
2
3
4
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

How it is used: This idea in this task is a useful exercise for linguistic research and analysis. Text processing is one of the main tools used in the analysis of various books and languages and can help translate print text to a digital format.

Precondition:The text contains only ASCII symbols.
0 < |text| < 105


答案:

VOWELS = "AEIOUY"
CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"
def checkio(s):
    s=s.replace(","," ")
    s=s.replace("."," ")
    l=s.split(' ')
    count = 0
    for i in l:
        if len(i) <2:
            continue
        else:
            flag = 0
            for j in range(len(i)-1):
                if (i[j].upper() in VOWELS and i[j+1].upper() in VOWELS) or (i[j].upper() in CONSONANTS and i[j+1].upper() in CONSONANTS) or not i[j].isalpha():
                    flag=0
                    break
                else:
                    flag=1
            count+=flag
    return count

你可能感兴趣的:(checkio -- Striped Words)