Reputation: 47
I have been given the following string 'abcdea'
and I need to find the repeated character but remove the first one so the result most be 'bcdea'
I have tried to following but only get this result
def remove_rep(x):
new_list = []
for i in x:
if i not in new_list:
new_list.append(i)
new_list = ''.join(new_list)
print(new_list)
remove_rep('abcdea')
and the result is 'abcde'
not the one that I was looking 'bcdea'
Upvotes: 2
Views: 469
Reputation: 1856
One of the approaches with one small change in the if
condition:
def remove_rep(x):
new_list = []
visited = []
for i, item in enumerate(x):
if item not in x[i+1:] or item in visited:
new_list.append(item)
else:
visited.append(item)
new_list = ''.join(new_list)
print(new_list)
remove_rep('abcdeaa')
remove_rep('abcdeaabcdea')
Output:
bcdeaa
aabcdea
Upvotes: 1
Reputation: 11943
str.replace()
does that :
https://docs.python.org/3/library/stdtypes.html#str.replace
str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
So basically :
"abcabc".replace('b', '', 1)
# output : 'acabc'
Upvotes: 0
Reputation: 11612
One approach can be to iterate in reverse order over the string, and keep track of all the characters seen in the string. If a character is repeated, we don't add it to the new_list
.
def remove_rep(x: str):
new_list = []
seen = set()
for char in reversed(x):
if char not in seen:
new_list.append(char)
seen.add(char)
return ''.join(reversed(new_list))
print(remove_rep('abcdea'))
Result: 'bcdea'
Note that the above solution doesn't exactly work as desired, as it'll remove all occurrences of a character except the last one; for example, if you have 2+ occurrences of a chracter and you only want to remove the first one. To resolve that, you can instead do something like below:
def remove_rep(x: str):
new_list = []
first_seen = set()
for char in x:
freq = x.count(char)
if char in first_seen or freq == 1:
new_list.append(char)
elif freq > 1:
first_seen.add(char)
return ''.join(new_list)
Now for the given input:
print(remove_rep('abcdeaca'))
We get the desired result - only the first a
and c
is removed:
bdeaca
Test for a more complicated input:
print(remove_rep('abcdeaabcdea'))
We do get the correct result:
aabcdea
Do you see what happened in that last one? The first abcde
sequence got removed, as all characters are repeated in this string. So our result is actually correct, even though it doesn't look so at an initial glance.
Upvotes: 1
Reputation: 18106
You could make use of str.find()
, which returns the first occurrence with the string:
def remove_rep(oldString):
newString = ''
for i in oldString:
if i in newString:
# Character used previously, .find() returns the first position within string
first_position_index = newString.find(i)
newString = newString[:first_position_index] + newString[
first_position_index + 1:]
newString += i
print(newString)
remove_rep('abcdea')
remove_rep('abcdeaabcdea')
Out:
bcdea
bcdea
Upvotes: 1
Reputation: 2044
Change
new_list = ''.join(new_list)
to
new_list = ''.join(new_list[1:]+[i])
(and figure out why! Hint: what's the condition of your if
block? What are you checking for and why?)
Upvotes: -1