So, I am making a code/project that counts the occurrence of a character (repeated in a row) then reset the counter to 1 if the next character doesn't equal to the current character, and each character has a number next to it, which is the "counter" of the curremt character...
It works as expected, but now I want the program to remove the counter if the counter is 1(for example, hello would return hel2o).. But instead, it removes all the occurrence of "1" in the string:
t
ext = "aaabbbabcccc"+ " " #I added space in case it does not get the last character of the string!
counter = 0
n = ""
decom = ""
for x in range(0,len(text)):
if text[x] == text[x-1]:
counter+=1;
else:
counter=1
decom+=f"{text[x]}{str(counter)}"if x+1!=len(text) and text[x]!=text[x+1] else ""
if counter==1:
n = decom.replace(str(counter),"")
print("With 1 not removed:")
print(decom.strip()) #This one calculates more accurate than the one with 1 removed
print("With 1 removed:")
print(n.strip()) #This one removes all the ones in the code..
Is there a way to remove the counter if the counter is equal to 1, instead of removing all the occurrences of "1" in the string(ex: if h is repeated 14 times in a row, it returns h4 but I was expecting to see h14)?
I like to solve these types of problems using a regex replacement approach:
import re
def repl(m):
return m.group()[0] + str(len(m.group()))
inp = "hello world"
output = re.sub(r'([a-z])\1+', repl, inp, flags=re.I)
print(output) # hel2o world
The regex pattern ([a-z])\1+
matches any single letter (and captures it in \1
), followed by that same character one or more times. We replace such matches by the letter followed by the number of occurrences.