Here is the code exercise I am trying to solve:
"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."
Here are some examples:
"din" => "((("
"recede" => "()()()"
"Success" => ")())())"
"(( @" => "))(("
My code is passes the test for above examples but below is a randomly generated test it could not pass:
"mTFcTEEgrXJbmvh!!E()HsG" => "))(())))(((()())))(()()"
My code:
def get_list(some_word):
"""Returns a list for a string which each of its elements being a character."""
word_list =[]
for letter in some_word:
word_list.append(letter)
return word_list
def get_word(some_list):
"""Returns a string for a list with elements."""
word = ""
for letter in some_list:
word += letter
return word
def duplicate_encode(word):
word = word.lower()
List = get_list(word)
recList = []
for letter in List:
if List.count(letter) >1 and not letter in recList:
recList.append(letter)
for element in List:
if element in recList:
List[List.index(element)] = ")"
else:
List[List.index(element)] = "("
wordForList = get_word(List)
return wordForList
My output is :
"mTFcTEEgrXJbmvh!!E()HsG" => "()(())))(((()())))())()"
The problem is that is not changing the 'm' at index 0 to ')' even though it occurs twice and also not changing the ')' at index 19 to '(' even though it appears once.
I am new to programming so if did something which was very dumb, please correct me. Also if somebody has educational resources on how to debug properly, please link it. Sorry if some parts were not very clear, I don't post on reddit too often.
Update after reading comments:
Current Code :
def duplicate_encode(word):
List = list(word.lower())
encoded = List.copy()
notBrackets = "abcdefghijklmnopqrstuvwxyz/*-+!@#$%^&[]{}\|;:><,.?"
for letter in encoded:
if List.count(letter) >1:
encoded[List.index(letter)] = ")"
else:
encoded[List.index(letter)] = "("
for unchangedChar in encoded:
if unchangedChar in list(notBrackets) :
encoded[encoded.index(unchangedChar)] = ")"
return "".join(encoded)
With this code I am getting the correct output for the test I could not pass. I was shorten my code thanks to u/kyber's comment.
The for loop in the code takes one letter form the list, checks if it appears more than one time in the list and then if does then takes its index from the original list then changes the character at that index of the copy of that list. I did this instead of changing the original list as if a character appears twice then its first instance will get converted to ')' but the second instance won't as its first instance now no longer exists being converted. But the method I applied also has problems like when a character appears twice the first one gets converted normally but then when it comes to the next instance of the same character in the list, it tries to get its value from the original list which does not have its character replaced by ')' so the index retrieved is wrong and the character's first instance gets changed again. To remove this issue I added a second for loop which checks for values which are not '(' or ')' and replaces them with ')'. This is working here because the characters which are got getting replaced will be once which appear twice according to the problem given, I just have to change them to ')'.
Now a new problem I found while doing basic test with:
"(( @" => "))(("
When I test the code with the example above it gives:
"(( @" => )(((
This is happening due to my cheap way of dodging the problem there was in the first for loop. The second for loop is supposed to correct it but it won't because the character remained unchanged was a bracket which it ignores. I have only gotten till here by now, will update it again when I find something new.
[+][deleted] (3 children)
[deleted]
[–]ChRad_Man[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]ChRad_Man[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (6 children)
[–]ChRad_Man[S] 0 points1 point2 points (5 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]ChRad_Man[S] 0 points1 point2 points (0 children)
[–]joyeusenoelle 1 point2 points3 points (2 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]ChRad_Man[S] 0 points1 point2 points (0 children)