+ 1
String to backwards alphabet
str=input() str=str.lower() talf=“a,b,c,d,e,f,g,h,I,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z ”.split(‘,’) rtalf=talf[::-1] for I in range(len(str)): char=str[i] idx = talf.index(char) hide = str.replace(str[i],rtalf[idx]] print(hide) But when I’m sample input like : Pirate Sample output is : Piratv It’s like string input effectively change but char by char it’s reset to the start string input so I’ve only the last char who is convert to backwards alphabet
15 ответов
+ 1
1) You're not using a new empty string to populate it with each iteration's output.
So then you have to add space in your list, at both ends, so that you can replace it with itself just by using the same formula, it seems to me.
2)If you used an empty string and just added new character, then an if statement would do it, one as to reverse only if the char is in the alphabet and then add the character inside the loop but outside of if statement so that the space could get added.
Here are 3 solutions in this:
https://code.sololearn.com/ckWfdFR9k5m8/?ref=app
These are just to work with. Lothar'd said slices consume more time, what you see there is just indexing the same alphabet from the end to beginning (as opposed to reversing it and then indexing)
+ 2
O'neal MBOULA PENDA There are a lot of characters the code should not change. Instead of trying to deal with spaces in a special way, you could just change the letters, and copy everything else as is.
+ 1
After removing some other error in code,
Use this: and finally print str
str = str.replace(str[i],rtalf[idx])
#instead hide = str.replace(str[i],rtalf[idx])
+ 1
O'neal MBOULA PENDA
As you said you got only wrong output so you are aware of other errors mismatch brace ] ), using wrong quotes for string..
Returns new so store in back on str so you work on same but new modified string in next iteration....
+ 1
Your logic don't affect white space. I think its correct to don't affect it. Put as it is.
+ 1
Edited the comment in the last part of the code because x.remove() always returns nothing. It changes the list in place. So any assignment is useless. My wording could be misleading, as if it was doable elsewhere.)
Also, about your other question:
https://code.sololearn.com/c4MluWEfWq1r/?ref=app
+ 1
O'neal MBOULA PENDA By all means, pls rename variable "str" - a class you will need has this exact name.
Also, in the for loop, you define variable "I", but use variable "i". Make them equal.
BTW, instead of splitting with commas, you can just use list("abcdef...").
+ 1
O'neal MBOULA PENDA
Add your approach to skip space..
index.of method return error when there is no element in list, for which you finding..
So talf.index(' ') result error.
And replace function replaces all occurences of string to replace for example "abab" Will first replaced as "zbzb" (a by z) then "zyzy" (b by y) , then "ayay" ( z by a) , last "abab"(y by b) ..
Ex: "azaz" => "zzzz" => "aaaa" => "zzzz" => "aaaa" Final string after replace.
Use count value to replace the limit for replaces.. As
str = str.replace( str[i], rtalf[inx] , 1) #will replace only single character, not all occurences..
May be late, if not solved then , hope it helps..
0
I just have to change this ?
Of which error are you talking about ?
.replace() method does it update string or return a newer
0
I’m coming to test your proposition and its work verry good hello return svloo
But how can i manage if input has whitespeces character
0
Unfortunaly when i’m enterring whitespace program gives an value error : “ ” is not in list
0
I’ve even try to add this :
if char == ‘ ’:
continue
for ignore ‘ ‘ character but this time second world does not converted well
0
For example when im sample input :
Hello world
Sample output :
Svloo woild
svloo result is correct but the other not
0
Jayakrisna , thx let me try … but if i add :
Space character in a both of two list it’s will work too?
0
You can but it depends on how you add, and use those.. Try add at end then see.. But better is to don't touch other except alphabets..