Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
I hate this cow... solution in Uncategorized category for What Does the Cow Say? by capback250
# migrated from python 2.7
import re
cow = r'''
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
'''
out = []
def cowsay(words):
global out
addNewLine = True if words[0] == ' ' else False
words = re.sub(r'\s+', r' ', words)
if len(words) < 40:
p1 = "{}{}".format(' ', "_"*(len(words) + 2))
p2 = "{} {} {}".format("<", words, ">")
p3 = "{}{}".format(' ', "-"*(len(words) + 2))
return "\n{}{}".format("\n".join([p1, p2, p3]), cow)
else:
words = list(re.sub(r'\s+', r' ', words))
rec(words)
maxLen = max(list(map(len, out)))
result = [''.join(x) + ' ' * (maxLen - len(x)) for x in out]
if addNewLine:
result.insert(0, ' '*maxLen)
formattetOut = []
formattetOut.append("{}{}\n".format(' ', "_"*(maxLen+2)))
for i, v in enumerate(result):
if i == 0:
formattetOut.append("{}{}{}\n".format("/ ", v, " \\"))
if 0 < i < len(result) -1:
formattetOut.append("{}{}{}\n".format("| ", v, " |"))
if i == len(result) - 1:
formattetOut.append("{}{}{}\n".format("\ ", v, " /"))
formattetOut.append("{}{}".format(' ', "-"*(maxLen+2)))
out = []
return "\n%s%s" % (''.join(formattetOut), cow)
def rec(words):
global out
pattern = words[:39]
if not pattern:
return
elif pattern[0] == ' ':
del words[0]
rec(words)
elif " " not in pattern:
out.append(pattern)
words = words[39:]
rec(words)
else:
rMost = max([x for x in range(len(pattern)) if pattern[x] == ' '])
nxtR = None
for x in range(rMost, len(words)):
if x != rMost and words[x] == ' ':
nxtR = x
break
if len(pattern) < 39:
out.append(pattern)
return
if nxtR <=len(pattern) and nxtR:
out.append(pattern)
words = words[39:]
rec(words)
else:
out.append(pattern[:rMost])
words = words[rMost+1:]
rec(words)
Jan. 28, 2016