Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for What Does the Cow Say? by wo.tomasz
import re
COW = r'''
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
'''
def dev_text(text):
text = re.sub('[ ]+', ' ', text)
lines = []
while len(text) > 39:
line = text[0:40]
n = line.rfind(' ')
if n == -1:
n = 39
lines.append(line[0:n])
text = text[n:].strip()
else:
lines.append(text)
return lines
def formating_lines(lines, max_line_size):
if len(lines) == 1:
lines[0] = '< ' + lines[0] + ' >'
elif len(lines) > 1:
lines = ['| ' + x + ' ' * (max_line_size-len(x)) + ' |' for x in lines]
lines[0] = lines[0].replace('|','/', 1).replace('|','\\', -1)
lines[-1] = lines[-1].replace('|','\\', 1).replace('|','/', -1)
return lines
def cowsay(text):
lines = dev_text(text)
max_line_size = max([len(x) for x in lines])
top = " " + "_" * (max_line_size + 2)
bottom = " " + "-" * (max_line_size + 2)
lines = formating_lines(lines, max_line_size)
output = '\n' + top + '\n' + '\n'.join(lines) + '\n' + bottom + COW
return output
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
expected_cowsay_one_line = r'''
________________
< Checkio rulezz >
----------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
'''
expected_cowsay_two_lines = r'''
________________________________________
/ A \
\ longtextwithonlyonespacetofittwolines. /
----------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
'''
expected_cowsay_many_lines = r'''
_________________________________________
/ Lorem ipsum dolor sit amet, consectetur \
| adipisicing elit, sed do eiusmod tempor |
| incididunt ut labore et dolore magna |
\ aliqua. /
-----------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
'''
cowsay_one_line = cowsay('Checkio rulezz')
assert cowsay_one_line == expected_cowsay_one_line, 'Wrong answer:\n%s' % cowsay_one_line
cowsay_two_lines = cowsay('A longtextwithonlyonespacetofittwolines.')
assert cowsay_two_lines == expected_cowsay_two_lines, 'Wrong answer:\n%s' % cowsay_two_lines
cowsay_many_lines = cowsay('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do '
'eiusmod tempor incididunt ut labore et dolore magna aliqua.')
assert cowsay_many_lines == expected_cowsay_many_lines, 'Wrong answer:\n%s' % cowsay_many_lines
Jan. 9, 2021