MIT公开课 Python Assignment4

References:


4A Permutations of a string

实验分析
本实验输入一个字符串,返回其所有可能的排列方式。

思路分析
利用递归算法,完成示例程序中的get_permutations()函数。

核心代码

def get_permutations(sequence):
    cnt = 0
    length = len(sequence)
    res = []
    swap(res, sequence, cnt, length)
    # res = [list(i) for i in res]
    print(res)

    
def swap(l, seq, cnt, length):
    tmp = ""
    if cnt == length:
            for i in range(length):
                tmp += seq[i]
            l.append(tmp)
    else:
        for i in range(cnt, length):
            tp_l = list(seq)
            temp = tp_l[cnt]
            tp_l[cnt] = tp_l[i]
            tp_l[i] = temp
            seq = "".join(tp_l)
            swap(l, seq, cnt+1, length)
            tp_l = list(seq)
            temp = tp_l[cnt]
            tp_l[cnt] = tp_l[i]
            tp_l[i] = temp
            seq = "".join(tp_l)

4B Cipher Like Caesar

实验分析
本实验输入一个“已加密“的字符串,返回其最佳”解密“结果。

思路分析
利用字符串处理以及遍历匹配即可。

核心代码

def build_shift_dict(self, shift):
    dict_low = dict(zip(string.ascii_lowercase,\
                        string.ascii_lowercase[shift:] +\
                        string.ascii_lowercase[:shift]))
    dict_high = dict(zip(string.ascii_uppercase,\
                         string.ascii_uppercase[shift:] +\
                         string.ascii_uppercase[:shift]))
    dict_high.update(dict_low)
    return dict_high


def apply_shift(self, shift):
    res = ""
    dict_s = self.build_shift_dict(shift)
    for i in range(0, len(self.message_text)):
        cur = ord(self.message_text[i])
        if 97 <= cur <= 122:
            res += dict_s[chr(cur)]
        elif 65 <= cur <= 90:
            res += dict_s[chr(cur)]
        else:
            res += self.message_text[i]
            continue
    return res


def decrypt_message(self):
    best_shift = 0
    cur_match = 0
    decrypted = ""
    for i in range(0, 26):
        l_str = self.apply_shift(i).split()
        tmp = 0
        for j in l_str:
            if is_word(self.valid_words, j):
                tmp += 1
        if tmp > cur_match:
            cur_match = tmp
            decrypted = self.apply_shift(i)
            best_shift = i
        else:
            continue
    tp = (best_shift, decrypted)
    return tp

4C Cipher Like Caesar

实验分析
本实验输入一个“元音替换“的字符串,返回其最佳”还原“结果。

思路分析
利用4A中处理组合的函数,结合字符串处理以及遍历匹配即可。

核心代码

def build_transpose_dict(self, vowels_permutation):
    dict_map = dict(zip(('a', 'e', 'i', 'o', 'u'), vowels_permutation))
    return dict_map

def apply_transpose(self, transpose_dict):
    tmp = list(self.message_text)
    for i in range(len(tmp)):
        if tmp[i] in ('a', 'e', 'i', 'o', 'u'):
            tmp[i] = transpose_dict[tmp[i]]
	return "".join(tmp)

def decrypt_message(self):
    permu = get_permutations("aeiou")
    best_permu = []
    cur_match = 0
    decrypted = ""
    for i in range(len(permu)):
        trans_dict = self.build_transpose_dict(permu[i])
        l_str = self.apply_transpose(trans_dict).split()
        tmp = 0
        for j in l_str:
            if is_word(self.valid_words, j):
                tmp += 1
        if tmp > cur_match:
            cur_match = tmp
            decrypted = self.apply_transpose(trans_dict)
            best_permu = permu[i]
        else:
            continue
    tp = (("".join(best_permu)), decrypted)
    return tp

// SZP 2019 / 5 / 7

发布了30 篇原创文章 · 获赞 7 · 访问量 4435

猜你喜欢

转载自blog.csdn.net/weixin_40005329/article/details/89929005