Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for What Does the Cow Say? by TovarischZhukov
COW = r'''
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
'''
def cowsay(text):
formated_text = ""
retval = []
for i, ch in enumerate(text):
if i==0 or ch == " " and text[i-1] != " " or ch != " ":
formated_text+=ch
if len(formated_text) > 39:
while formated_text:
pos = formated_text[:40].rfind(" ")
if pos != -1 and len(formated_text)> 39:
retval.append(formated_text[:pos] )#+ (39 - pos)*" ")
formated_text = formated_text[pos+1:]
else:
retval.append(formated_text[:39])#+ (39 - len(formated_text[:39]))*" ")
formated_text = formated_text[39:]
max_len = len(max(retval, key = len))
for i, elem in enumerate(retval):
elem = elem + (max_len - len(elem))*" "
if i == 0:
formated_text+="/ "+elem+" \\\n"
elif i == (len(retval)- 1):
formated_text+="\\ "+elem+" /\n"
else:
formated_text+="| "+elem+" |\n"
formated_text =" "+ (max_len+2)*"_"+ "\n%s " % formated_text + (max_len+2)*"-"
else:
num = len(formated_text)+2
formated_text =" "+ num*"_"+ "\n< %s >\n " % formated_text + num*"-"
return '\n%s%s' % (formated_text, COW)
Dec. 17, 2015