【每日一题0916】去掉注释

:woman_mage:给定一个文本字符串words和注释符号列表markers,请编写函数,将其中注释标记后的文本都去除,包括字符串末尾的空格。
示例:

输入:("apples, pears # and bananas", ["#"])
输出:"apples, pears"

输入:"bananas !apples"
输出:"bananas"

题目难度:中等
题目来源:codewars

def strip_comments(words: str, markers: list) -> str:
    pass

assert strip_comments("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"]) == "apples, pears\ngrapes\nbananas"
assert strip_comments("a #b\nc\nd $e f g", ["#", "$"]) == "a\nc\nd"

题目中的两个断言是对的。。。其他用例还不知道

import re
def strip_comments(words,markers:list):
    for i in markers:
        if words.find(i)!=-1:
            li1=re.findall('\\'+i+'.*',words)
            for j in li1:
                words=words.replace(j,'')
        else:
            continue
    return words
def strip_comments(words,markers):
    line = words.split('\n')
    temp_list=[]
    for i in markers:
        for j in line:
            temp_list.append(j.split(i)[0].rstrip())
        line=temp_list
        result_list = temp_list
        temp_list=[]
    return '\n'.join(result_list)

刚开始没有意识到 \n 是换行符,稍微过一下才回过神。

def strip_comments(words: str, markers: list) -> str:
    result = []
    words_list = words.split('\n')
    for words in words_list:
        for m in markers:
            words = words.split(m)[0]
        result.append(words.rstrip())
    return '\n'.join(result)

assert strip_comments("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"]) == "apples, pears\ngrapes\nbananas"
assert strip_comments("a #b\nc\nd $e f g", ["#", "$"]) == "a\nc\nd"
def strip_comments(words: str, markers: list) -> str:
    lines=words.split('\n')
    list=[]
    for i in markers:
        for j in lines:
            list.append(j.split(f'{i}')[0].rstrip())
        lines=list
        list=[]
    result_list=lines
    result='\n'.join(result_list)
    return result

assert strip_comments("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"]) == "apples, pears\ngrapes\nbananas"
assert strip_comments("a #b\nc\nd $e f g", ["#", "$"]) == "a\nc\nd"

def strip_comments(words: str, markers: list) → str:
wordsL = words.split("\n")
wordF =

for item in wordsL:
    for marker in markers:
        item = item[:item.find(marker)].rstrip() if item.find(marker) >0 else item
    wordF.append(item)
return '\n'.join(wordF)

assert strip_comments(“apples, pears # and bananas\ngrapes\nbananas !apples”, ["#", “!”]) == “apples, pears\ngrapes\nbananas”
assert strip_comments(“a #b\nc\nd $e f g”, ["#", “$”]) == “a\nc\nd”

def strip_comments(words: str, markers: list) -> str:
    words_list = words.split('\n')
    for marker in markers:
        for word in words_list:
            if marker in word:
                words_list[words_list.index(word)] = word.split(marker)[0]
    return '\n'.join([word.rstrip() for word in words_list])


assert strip_comments("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"]) == "apples, pears\ngrapes\nbananas"
assert strip_comments("a #b\nc\nd $e f g", ["#", "$"]) == "a\nc\nd"
关闭