python作业 求助!

有一段英文文本(这样我们不用考虑中文文本导致的对齐问题),该文本有多行,每行前后可能会有空格,每行前面的空格要保留,而每行最后的空格应该去除。请在这段英文文本上面加上装饰... 有一段英文文本(这样我们不用考虑中文文本导致的对齐问题),该文本有多行,每行前后可能会有空格,每行前面的空格要保留,而每行最后的空格应该去除。请在这段英文文本上面加上装饰栏,如下所示。 text = r''' The King and Queen of Hearts were seated on their throne when they arrived,with a great crowd assembled about them--all sorts of little birds and beasts, as well as the whole pack of cards: the Knave was standing before them, in chains, with a soldier on each side to guard him; and near the King was the White Rabbit, with a trumpet in one hand, and a scroll of parchment in the other. In the very middle of the court was a table, with a large dish of tartsupon it: they looked so good, that it made Alice quite hungry to look at them-- `I wish they'd get the trial done,' she thought, `and hand round the refreshments!' But there seemed to be no chance of this, so she began looking at everything about her, to pass away the time.''' 展开
 我来答
匿名用户
2018-05-20
展开全部
# -*- coding: utf-8 -*-

TEXT = r''' The King and Queen of Hearts were seated on their throne when they arrived, with a great crowd assembled about them--all sorts of little birds and beasts, as well as the whole pack of cards: the Knave was standing before them, in chains, with a soldier on each side to guard him; and near the King was the White Rabbit, with a trumpet in one hand, and a scroll of parchment in the other. In the very middle of the court was a table, with a large dish of tartsupon it: they looked so good, that it made Alice quite hungry to look at them-- `I wish they'd get the trial done,' she thought, `and hand round the refreshments!' But there seemed to be no chance of this, so she began looking at everything about her, to pass away the time.'''

class BorderAdder():
CORNER = "+"
ROW = "-"
COLUMN = "|"
BLANK = " "
ENDLINE = "\n"

def __init__(self, text, letter_per_line=80):
self.text = text
self.letter_per_line = letter_per_line
self.letter_list = []
self.current = 0
self.total = 0
self.current_word = 0

def add_border_line(self):
self.letter_list.append(BorderAdder.CORNER)
for i in range(self.letter_per_line):
self.letter_list.append(BorderAdder.ROW)
self.letter_list.append(BorderAdder.CORNER)
self.letter_list.append(BorderAdder.ENDLINE)
return self

def add_body_line(self, line):
self.letter_list.append(BorderAdder.COLUMN)
for letter in line:
self.letter_list.append(letter)
for i in range(self.letter_per_line - len(line)):
self.letter_list.append(BorderAdder.BLANK)
self.letter_list.append(BorderAdder.COLUMN)
self.letter_list.append(BorderAdder.ENDLINE)

def add_text(self):
self.add_border_line()
for i, t in enumerate(self.text):
if t in [" "]:
if (self.total + self.current_word) < self.letter_per_line:
if (self.total + self.current_word + 1) == self.letter_per_line:
self.add_body_line(self.text[self.current: self.current + self.total + self.current_word])
self.current = self.current + self.total + self.current_word + 1
self.total = 0
else:
self.total += (self.current_word + 1)
else:
self.add_body_line(self.text[self.current: self.current + self.total])
self.current = self.current + self.total
self.total = (self.current_word + 1)
self.current_word = 0
else:
self.current_word += 1
if self.total > 0:
self.add_body_line(self.text[self.current: self.current + self.total + self.current_word])
self.add_border_line()
return self

def show(self):
print "".join(self.letter_list)

BorderAdder(TEXT).add_text().show()
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式