PythonでStriped Words
コード2
http://www.checkio.org/mission/striped-words/publications/RRRQ/python-3/first/
からの引用。
すげー綺麗なコードで感動した。
VOWELS = "AEIOUY" CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ" PUNCTUATION = ",.!?" def checkio(text): text = text.upper() for c in PUNCTUATION: text = text.replace( c, " " ) for c in VOWELS: text = text.replace( c, "v" ) for c in CONSONANTS: text = text.replace( c, "c" ) words = text.split( " " ) count = 0 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
コード1
VOWELS = "AEIOUY" CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ" def checkio(text): s = text.upper() s = s.encode("utf-8") s = s.replace(","," ") s = s.replace("."," ") s = s.replace("!"," ") s = s.replace("?"," ") s = s.split() count = 0 b = [] print s for i in s: if len(i) == 1: continue print i for x in i: if x in VOWELS and b != "V": check = 1 b = "V" print x,1 elif x in CONSONANTS and b != "C": check = 1 b = "C" print x,2 elif b == "V" and x in CONSONANTS and check == 1: check = 1 print x,3 elif b == "C" and x in VOWELS and check == 1: check = 1 print x,4 else: check = 0 b = [] print x,5 break else: print 6 print check if check == 1: count += 1 check = 0 b = [] print "-----------" print "Count is ",count #These "asserts" using only for self-checking and not necessary for auto-testing if __name__ == '__main__': # checkio(u"My name is ...") #== 3, "All words are striped" # checkio(u"Hello world")# == 0, "No one" # checkio(u"A quantity of striped words.")# == 1, "Only of" # checkio(u"A quantity of striped words")# == 3, "Dog, cat and human" checkio(u"To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it?")