bol_in 2022-01-07 21:39 采纳率: 64.6%
浏览 67
已结题

python-文字編輯器


輸入說明
第一行輸入一個整數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
  • 写回答

4条回答 默认 最新

  • 广大菜鸟 2022-01-08 11:04
    关注
    # 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()
    
    
    

    img

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

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月8日
  • 已采纳回答 1月8日
  • 创建了问题 1月7日

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面