輸入說明
第一行輸入一個整數M, N, M是原始文章行數,N是編輯指令數
其後M行,每一行為原始句子,以空白隔開每個字
其後N行,每一行的第一個字串為編輯指令,隨後為對應的控制字串,以空白隔開
文章不會包含標點符號,第幾行、第幾個字都是從1開始算
編輯指令說明:
ADD_W_FRONT i n word # 在第i行中,第n個字前面加上word
ADD_W_AFTER i n word # 在第i行中,第n個字後面加上word
ADD_S_FRONT i sentence # 在第i行前面加入一句sentence
ADD_S_AFTER i sentence # 在第i行後面加入一句sentence
INSERT_FRONT key word # 在文章中所有key前面加上word
INSERT_AFTER key word # 在文章中所有key的後面加上word
DEL_W i n # 刪除第i行中,第n個字
DEL_L i # 刪除第i行 (請注意其他行的行數,可能被此指令影響)
REPLACE old new # 將文章中所有old替換為new (區分大小寫)
COUNT # 輸出目前文章總計字數
輸出說明
輸出編輯完成之文章
範例:
輸入:
5 6
Morning mom # 第1行
Morning dear # 第2行
What is for breakfast # 第3行
Here are your eggs and milk # 第4行
Looks good # 第5行
INSERT_FRONT Morning Good # 1 -> Good Morning mom 2 -> Good Morning dear
REPLACE Morning night # 1 -> Good night mom 2 -> Good night dear
DEL_L 1 # 刪除第一行,其他行數-1
DEL_W 1 3 # 刪除 dear 1 -> Good night
ADD_S_AFTER 4 thanks mom # 4 -> Looks good thanks mom
COUNT # 輸出當前總字數
輸出:
16
Good night
What is for breakfast
Here are your eggs and milk
Looks good thanks mom
輸入 1
4 5
Loving can hurt
Loving can hurt sometimes
It is the only thing makes us feel alive
we keep this love in a photograph
DEL_L 1
DEL_L 2
ADD_S_FRONT 2 Maybe
INSERT_FRONT hurt not
COUNT
輸出1
13
Loving can not hurt sometimes
Maybe we keep this love in a photograph
輸入 2
3 3
Once I was seven years old
my mama told me
mama love me
REPLACE mama daddy
INSERT_AFTER daddy and
INSERT_AFTER and mama
輸出 2
Once I was seven years old
my daddy and mama told me
daddy and mama love me
輸入3
3 5
Who lives in a pineapple in the sea
Absorbent yellow and porous is he
Nautical nonsense be something you wish
DEL_W 1 6
ADD_W_FRONT 3 1 Who
ADD_W_AFTER 1 5 under
REPLACE Who You
ADD_S_AFTER 3 it
輸出 3
You lives in a pineapple under the sea
Absorbent yellow and porous is he
You Nautical nonsense be something you wish it
輸入 4
1 1
you you you
INSERT_FRONT you are
輸出4
are you are you are you
# ADD_W_FRONT i n word
def ADD_W_FRONT(i: int, n: int, word: str, textList: list) -> list:
index = i - 1
tmpStr = textList[index]
tmpList = tmpStr.strip().split()
tmpList.insert(n - 1, word)
textList[index] = ' '.join(tmpList)
return textList
# ADD_W_AFTER i n word
def ADD_W_AFTER(i: int, n: int, word: str, textList: list) -> list:
index = i - 1
tmpStr = textList[index]
tmpList = tmpStr.strip().split()
tmpList.insert(n, word)
textList[index] = ' '.join(tmpList)
return textList
# ADD_S_FRONT i sentence
def ADD_S_FRONT(i: int, sentence: str, textList: list) -> list:
index = i - 1
tmpStr = textList[index]
textList[index] = sentence + ' ' + tmpStr
return textList
# ADD_S_AFTER i sentence
def ADD_S_AFTER(i: int, sentence: str, textList: list) -> list:
index = i - 1
tmpStr = textList[index]
textList[index] = tmpStr + ' ' + sentence
return textList
# INSERT_FRONT key word
def INSERT_FRONT(key: str, word: str, textList: list) -> list:
for i in range(len(textList)):
textList[i] = textList[i].replace(key, word + ' ' + key)
return textList
# INSERT_AFTER key word
def INSERT_AFTER(key: str, word: str, textList: list) -> list:
for i in range(len(textList)):
textList[i] = textList[i].replace(key, key + ' ' + word)
return textList
# DEL_W i n
def DEL_W(i: int, n: int, textList: list) -> list:
index = i - 1
tmpStr = textList[index]
tmpList = tmpStr.strip().split()
n -= 1
if 0 <= n < len(tmpList):
del tmpList[n]
textList[index] = ' '.join(tmpList)
return textList
# DEL_L i
def DEL_L(i: int, textList: list) -> list:
index = i - 1
if 0 <= index < len(textList):
del textList[index]
return textList
# REPLACE old new
def REPLACE(old: str, new: str, textList: list) -> list:
for i in range(len(textList)):
textList[i] = textList[i].replace(old, new)
return textList
def COUNT(textList: list) -> int:
n = 0
for i in range(len(textList)):
tmpStr = textList[i]
tmpList = tmpStr.strip().split()
n += len(tmpList)
return n
def command(commandStr: str, textList: list) -> list:
# ADD_W_FRONT i n word # 在第i行中,第n個字前面加上word
# ADD_W_AFTER i n word # 在第i行中,第n個字後面加上word
# ADD_S_FRONT i sentence # 在第i行前面加入一句sentence
# ADD_S_AFTER i sentence # 在第i行後面加入一句sentence
# INSERT_FRONT key word # 在文章中所有key前面加上word
# INSERT_AFTER key word # 在文章中所有key的後面加上word
# DEL_W i n # 刪除第i行中,第n個字
# DEL_L i # 刪除第i行 (請注意其他行的行數,可能被此指令影響)
# REPLACE old new # 將文章中所有old替換為new (區分大小寫)
# COUNT # 輸出目前文章總計字數
tmpList = commandStr.strip().split()
if len(tmpList) == 0:
return textList
elif tmpList[0] == 'ADD_W_FRONT':
i, n, word = eval(tmpList[1]), eval(tmpList[2]), tmpList[3]
textList = ADD_W_FRONT(i, n, word, textList)
elif tmpList[0] == 'ADD_W_AFTER':
i, n, word = eval(tmpList[1]), eval(tmpList[2]), tmpList[3]
textList = ADD_W_AFTER(i, n, word, textList)
elif tmpList[0] == 'ADD_S_FRONT':
i, sentence = eval(tmpList[1]), ' '.join(tmpList[2:])
textList = ADD_S_FRONT(i, sentence, textList)
elif tmpList[0] == 'ADD_S_AFTER':
i, sentence = eval(tmpList[1]), ' '.join(tmpList[2:])
textList = ADD_S_AFTER(i, sentence, textList)
elif tmpList[0] == 'INSERT_FRONT':
key, word = tmpList[1], tmpList[2]
textList = INSERT_FRONT(key, word, textList)
elif tmpList[0] == 'INSERT_AFTER':
key, word = tmpList[1], tmpList[2]
textList = INSERT_AFTER(key, word, textList)
elif tmpList[0] == 'DEL_W':
i, n = eval(tmpList[1]), eval(tmpList[2])
textList = DEL_W(i, n, textList)
elif tmpList[0] == 'DEL_L':
i = eval(tmpList[1])
textList = DEL_L(i, textList)
elif tmpList[0] == 'REPLACE':
old, new = tmpList[1], tmpList[2]
textList = REPLACE(old, new, textList)
return textList
def main():
m, n = map(int, input().split())
lst = []
for _ in range(m):
lst.append(input())
num = -1
for _ in range(n):
commandStr = input().strip()
if commandStr == 'COUNT':
num = COUNT(lst)
else:
lst = command(commandStr, lst)
if num != -1:
print(num)
for line in lst:
print(line)
if __name__ == '__main__':
main()
这是直接代写作业了??
import linecache as lc
# con:内容 i:行 n:字位置 ws:插入内容 flg_w_s:是字还是句子的标志 flg_f_a:是前还是后的标志
def _ADD(con, i, n, ws, flg_ws, flg_fa):
split_con = [i.split() for i in con]
if flg_ws == 'WORD':
if flg_fa == 'FRONT':
split_con[i-1].insert(n - 1, ws[0])
else:
split_con[i-1].insert(n, ws[0])
else:
if flg_fa == 'FRONT':
split_con[i-1] = ws + split_con[i-1]
else:
split_con[i-1].extend(ws)
return [' '.join(i) for i in split_con]
def _INSERT(con, key, word, flg_fa):
split_con = [i.split() for i in con]
for i,v in enumerate(split_con):
for j in range(len(v) - 1, -1, -1):
if v[j] == key:
if flg_fa == 'FRONT':
split_con[i].insert(j, word)
else:
split_con[i].insert(j + 1, word)
return [' '.join(i) for i in split_con]
def _DEL(con, i, n, flg_wl):
split_con = [i.split() for i in con]
if flg_wl == "WORD":
del split_con[i-1][n - 1]
else:
del split_con[i-1]
return [' '.join(i) for i in split_con]
def _REPLACE(con, old, new):
return [i.replace(old, new) for i in con]
def _COUNT(con):
return sum([len(i.split()) for i in con])
def GetCommand():
M, N = map(int, input(">>>").split())
for i in range(M + N):
tmp_con = input(">>>")
if i < M:
DICT_CONTENT['con'].append(tmp_con.strip())
else:
DICT_CONTENT['com'].append(tmp_con.strip())
# 文件测试
#ii = 1
#while True:
#line = lc.getline(dirname + "\\test.txt", ii)
#if line == "":
#break
#line = line.replace("\n", '')
#if ii == 1:
#M, N = map(int, line.split())
#else:
#if ii <= M + 1:
#DICT_CONTENT['con'].append(line.strip())
#else:
#DICT_CONTENT['com'].append(line.strip())
#ii += 1
DICT_CONTENT = {'con': [],'com': [],}
GetCommand()
CON = DICT_CONTENT['con']
cou = -1
for i in DICT_CONTENT['com']:
tmp_com = i.split()
if tmp_com[0] == 'ADD_W_FRONT':
CON = _ADD(CON, int(tmp_com[1]), int(tmp_com[2]), tmp_com[3:], 'WORD', 'FRONT')
elif tmp_com[0] == 'ADD_W_AFTER':
CON = _ADD(CON, int(tmp_com[1]), int(tmp_com[2]), tmp_com[3:], 'WORD', 'AFTER')
elif tmp_com[0] == 'ADD_S_FRONT':
CON = _ADD(CON, int(tmp_com[1]), -1, tmp_com[2:], 'SENTENCE', 'FRONT')
elif tmp_com[0] == 'ADD_S_AFTER':
CON = _ADD(CON, int(tmp_com[1]), -1, tmp_com[2:], 'SENTENCE', 'AFTER')
elif tmp_com[0] == 'INSERT_FRONT':
CON = _INSERT(CON, tmp_com[1], tmp_com[2], 'FRONT')
elif tmp_com[0] == 'INSERT_AFTER':
CON = _INSERT(CON, tmp_com[1], tmp_com[2], 'AFTER')
elif tmp_com[0] == 'DEL_W':
CON = _DEL(CON, int(tmp_com[1]), int(tmp_com[2]), 'WORD')
elif tmp_com[0] == 'DEL_L':
CON = _DEL(CON, int(tmp_com[1]), -1, 'SENTENCE')
elif tmp_com[0] == 'REPLACE':
CON = _REPLACE(CON, tmp_com[1], tmp_com[2])
elif tmp_com[0] == 'COUNT':
cou = _COUNT(CON)
if cou != -1:
print(cou)
print('\n'.join(CON))
text = []
order = []
def ADD_W_FRONT(i,n,word):
i = int(i)
n = int(n)
word_list = text[i-1].split(' ')
word_list.insert(n-1,word)
new = " ".join(word_list)
text[i-1] = new
return text
def ADD_W_AFTER(i,n,word):
i = int(i)
n = int(n)
word_list = text[i-1].split(' ')
word_list.insert(n,word)
new = " ".join(word_list)
text[i-1] = new
return text
def ADD_S_FRONT(i, sentence):
i = int(i)
text.insert(i-1,sentence)
return text
def ADD_S_AFTER(i, sentence):
i = int(i)
text.insert(i,sentence)
return text
def INSERT_FRONT(key, word):
for index1,m in enumerate(text):
word_list = m.split(' ')
for index2,n in enumerate(word_list):
if n == key:
word_list.insert(index2,word)
text[index1] = ' '.join(word_list)
return text
def INSERT_AFTER(key, word):
for index1,m in enumerate(text):
word_list = m.split(' ')
for index2,n in enumerate(word_list):
if n == key:
word_list.insert(index2+1,word)
text[index1] = ' '.join(word_list)
return text
def DEL_W(i,n):
i = int(i)
n = int(n)
word_list = text[i-1].split(' ')
word_list.pop(n-1)
text[i-1] = ' '.join(word_list)
return text
def DEL_L(i):
i = int(i)
text.pop(i-1)
return text
def REPLACE(old,new):
for index1,m in enumerate(text):
word_list = m.split(' ')
for index2,n in enumerate(word_list):
if n == old:
word_list[index2] = new
text[index1] = ' '.join(word_list)
return text
def Count():
all = []
for m in text:
word_list = m.split(' ')
for n in word_list:
all.append(n)
print(len(all))
num = input('输入数字:').split(' ')
row_num = int(num[0])
ins_num = int(num[1])
for i in range(row_num):
content = input('输入内容:')
text.append(content)
for i in range(ins_num):
instruction = input('输入指令:')
order.append(instruction)
for i in order:
order_list = i.split(' ')
if order_list[0] == 'ADD_W_FRONT':
result = ADD_W_FRONT(order_list[1],order_list[2],order_list[3])
elif order_list[0] == 'ADD_W_AFTER':
result = ADD_W_AFTER(order_list[1], order_list[2],order_list[3])
elif order_list[0] == 'ADD_S_FRONT':
result = ADD_S_FRONT(order_list[1],order_list[2])
elif order_list[0] == 'ADD_S_AFTER':
result = ADD_S_AFTER(order_list[1],order_list[2])
elif order_list[0] == 'INSERT_FRONT':
result = INSERT_FRONT(order_list[1],order_list[2])
elif order_list[0] == 'INSERT_AFTER':
result = INSERT_AFTER(order_list[1],order_list[2])
elif order_list[0] == 'DEL_W':
result = DEL_W(order_list[1],order_list[2])
elif order_list[0] == 'DEL_L':
result = DEL_L(order_list[1])
elif order_list[0] == 'REPLACE':
result = REPLACE(order_list[1],order_list[2])
else:
Count()
for i in result:
print(i)
结果3