Skip to content
Sign upLog in
← Back to Community

Double Letter Problem

Profile icon
Alex0204

So, I already made a post about a day ago asking for help making all double letters have an "x" in between them. Some nice guy helped me out a lot. The only problem is: I need to make a change to the code he gave me, and don't I know how.

The code he gave me turns every instance of this "hello" into this "helxlo" and "hheelloooo" into "hxhexelxloxoxoxo" which is what I wanted at the time.

Now I only want to change every set of double letters starting with an even number. For example "hello" becomes "helxlo", "abbcc" remains "abbcc" but "abbacc" becomes "abbacxc" because the first letter of the second set of double letters is even (fourth index).

I would really really appreciate the help of a smart person out there to give me a couple minutes of their day and take a look at my code. The code which I am working on is at the bottom, not in the comments.

Voters
Profile icon
Spacecraft
Profile icon
Alex0204
Comments
hotnewtop
Profile icon
Spacecraft

Hi again

@Alex0204
, I just put this on your first post, but then I saw this thread.
Here you go- Added one more IF statement if (i % 2) != 0:

#Inputting the message message = input("Enter the message: ") #Checking double letters messageModified = "" char = [] for letter in message: char.append(letter) temp = char for i in reversed(range(1,len(char))): if (i % 2) != 0: if char[i] == char[i-1]: temp.insert(i,'x') for i in range(len(temp)): messageModified += temp[i] message = messageModified print(message)
Profile icon
Spacecraft

The solution you came up with seems to work; it's just skipping every other letter. It seems possible that you might run into issues with certain words using this method, depending on where the doubles land. Kudos on finding a solution - Best of luck!

Profile icon
PattanAhmed

@Alex0204
Hi,
I just checked out your Repl.
It's working as you said!

abbacc is turning into abbacxc
and
hello is turning into helxlo

Thanks!
Hope this helps

Profile icon
Alex0204

@PattanAhmed
Yeah. I actually realized the solution on my own shortly after I made this post. I just change line 211 from:

for i in reversed(range(1,len(char))):

to:

for i in reversed(range(1,len(char), 2)):

Thanks anyway bro!

Profile icon
PattanAhmed

@Alex0204
Welcome!
I am so happy that it is solved :)
If you have any more questions or problems, don't forget to post on Repl.it Ask board
Thanks!

Profile icon
PattanAhmed

@Alex0204

Happy to help you anyways!
Please mark my answer if that helps

:)