首页 > 解决方案 > 如何使用基于伪随机数的字母来编码给定的字符串,使用列表、字符串方法、字典?

问题描述

字母表:

Letter - `a, b, c, d, ... z`.  
Number - `0, 1, 2, 3, ... 25`.

编码表:

Number - `0, 1, 2, 3, ...25`.  
Code (from random seed 202090) - `23, 15, 1, 22, ...` (same length as letters above).

使用表 1 和表 2,任何字母都可以编码如下:

  1. 在表 1 中查找字母的编号(例如,“m”对应于数字 12)。

  2. 查找表 2 中的代码(例如,数字 12 对应于代码 10)。

  3. 在表 1 中查找代码以获得编码字母(例如,代码 10 对应于字母“k”)。

  4. 将编码字母转换为大写(例如,编码字母“k”转换为“K”)。

这是我正在努力处理的实际编码消息/文件内容的第二部分。

不允许导入标准随机模块以外的模块。

必须使用字符串方法、列表和字典。 1

import random

def main():
    user = int(input('Enter a number: '))
    random.seed(user)
    message = input('Enter message: ') 
    create_code_list(user)
    encode(user,message)

def create_code_list(user):
    letter = 'abcdefghijklmnopqrstuvwxyz'
    code_list = (random.sample(range(0,26),len(letter)))
    print (code_list)
    return code_list

def encode(code_list,message):
    letter = 'abcdefghijklmnopqrstuvwxyz'
    letter_list = list(letter)
    code_list = create_code_list(letter_list)
    string_list = list(string)
    c = {}
    for i in range(len(letter_list)):
        c[string_list[i]] = letter[i]
    
main()

标签: pythonlistdictionaryencoding

解决方案


好的,你想对给定的单词进行编码和解码吗?请参阅我在 git-hub 中发布的 repo:Simple-Cryptography。代码可能很长。请参阅此代码:

#An inbuilt module which contains all the printables
import string

letters = [] #A list where all the printable letters will be added.
numbers = [] #A list which contains the equal number of elements with letters


for letter in string.printable:
    letters.append(letter)  #Append the letter to the list named letters

for number in range(0, len(letters)):
    numbers.append(number)  #Append the number in the list named numbers


#Create a dict that contains all the keys as letters and the values as numbers.
encode_dict = dict(zip(letters, numbers))

#Create a dict that contains all the keys as numbers and the values as letters.
decode_dict = dict(zip(numbers, letters))

#A function that encodes the given key:
def encode():

    #Get the input from user:
    text_to_encode = str(input('The Text To Encode: '))

    #A string which will contain all the decoded numbers:
    encoded_text = ''

    #Append the values for the keys as the letter in the inputted text:
    for letter in text_to_encode:
        encoded_text = encoded_text + str(encode_dict[letter]) + " "   #Include The " "(white space) to make it decodable! !important

    #Print the encoded text
    print(encoded_text)

def decode():

    #Get the input from user:
    text_to_decode = str(input('The Text To Decode: '))

    #a string that contains all the decoded letter:
    decoded_text = ''

    #Append the values for the keys as the letter in the inputted text:
    for letter in text_to_decode.split(): #Remove the white_space to identify the letter .split() !important
        
        decoded_text = decoded_text + str(decode_dict[int(letter)]) #Convert the letter to 'int' because the keys are int !important

    print(decoded_text)

#The end of the code!

当我运行这个:

>>> encode()
The Text To Encode: stackoverflow
28 29 10 12 20 24 31 14 27 15 21 24 32 
>>> decode()
The Text To Decode: 28 29 10 12 20 24 31 14 27 15 21 24 32 
stackoverflow
>>> 

在此您可以输入键盘上的任何字母或数字!


推荐阅读