Hi Everyone,
I’m 41% through the Data and Programming Foundations for AI course. I’m struggling with the Vigenere Cipher exercise in Coded Correspondence. I’ve needed a lot of help from Grok and ChatGPT but even with that although the code runs, it outputs gobbledegook.
Any thoughts appreciated!
Thanks
Simon
here’s the gobbledegook output:
“you fugr doqd lc pmoyei ucgt? nice jaig! mgi mzq yrxphgod ulser the umcqip zy qhxpjujofgu!”
Here’s the code:
Setup
coded_message = “txm srom vkda gl lzlgzr qpdb? fepb ejac! ubr imn tapludwy mhfbz cza ruxzal wg zztcgcexxch!”
keyword = “friends”
alphabet = “abcdefghijklmnopqrstuvwxyz”
Step 1: Generate keyword_phrase
keyword_phrase = ‘’
for i in range(len(coded_message)):
keyword_phrase += keyword[i % len(keyword)]
Step 2: Get offsets
offsets =
for i in range(len(coded_message)):
char = coded_message[i]
key_char = keyword_phrase[i]
if char in alphabet:
offsets.append(alphabet.index(key_char))
else:
offsets.append(" ")
Step 3: Decode (add offsets)
decoded = ‘’
for i in range(len(coded_message)):
char = coded_message[i]
if char in alphabet:
char_pos = alphabet.index(char)
offset = offsets[i]
new_pos = (char_pos + offset) % 26 # Add for this cipher
decoded += alphabet[new_pos]
else:
decoded += char
Result: offsets is the list of offsets, decoded is the plaintext
For checking, you can print:
print(offsets) # [1, 4, 5, None, …]
print(decoded) # The decoded message