Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
striped_words solution in Clear category for Striped Words by dannedved
VOWELS = "AEIOUY"
CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"
# This function checks if vowels and consonants are altering in a given word.
# The word is transformed to list, wherein each element tells if the corresponding position is occupied by a vowel.
# If vowels and consonants are altering, we receive either [True, False, True, ...] or [False, True, False...]
# We split the list into odd and even positions - in a striped word, either odd positions are all True and even positions all False, or vice versa.
# We check this using set operations.
def isstriped(word):
word = [char.upper() in VOWELS for char in word]
return set(word[::2]).intersection(set(word[1::2])) == set()
def checkio(text):
# We remove punctuation marks and then whitespaces.
for char in text:
if not (char.isalpha() or char.isdigit()):
text = text.replace(char, " ")
text = text.strip().split(" ")
striped_count = 0
for word in text:
if len(word) > 1 and all([char.isalpha() for char in word]):
striped_count += isstriped(word)
return striped_count
#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"
July 11, 2019
Comments: