Just finished an assignment in Python where one part was to write a function to find the correct rotation in a encryption similar to a Caesar Cipher..
As mentioned it's already finished and works as intended. The course is an introduction to Python, but I do have experience with other languages since previously.
I was hoping someone could have a look at my findRot to see where I possibly could have done anything that isn't 'Python-esque' or what potential bottlenecks might occur.
def findRot(word1, word2, iterator): letterFreq = [("A", 65), ("E", 69), ("I", 73), ("O", 79), ("T", 84), ("U", 85)] rot = ord(word1) - letterFreq[iterator] letters = [chr(ord(letter) - rot) if (ord(letter) - rot >= 65) else chr(90 - (65 -(ord(word2) - rot + 1))) for letter in word2] with open('words.txt', 'r') as words: lines = words.readlines() lines = list(map(lambda s: s.upper().strip(), lines)) if ''.join(letters) in lines: print(''.join(letters) + ", " + str(True)) return rot else: findRot(word1, word2, iterator + 1)
The assignment specified the usage of letter frequency, so I just took some of the more common letters found in English and put them in a list of tuples for readability purposes.
In the professor's use-case the first word would be 'R' and the second would be 'Uxen'. So in this case the rotation would supply the 'I' and 'Love'.
Again, this function just runs a primary check to find the rotation to apply to the rest of the 'encrypted' text. So a validation of the decrypted text will take place later.
>>> findRot('R', 'UXEN', 0) 9
Because it takes 9 rotations to get to the clear text