'str.replace(last_letter, second_letter) and replace(second_letter, last_letter) resulting in only one of them executing
In the following Python code where I am deciphering a message that should theoretically result in Ready, set go output the two lines with str.replace() are obviously not executing as expected, resulting in only the second character of the word being replaced:
# Input:
# 82yade 115te 103o
#
# Output:
# Ready set go
user_input = input().split(" ")
first_letter = ''
second_letter = ''
last_letter = ''
printable_list = []
ord_char = ''
final_str = ''
concat_str = ''
for word in user_input:
final_str = ''
first_letter = ''
last_letter = ''
ord_char = ''
concat_str = ''
for idx in range(0, len(word)):
if word[idx].isdigit():
first_letter = word[idx]
ord_char += first_letter
else:
final_str += word[idx]
concat_str = chr(int(ord_char)) + final_str
second_letter = concat_str[1]
last_letter = concat_str[len(concat_str) - 1]
print(second_letter, last_letter) # this prints here only for debugging purposes, e.g. to check 2nd and last letter
concat_str = concat_str.replace(last_letter, second_letter)
concat_str = concat_str.replace(second_letter, last_letter)
printable_list.append(concat_str)
print(' '.join(printable_list))
Hence, what I expect to come out as "Ready set go" is "Reade see go". What is causing the second replace to malfunction?
What I tried to achieve is that the second and the last letter are switched (e.g., Holle means Hello) and that the first letter is replaced by its character code (e.g., 72 means H) which runs smoothly until I hit the line replacing the 2nd with my last character of each of the words I add to the printable_list.
Solution 1:[1]
Consider the string:
Ryade
If you replace the last_letter (e) with the second_letter (y) you get:
Ryady
Now if you replace second_letter (y) with last_letter (e) you get:
Reade
All of the y's were replaced with e's.
To get around this issue you can introduce a placeholder character (as one of many options):
placeholder = '^'
concat_str = concat_str.replace(last_letter, placeholder)
concat_str = concat_str.replace(second_letter, last_letter)
concat_str = concat_str.replace(placeholder, second_letter)
With this logic our input of Ryade will take the following steps:
Ryed^Read^Ready
From my comment below, you may have better success being more tactical about which character you are replacing by using its position in the word (after swapping out that first letter). There are likely prettier ways to do this, but this feels like a reasonable direction:
import re
user_input = input().split(" ")
printable_list = []
for word in user_input:
word = word.replace(re.findall('^[0-9]*', word)[0], chr(int(re.findall('^[0-9]*', word)[0])))
if len(word) > 2:
word = word[0] + word[len(word) - 1] + word[2:len(word)-1] + word[1]
printable_list.append(word)
print(' '.join(printable_list))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
