Created
June 5, 2014 10:32
-
-
Save GuillermoPena/e29bc0a6b73ee51bfb24 to your computer and use it in GitHub Desktop.
CheckIO - O'Reilly Challenge 1 : Striped Words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CheckIO - O'Reilly Challenge 1 : Striped Words | |
# http://checkio.org | |
# 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. | |
# Input: A text as a string (unicode) | |
# Output: A quantity of striped words as an integer. | |
VOWELS = "AEIOUY" | |
CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ" | |
SEPARATORS=".,?" | |
def checkio(text): | |
result=0 | |
# Splitting text | |
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()): | |
print("Bad word: " + word + " - Reason: " + word[i] + word[i+1]) | |
i=len(word) | |
i+=1 | |
if i==len(word)-1: | |
print("Correct word: " + word) | |
result+=1 | |
print("Result: " + str(result)) | |
return result | |
#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" | |
assert checkio("1st 2a ab3er root rate") == 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment