Day 2:Remove n exclamation marks in the sentence

Details:
Remove n exclamation marks in the sentence from left to right. n is positive integer.

remove("Hi!",1) === "Hi"
remove("Hi!",100) === "Hi"
remove("Hi!!!",1) === "Hi!!"
remove("Hi!!!",100) === "Hi"
remove("!Hi",1) === "Hi"
remove("!Hi!",1) === "Hi!"
remove("!Hi!",100) === "Hi"
remove("!!!Hi !!hi!!! !hi",1) === "!!Hi !!hi!!! !hi"
remove("!!!Hi !!hi!!! !hi",3) === "Hi !!hi!!! !hi"
remove("!!!Hi !!hi!!! !hi",5) === "Hi hi!!! !hi"
remove("!!!Hi !!hi!!! !hi",100) === "Hi hi hi"
My Solution:
def remove(s, n):
    l = []
    for i in s:
        if i == "!" and n > 0:
            n -= 1
            continue
        l.append(i)
    return ''.join(l)
Best Practice:
def remove(s, n):
    return s.replace('!', '', n)
Tips:
1. 替换字符串中指定数量的单个字符用str.replace('target', 'replace', n)。
    原文作者:天野
    原文地址: https://segmentfault.com/a/1190000014642745
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞