Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Naive solution in Clear category for What Does the Cow Say? by obone
COW = r'''
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
'''
BORDER = (('<', '>'), ('/', '\\'), ('\\', '/'), ('|', '|'))
def cowsay(text):
from re import sub
text = sub(r" +", " ", text)
start, width, line = 0, 0, []
while start < len(text):
if len(text) - start < 40:
stop = len(text)
else:
stop = text.rfind(' ', start, start + 40)
stop = start + 39 if stop == -1 else stop
line.append(text[start:stop])
width = max(width, stop - start)
start = stop if stop == len(text) or text[stop] != ' ' else stop + 1
def border(i):
if len(line) == 1:
return BORDER[0]
if i == 0:
return BORDER[1]
if i == len(line) - 1:
return BORDER[2]
return BORDER[3]
fmt = '{0[0]} {1:<' + str(width) + '} {0[1]}'
cow_say = '\n' + '\n'.join([' ' + '_' * (width + 2)] +
[fmt.format(border(i), line[i]) for i in range(len(line))] +
[' ' + '-' * (width + 2)]) + COW
return cow_say
July 16, 2019
Comments: