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 Moff
def cowsay(s):
while s.find(' ') > -1:
s = s.replace(' ', ' ')
words = []
w = ''
for c in s:
if c == ' ':
words.append(w)
w = ''
else:
w += c
words.append(w)
lines = ['']
while words:
w = words.pop(0)
if len(w) >= 40:
words.insert(0, w[39:])
w = w[:39]
if len(w) + len(lines[-1]) < 40:
lines[-1] += w + ' '
else:
lines.append(w + ' ')
width = max(len(line) for line in lines) + 1
result = '\n {}\n'.format('_' * width)
if len(lines) == 1:
result += '< {}>\n'.format(lines[0])
else:
for i, line in enumerate(lines):
line += ' ' * (width - len(line) - 1)
if i == 0:
start, end = '/\\'
elif i == len(lines) - 1:
start, end = '\\/'
else:
start, end = '||'
result += '{} {}{}\n'.format(start, line, end)
result += ' {}'.format('-' * width)
result += r'''
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
'''
return result
Aug. 1, 2015