Codewars 8 kyu Exclamation marks series #6: Remove n exclamation marks in the sentence from left to right
https://www.codewars.com/kata/57faf7275c991027af000679/python
Task
Remove n exclamation marks in the sentence from left to right. n is positive integer.
Verbalization
If i is !, revolve ! n times (use replace(“!”, “”, n)
I assumed that I need Put vacant list (m = “”), Roop st (For i in st), Add i in vacant list but they were not needed :->
Only I needed to know is replace method can assign the number to replace.
Code
def remove(st, n):
return st.replace("!","", n)
Reference
Other solution that I initially tried.
def remove(s, n):
liste=[]
k=1
for i in range(len(s)):
if s[i]=="!" and k<=n:
k+=1
else:
liste.append(s[i])
return "".join(liste)