0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Codewars 8 kyu Exclamation marks series #6: Remove n exclamation marks in the sentence from left to right

Posted at

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)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?