티스토리 뷰
The computer program cowsay generates ASCII pictures of a cow with a message.
+----------------------------+| Moo may represent an idea, || but only the cow knows. |+----------------------------+ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
The program initially started more or less as an inside joke within hacker culture, but has been around long enough that its use has become rather widespread.
Assignment
Your task is to write a program that can be used to generate ASCII pictures of a cow with a message that is displayed with a certain width. The text fragment should be cleaned up, split across multiple lines, centered and framed within a speech bubble.
Strictly follow these instructions to format the speech bubble.
Clean the given text by removing leading and trailing spaces and reducing consecutive spaces into a single space.
If the text is shorter than the maximal allowed width, the speech bubble should be made as wide as the text itself (see second example below).
If the text is longer than the maximal allowed width, the text should be split across multiple lines. Each line should contain the maximum number of remaining words that fits the maximal allowed width. The words of the given text are separated by spaces. Punctuation marks and other special characters belong to the word to which they are attached.
The text fragment that fits a single line should be centered across the width of the speech bubble (according to the rules that are implemented by the string method center).
Put additional dashes, plus symbols, vertical bars and spaces around the text as illustrated in the examples.
Your submitted solution will be compared character by character to the correct solution, so it is crucial to put all characters at exactly the right position.
Input
A line of text followed by a line containing the maximal allowed width of the speech bubble. You may assume that the longest word in the text fragment is not longer than the maximal allowed width .
Output
The given text formatted in a speech bubble having width , according to procedure outlined in the introduction. A fixed ASCII image of the cow should immediately follow the speech bubble.
Example
Input:
Moo may represent an idea, but only the cow knows.26
Output:
+----------------------------+| Moo may represent an idea, || but only the cow knows. |+----------------------------+ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Example
Input:
Moo may represent an idea, but only the cow knows.1000
Output:
+----------------------------------------------------+| Moo may represent an idea, but only the cow knows. |+----------------------------------------------------+ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Example
Input:
Moo may represent an idea, but only the cow knows.9
Output:
+-----------+| Moo may || represent || an idea, || but only || the cow || knows. |+-----------+ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
latest
def check_vowel(char):
VOWELS = ['a', 'e', 'i', 'o', 'u']
if char.lower() in VOWELS:
return True
return False
def is_p(chars):
index_token = int(len(chars) / 2)
cond_token = chars[index_token] == 'p'
for i in range(index_token):
cond_vowel = chars[i].lower() == chars[i + index_token + 1].lower()
if cond_token and cond_vowel:
return True
return False
def get_chars_trans(chars, start, length):
chars_trans = list()
stop = start + length + 1
for i in range(start, stop):
chars_trans.append(chars[i])
return chars_trans
def get_indexes_remove(chars, current, length):
indexes_remove = list()
cond_p = is_p(get_chars_trans(chars, current, length)) if current + length < len(chars) else False
if cond_p:
half = int(length / 2)
index_p = current + half
stop = current + length + 1
for i in range(index_p, stop):
indexes_remove.append(i)
return indexes_remove
def extract_chars(chars, indexes):
english = ""
for i, char in enumerate(chars):
if not i in indexes:
english += char
return english
def p_to_eng(chars):
indexes_remove = list()
for i, char in enumerate(chars):
if check_vowel(char):
cond_vowel_double = i + 1 < len(chars) and check_vowel(chars[i + 1])
indexes_remove.extend(get_indexes_remove(chars, i, 4 if cond_vowel_double else 2))
print(extract_chars(chars, indexes_remove))
p_to_eng(str(input()))
v. 0.3
IMAGE_COW = " \ ^__^" \
+ "\n \ (oo)\_______" \
+ "\n (__)\ )\/\\" \
+ "\n ||----w |" \
+ "\n || ||"
def msg_to_words(msg):
words = list()
word = ""
for char in msg:
if not char == " ":
word += char
else:
words.append(word)
word = ""
words.append(word)
return words
def get_outline(msg, width):
outline = "+-"
stop = width if len(msg) > width else len(msg)
for _ in range(stop):
outline += "-"
outline += "-+"
return outline
def get_line(words, start, stop, width, len_msg):
body = ""
cnt_loop = 0
for i in range(start, stop):
prev = " " if cnt_loop > 0 else ""
body += prev + words[i]
cnt_loop += 1
body = body.center(width if len_msg > width else len_msg, " ")
return "| " + body + " |\n"
def get_index_end(words, start, width):
len_line = 0
cur = start
cnt_loop = 0
while cur < len(words):
len_line += len(words[cur]) + (1 if cnt_loop > 0 else 0)
if len_line > width:
return cur
else:
cur += 1
cnt_loop += 1
return len(words)
def get_body(msg, width):
body = ""
words = msg_to_words(msg)
len_msg = len(msg)
start = 0
stop = 0
while stop < len(words):
stop = get_index_end(words, start, width)
body += get_line(words, start, stop, width, len_msg)
start = stop
return body
def set_text(msg, width):
outline = get_outline(msg, width)
body = get_body(msg, width)
result = outline + "\n" + body + outline
return result
def show(msg, width):
msg = " ".join(msg.split())
msg = msg.strip()
result = set_text(msg, width) + "\n" + IMAGE_COW
print(result)
show(str(input()), int(input()))
'python lecture > algorism' 카테고리의 다른 글
[edu] Penney Ante (0) | 2018.10.06 |
---|---|
[edu] Corkscrew (0) | 2018.10.04 |
[edu] Wepe speapeak p (0) | 2018.10.02 |
[edu] Pythagorean triples (0) | 2018.10.02 |
[edu] Monkeys and coconuts (0) | 2018.10.02 |
- Total
- Today
- Yesterday
- pycrypto
- 이미지 비교
- 장고 카톡 자동응답
- 장고 플러스친구 자동응답
- PuTTYGen
- wsgi
- django chatbot
- 엑셀 비교
- Python
- 모바일 스킨 적용
- 문서 비교
- 플러스친구 자동응답
- 파이썬 강좌
- gitignore
- virtualenv
- 파이썬 입문
- 면접답변
- Tistory
- admin.py
- 파이썬 독학
- gitlab
- 문과 코딩
- 파이썬
- chatbot
- GIT
- 장고
- 면접정답
- 파이썬 프로그래밍
- 모바일 테마 적용
- django
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |