Alternate Answer to Off-Platform Project: Coded Correspondence

Link to project

I am not nessesary asking for any help, but for a question with many people commenting that it is hard, I did extra and even made everything into 1 function, it is a bit different from the standard answer, so I’d thought I would share my solution (and hopefully this helps some people)

def shift_l(letter, count, mode = 0): #shift index = alphabet.find(letter) if index == -1: return letter else: if mode == 0: shifted_index = int((index + count)%len(alphabet)) elif mode == 1: shifted_index = int((index - count)%len(alphabet)) decoded_letter = alphabet[shifted_index] return decoded_letter def shift_w(word, count, mode = 0): decoded_word = "" for w in word: decoded_word += shift_l(w, count, mode) return decoded_word def shift(sentence, count, mode = 0): decoded_sentence = [] word_list = sentence.split() for w in word_list: decoded_sentence.append(shift_w(w,count,mode)) return " ".join(decoded_sentence) def shift_vig_w(word, key, mode = 0): decoded_word = "" m = (mode+1)%2 for i in range(len(word)): decoded_word += shift_l(word[i], alphabet.find(key[i]), m) return decoded_word def shift_vig(sentence, key, mode = 0): decoded_sentence = [] keyword_phrase_list = [] key_count = 0 key_count_looped = key_count%len(key) for m in sentence: if m in alphabet: keyword_phrase_list.append(key[key_count_looped]) key_count += 1 key_count_looped = key_count%len(key) elif m == " ": keyword_phrase_list.append(" ") else: keyword_phrase_list.append("a") keyword_phrase = "".join(keyword_phrase_list) word_list = sentence.split() keyword_phrase_list = keyword_phrase.split() for i in range(len(word_list)): decoded_sentence.append(shift_vig_w(word_list[i],keyword_phrase_list[i],mode)) return " ".join(decoded_sentence)

Ceasar: shift(message, offset, mode (1 = encrypt, 0 = decrypt))
Vigenère: shift_vig(message, key, mode (1 = encrypt, 0 = decrypt))

I was struggling to preserve the information of the punctuations of Vigenère, and ended up getting around this by also assigning letters to characters in the encrypted messages that are not alphabets and then negating those non-alphabet characters in the conversion process.

I think another way it could be done is to split the message into a list alphabet words and punctuations, select the alphabet words, convert them and place them back into the list, and join them back together and this way might be more efficient?

idk anymore I literally spent 2 hrs on this

1 Like