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 Kurush
# migrated from python 2.7
COW = r'''
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
'''
def cowsay(text):
word_list = text.split()
if len(word_list) > 0:
if text[0] == " ":
word_list[0] = " " + word_list[0]
if text[-1] == " ":
word_list[-1] += " "
new_word_list = []
for word in word_list:
if len(word) < 40:
new_word_list.append(word)
else:
while (len(word) >= 40):
new_word_list.append(word[:39])
word = word[39:]
new_word_list.append(word)
word_list = new_word_list
text_list = [""]
i = 0
for word in word_list:
if text_list[i] == "": max_text_len = len(word)
else: max_text_len = len(text_list[i]) + 1 + len(word)
if (max_text_len > 39):
i += 1
text_list.append(word)
else:
if text_list[i] != "":
text_list[i] += " " + word
else:
text_list[i] = word
text_list = [word.ljust(len(max(text_list, key=len))) for word in text_list]
for word_number, word in enumerate(text_list):
if len(text_list) != 1:
if word_number == 0: text_list[word_number] = "/ " + text_list[word_number] + " \\"
elif word_number == len(text_list) - 1: text_list[word_number] = "\\ " + text_list[word_number] + " /"
else: text_list[word_number] = "| " + text_list[word_number] + " |"
else:
text_list[0] = text_list[word_number] = "< " + text_list[0] + " >"
filling_len = len(max(text_list, key=len)) - 2
text_list.insert(0, " " + "_" * filling_len)
text_list.append(" " + "-" * filling_len)
text = "\n".join(text_list)
return "\n" + text + COW
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
June 29, 2014