Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Striped words w/ itertools.cycle solution in Clear category for Striped Words by Kaan
import itertools, string
trans_fr = "abcdefghijklmnopqrstuvwxyz"
trans_to = "VCCCVCCCVCCCCCVCCCCCVCCCVC"
convert_table = str.maketrans(trans_fr, trans_to)
punctuation_remove = str.maketrans(string.punctuation, " "*len(string.punctuation))
def is_striped(word):
word = word.lower().translate(convert_table)
if len(word) < 2: return 0
striped_cycle1 = itertools.cycle("VC")
striped_cycle2 = itertools.cycle("CV")
if all(word_letter==pattern_letter for word_letter, pattern_letter in zip(word,striped_cycle1)):
return 1
elif all(word_letter==pattern_letter for word_letter, pattern_letter in zip(word,striped_cycle2)):
return 1
else:
return 0
def checkio(text):
text = text.translate(punctuation_remove)
return sum([is_striped(word) for word in text.split()])
#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"
April 5, 2015