First off, I'm new to python and generally inexperienced with programming in general.
My problem is that I cannot convert the entire sentence in one swoop. I tried a for loop, but that just ended up in a whole sentence in a's and b's. Is there any efficient way to convert each letter without creating a whole new list displaying the sentence?
I tried a for loop and using a : to show all values in the list, but the for loop kept converting letters that were already converted and the : gave me errors saying I can't use a whole list as my key for the dictionary.
sentence = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
#sentence before conversion
letters = ['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']
new_letters = {}
#dictionary that makes all conversions (basically go 2 letters down the alphabet, a=c, b=d)
for i in range(0, 24):
new_letters.update({(letters[i]): (letters[i+2])})
i+=1
#creates dictionary
new_letters.update({'y':'a', 'z':'b'})
#had to add in y and z because I cannot go 2 letters down from y or z
new_sentence = sentence.replace(letters[], new_letters[letters[]])
#trying to replace certain letters with other letters, using the new_letters dictionary
print(new_letters)
print(new_sentence)
And yes, this is from http://www.pythonchallenge.com/index.php :D
So, basically you want to replace every character in a string with that character's equivalent in the alphabet rotated two places back (so 'a' becomes 'c', 'b' becomes 'd' and so on).
There are a few ways to do that. As you have noticed, you can't use a naive for loop because you'll convert characters that have already been converted once. Fortunately, Python strings have a translate method, which takes a dict that maps characters to each other. Therefore, you can just do this:
from string import ascii_lowercase, ascii_uppercase
rotated_lowercase = ascii_lowercase[2:] + ascii_lowercase[:2]
rotated_uppercase = ascii_uppercase[2:] + ascii_uppercase[:2]
translation_table = str.maketrans(ascii_lowercase + ascii_uppercase, rotated_lowercase + rotated_uppercase)
print('This is a translated string.'.translate(translation_table))
Output:
Vjku ku c vtcpuncvgf uvtkpi.
ascii_lowercase is an attribute in the string module that contains the entire lowercase alphabet, so you don't have to type it out manually.
rotated_alphabet takes the third character onward of the alphabet and adds the first two characters to the end, so you get 'cde...zab'.
We can then put the two together with maketrans, such that a maps to c, b to d and so on, just as we want.
Finally, it suffices to call the translate method on any string that we want to convert, passing in the translation table (note that the full stop is untouched).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With