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.
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