Skip to content
This repository was archived by the owner on Mar 14, 2022. It is now read-only.

[Homework 4] 文句處理 #377

@organicguava

Description

@organicguava

提交連結

https://judge.ccclub.io/status/2b593c4ac73637a70714769ae23640ab

程式碼

keyword = input().split()   #關鍵字串列       
input_lst = []              #文句串列            
while True:
    data = input()
    if data == "end":
        break
    else:
        input_lst.append(data)
inspect=0
#找出每一句裡的關鍵字,並輸出小串列work,裡面包含關鍵字、關鍵字起始index、關鍵字結束index,再把小串列append到index_lst
for i in input_lst:
    index_input_lst = []
    j = 0
    while j <= len(keyword)-1:
        k = 0
        while k <= len(i)-1:
            if i.find(keyword[j],k) == -1:  #使用string.find 尋找關鍵字第一個字的index。若沒有關鍵字結果為-1
                break
            else: 
                m = i.find(keyword[j],k)+len(keyword[j])-1
                work = [keyword[j],i.find(keyword[j],k),m]
                index_input_lst.append(work)
                inspect=1 #若沒有關鍵字,inspect為False
            k = m+1   #從關鍵字的下一個字再開始找
        j+=1
   
    
    if inspect:
        index_input_lst.sort(key = lambda x : (x[1],-x[2]))  #關鍵字index串列,依照開頭文字的index由小到大排、若開頭文字的index一樣,再按照結束文字的index由大到小排
        
        
        need_input_lst = [index_input_lst[0]]        #實際上需要被匡起來的關鍵字index串列              
        p = 1
        while p <= len(index_input_lst)-1:
            if index_input_lst[p][1] > index_input_lst[p-1][2]:
                need_input_lst.append(index_input_lst[p])
            p+=1
     
        j = 0
        sentence = ""   #欲輸出的文句                              
        sentence += i[:need_input_lst[0][1]]  #先加入第一個關鍵字(不含)之前的內容               
        while j<=len(need_input_lst)-2:    #前後都有關鍵字的內容,逐步替換(每次替換會包含前面的關鍵字,不含後面的關鍵字)                  
            part = i[need_input_lst[j][1]:need_input_lst[j+1][1]]
            new_part = part.replace(need_input_lst[j][0],"「" + need_input_lst[j][0] + "」")
            sentence += new_part
            j+=1
        final_part = i[need_input_lst[-1][1]:]     #最後一個關鍵字(含)之後的內容         
        new_final_part = final_part.replace(need_input_lst[-1][0],"「" + need_input_lst[-1][0] + "」") 
        sentence += new_final_part    
    
        print(sentence)
        

if not inspect:
    for i in input_lst:
        print(i)

錯誤訊息

runtime error

問題描述

測資與助教提供的
that a good
that was a very good example
都有正確的output

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions