What does the cow say?
Lets cow say!
Our cow is young and can only say some of the words we teach it. Not only does it talk, but this cow can turn into the famous Tux (wiki/Cowsay) if we ask it nicely.
_________________________________ / Dog goes woof \ | Cat goes meow | | Bird goes tweet | | And mouse goes squeek | | Cow goes moo | | Duck goes quack | \ And the solution will go to you / --------------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||What does the cow say?
Input: Text as a string.
Output: The result for the console as a string.
Hint: Read python docs (2.7, 3.3) about formatting styles (str.format and %). Notice for r before the string. It is a raw string and they use different rules for interpreting backslash escape sequences.
How it is used: The original Cowsays are written in the Perl programming language, and as such are easily adaptable to system tasks in Unix. They can perform functions such as telling users their home directories are full, that they have new mail, etc. Now you will write your own realisation for this classic unix program. This concept can teach you how to prepare and format text for the console output.
Precondition: 0 < len(text) < 858;
text contains at least last non-space character;
text contains only ASCII letters, digits and punctuation.
import re def cowsay(words): l=re.split("\s+",words) cutl=[""]*(len(l)+20) j=0 out = "\n" print l for i in l: if len(cutl[j])+len(i)<40: cutl[j]+=i cutl[j]+=' ' elif len(i)>=40: if j!=0: j+=1 while len(i)>=40: cutl[j]= cutl[j]+i[:39]+' ' i=i[39:] j+=1 cutl[j]= cutl[j]+i[:39]+' ' else: j+=1 cutl[j]+=i cutl[j]+=' ' print cutl lenl=sorted(cutl,key=lambda x:len(x),reverse=True) maxlen=len(lenl[0]) for i in range(len(cutl)): if len(cutl[i])>=2 and len(cutl[i])==maxlen and cutl[i][-2]==' ': cutl[i] = cutl[i][0:-1] print cutl maxlen=len(lenl[0]) out= out + " "+(maxlen+1)*'_'+"\n" if len(lenl[1]) == 0: out = out +"< "+cutl[0]+(maxlen-len(cutl[0]))*' '+">"+"\n" else: for i in range(len(cutl)): if len(cutl[i])==0: break if i == 0: out = out +"/ "+cutl[i]+(maxlen-len(cutl[i]))*' '+"\\"+"\n" elif len(cutl[i+1]) == 0: out = out +"\ "+cutl[i]+(maxlen-len(cutl[i]))*' '+"/"+"\n" break else: out = out +"| "+cutl[i]+(maxlen-len(cutl[i]))*' '+"|"+"\n" out= out + " "+(maxlen+1)*'-'+"\n" out+= " \ ^__^\n" out+= " \ (oo)\_______\n" out+= " (__)\ )\/\\\n" out+= " ||----w |\n" out+= " || ||\n" print out return out
下面是我看到的一个较好的版本:
import re from functools import reduce COW = r''' \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || ''' def cowsay(text): lst = re.findall(r'''(?x) (?P<cut> [^\s]{39} ) \s? | (?= ( .{1,39} ) (?: \s | $ ) ) \2 \s?''', re.sub(r'\s+', r' ', text)) lst = [ match for el in lst for match in el if match ] just = max(len(s) for s in lst) lst = list(map(lambda s: s.ljust(just), lst)) forms = [ '< {0} >\n' ] if len(lst) == 1 else \ [ '/ {0} \\\n' ] + [ '| {0} |\n' ]*(len(lst)-2) + ['\\ {0} /\n' ] res = ['\n '+(just+2)*'_'+'\n'] + \ [fmt.format(el) for (fmt, el) in zip(forms, lst)] + [' '+(just+2)*'-'] return reduce(lambda acc,v: acc + v, res, '') + COW