+ 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" => ")())())" "(( @" => "))(("

25th Sep 2021, 6:51 AM
Shahir
Shahir - avatar
5 odpowiedzi
+ 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
25th Sep 2021, 7:06 AM
John Doe
+ 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 👍👍👍
25th Sep 2021, 7:25 AM
Shahir
Shahir - avatar
+ 1
Great!
25th Sep 2021, 7:26 AM
John Doe
0
No it's not working properly for "lccyL(CCcUE@XvvFCnJ hUp!JR(rSFQ)"
25th Sep 2021, 7:06 AM
Shahir
Shahir - avatar
0
Could you share the edited code and your output vs expected output?
25th Sep 2021, 7:23 AM
John Doe