+ 1
What's wrong with my code??
def duplicate_encode(word): word = word.lower() for j in word: n = word.count(j) if n==1: word = word.replace(j,"(") else: word = word.replace(j,")") return word The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears only once in the original string, or ")" if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate. Examples "din" => "(((" "recede" => "()()()" "Success" => ")())())" "(( @" => "))(("
5 Answers
+ 1
Usually it is not a good idea to manipulate the thing you are looping through. You are looping through word but you are also doing replaces on word.
Instead use a result variable
result=""
and in your if-else statement append "(" or ")" to the result variable
+ 1
def duplicate_encode(word):
r = ""
word = word.lower()
for j in word:
n = word.count(j)
if n==1:
r = r+"("
else:
r = r+")"
return r
it works well đđđ
+ 1
Great!
0
No it's not working properly for "lccyL(CCcUE@XvvFCnJ hUp!JR(rSFQ)"
0
Could you share the edited code and your output vs expected output?