Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is for python. (Question 2, will post others separately.) # 2. Implement ge

ID: 3799224 • Letter: T

Question

This is for python.

(Question 2, will post others separately.)

# 2. Implement gen_key(password)
def gen_key(password):
    """Generate a key from a password (as outlined in chapter 2 of our
    book):
    1. remove duplicates from the password
    2. split the alphabet into two parts by using the last letter of the
    password (with duplicates removed)
    3. remove any letters in both parts of the alphabet that appear in the
    password
    4. construct the key by joining the password with the first and second
    part of the alphabet

    gen_key('topsecret') # --> 'topsecruvwxyzabdfghijklmnq'

    :param password: the password used to generate a key
    :type password: str
    :return: a string 26 characters long, consisting of all letters from a - z
    :rtype: str
    """
    # implement this function! (need a hint; check out the book)
    return ''


def sub_encrypt(password, message):
    """Encrypt a message using the substitution cipher. If a
    character is not in the key, the character remains unchanged.
    :param password: the password used to generate a key
    :type password: str
    :param message: the message to be encrypted
    :type message: str
    :return: the resulting ciphertext as a string
    :rtype: str
    """
    key = gen_key(password)
    alpha = gen_consecutive_chars()
    ciphertext = ''
    for ch in message:
        try:
            ciphertext += key[alpha.index(ch)]
        except ValueError:
            ciphertext += ch
    return ciphertext

I don't understand how to implement this function. (Nor the others I'm going to post.) Can I get a walkthrough on how to do these? (A code + breakdown of why or just comments on it would be awesome.)

Explanation / Answer

I have written your function. Feel free to ask any doubt.

from collections import OrderedDict
def gen_key(password):
unique_pwd= "".join(OrderedDict.fromkeys(password))
unique_alpha="".join(OrderedDict.fromkeys(alphabet))
part1,part2=unique_alpha.split(unique_pwd[-1])
part1_new=''
part2_new=''
for ch in part1:
if ch not in unique_pwd:
part1_new+=ch
for ch in part2:
if ch not in unique_pwd:
part2_new+=ch
  
key=part1_new+part2_new
return key

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote