Codewars: Strip Comments

一道4kyu的题。 思路就是 先分行,对每行挨着去注释,然后rstrip,join一下,属于基础题。


Complete the solution so that it strips all text that follows any of a set of comment markers passed in. Any whitespace at the end of the line should also be stripped out.

Example:

Given an input string of:

apples, pears # and bananas
grapes
bananas !apples
The output expected would be:

apples, pears
grapes
bananas
The code would be called like so:

result = solution(“apples, pears # and bananas\ngrapes\nbananas !apples”, [“#”, “!”])
result should == “apples, pears\ngrapes\nbananas”


from functools import reduce
def solution(code,lst):
    return '\n'.join([reduce(lambda m,n:m.split(n)[0],lst,line).strip() for line in code.split('\n') ])

猜你喜欢

转载自blog.csdn.net/qq_35279914/article/details/82220049
今日推荐