Answer to Question #306626 in Python for reyadh

Question #306626

Given a string, create a new string with all the consecutive duplicates removed.




Hint: You may make a new string to store the result. You can check whether the current character and the next character are the same, then add that character to the new string.




Note: Only consecutive letters are removed not all duplicate occurences of a letter. You may try doing this alternative i.e. removing all duplicate letters from a given string, for your own practice.




Sample Input 1:



AAABBBBCDDBBECE




Sample Output 1:


ABCDBECE




Sample Input 2:



Jupyter Notebook is better. Case sensitivity check AAaaaAaaAAAa.




Sample Output 2:



Jupyter Notebok is beter. Case sensitivity check AaAaAa.

1
Expert's answer
2022-03-05T17:14:03-0500
print('Enter string: ')
str = input()
rezstr = ""
i = 0
while i < len(str):
    rezstr = rezstr + str[i]
    i += 1
    if i == len(str):
        break    
    while (str[i-1] == str[i]) & (i < len(str)):
        i += 1
        if i == len(str):
            break
print('Output:')
print(rezstr)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS