Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Perlish? solution in Creative category for What Does the Cow Say? by ale1ster
import re
from functools import reduce
COW = r'''
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
'''
def cowsay(text):
text = re.sub(r'\s+', r' ', text)
lst = re.findall(r'''(?x) (?P [^\s]{39} ) \s?
| (?= .{1,39} (?: \s | $ ) ) ( .{1,39} ) (?: \s | $ )''',
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)*'-']
res = reduce(lambda acc,v: acc + v, res, '') + COW
return res
May 28, 2014
Comments: