bol_in 2022-01-10 21:38 采纳率: 64.6%
浏览 155
已结题

Python-文字編輯器

求該題程式碼


輸入說明
第一行輸入一個整數M, N, M是原始文章行數,N是編輯指令數。
其後M行,每一行為原始句子,以空白隔開每個字
其後N行,每一行的第一個字串為編輯指令,
隨後為對應的控制字串,以空白隔開:

Note:
文章不會包含標點符號,
第幾行、第幾個字都是從1開始算;
PASTE必會在COPY後使用,無需考慮直接PASTE的情形;
INSERT,REPLACE 請注意要插入/替換文章中所有符合的字

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 (多個word以空白隔開)
ADD_S_AFTER i sentence # 在第i行後面加入一句sentence (多個word以空白隔開)
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 (區分大小寫)
COPY_L i # 複製第i行
COPY i n # 複製第i行的第n個字
PASTE_FRONT i n # 貼上於第i行,第n個字前
PASTE_AFTER i n # 貼上於第i行,第n個字後
COUNT # 統計,並輸出文章總單字數

輸出說明
輸出編輯完成之文章

範例:

輸入:
5 10
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
COPY_L 1 # copy “Good night”
PASTE_AFTER 4 4 # 4-> Looks good thanks mom Good night
COPY 4 4 # copy “mom”
PASTE_FRONT 2 1 # 2 -> mom What is for breakfast
COUNT # 輸出文章單字數

輸出:
19
Good night
mom What is for breakfast
Here are your eggs and milk
Looks good thanks mom Good night
--------------
輸入 1
4 7
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
COPY 1 2
PASTE_AFTER 2 2
COUNT

輸出 1
14
Loving can not hurt sometimes
Maybe we can keep this love in a photograph
----------------
輸入 2
5 6
I have a
pen
apple
Ugh
apple
COPY_L 1
DEL_L 1
PASTE_FRONT 1 1
PASTE_FRONT 2 1
COPY 1 4
PASTE_AFTER 4 1

輸出 2
I have a pen
I have a apple
Ugh
apple pen
--------------------
輸入 3
5 15
Once I was seven years old
my mama told me
mama love me
my
and my
COPY_L 3
REPLACE mama daddy
INSERT_AFTER daddy and
INSERT_AFTER and mama
PASTE_AFTER 4 1
PASTE_AFTER 5 3
DEL_W 4 2
DEL_W 5 2
DEL_W 5 3
ADD_W_AFTER 4 1 sister
ADD_W_AFTER 5 2 grandma
ADD_W_AFTER 5 5 ha
COPY 5 6
PASTE_FRONT 5 6
PASTE_AFTER 5 6

輸入 3
Once I was seven years old
my daddy and mama told me
daddy and mama love me
my sister love me
and my grandma love me ha ha ha
-------------------
輸入 4
6 15
You lives in a pineapple in the sea
Who
Absorbent and yellow and porous is he
Spongebob
nautical nonsense be something
Squarepants
DEL_W 1 6
ADD_W_FRONT 5 1 If
ADD_W_AFTER 1 5 under
COPY 6 1
PASTE_AFTER 4 1
COPY 4 1
PASTE_FRONT 6 1
COPY 6 2
PASTE_AFTER 2 1
DEL_W 2 1
COPY 4 1
PASTE_FRONT 2 1
REPLACE You Who
ADD_S_AFTER 5 you wish
COUNT

輸出 4
28
Who lives in a pineapple under the sea
Spongebob Squarepants
Absorbent and yellow and porous is he
Spongebob Squarepants
If nautical nonsense be something you wish
Spongebob Squarepants


  • 写回答

5条回答 默认 最新

  • cvpytorch 2022-01-14 08:02
    关注
    text = []
    order = []
    def ADD_W(i,n,word,sub):
        #这里我把加单词操作整合到一起了
        i = int(i)
        n = int(n)
        word_list = text[i-1].split(' ')
        if sub == 'ADD_W_FRONT':
            word_list.insert(n-1,word)
        else:
            word_list.insert(n, word)
        new = " ".join(word_list)
        text[i-1] = new
        return text
    def ADD_S(i, sentence,sub):
        #这里我把加句子操作整合到一起了
        i = int(i)
        if sub == 'ADD_S_FRONT':
            text[i-1]=sentence+' '+text[i-1]
        else:
            text[i-1]=text[i-1]+' '+sentence
        return text
    def INSERT(key, word,sub):
        #这里我把插入操作整合到一起了
        for index1,m in enumerate(text):
            word_list = m.split(' ')
            for index2,n in enumerate(word_list):
                if n == key:
                    if sub == 'INSERT_FRONT':
                        word_list.insert(index2,word)
                    else:
                        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 COPY_L(i):
        i = int(i)
        return text[i-1]
    def COPY(i,n):
        i = int(i)
        n = int(n)
        content = text[i-1].split(' ')
        return content[n-1]
    def PASTE(i,n,word,sub):
        #这里我把粘贴操作整合到一起了
        i = int(i)
        n = int(n)
        content = text[i-1].split(' ')
        if sub == 'PASTE_FRONT':
            content.insert(n-1,word)
        else:
            content.insert(n,word)
        text[i-1] = ' '.join(content)
        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)
    copy_list = []
    #存上一次拷贝命令
    copy_result = []
    #存上一次拷贝的东西
    for i in order:
        order_list = i.split(' ')
        if order_list[0][:5] == 'ADD_W':
            result = ADD_W(order_list[1],order_list[2],order_list[3],order_list[0])
        elif order_list[0][:5] == 'ADD_S':
            result = ADD_S(order_list[1],i[14:],order_list[0])
        elif order_list[0][0] == 'I':
            result = INSERT(order_list[1],order_list[2],order_list[0])
        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])
        elif order_list[0] == 'COPY_L':
            st = COPY_L(order_list[1])
            copy_list.append(order_list[0]+order_list[1])
            copy_result.append(st)
        elif order_list[0] == 'COPY':
            st = COPY(order_list[1],order_list[2])
            copy_list.append(order_list[0]+order_list[1]+order_list[2])
            copy_result.append(st)
        elif order_list[0][0] == 'P':
            if copy_list[-1][-2] == 'L':
                result = PASTE(order_list[1],order_list[2],copy_result[-1],order_list[0])
            elif copy_list[-1][-3] == 'Y':
                s = int(copy_list[-1][-2])
                t = int(copy_list[-1][-1])
                result = PASTE(order_list[1],order_list[2],copy_result[-1],order_list[0])
        else:
            Count()
    for i in result:
        print(i)
    

    这次的绝对没有问题了,完全符合你的要求,没有错误

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 1月25日
  • 已采纳回答 1月17日
  • 创建了问题 1月10日

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址