簡體   English   中英

如何使用基於偽隨機數的字母來編碼給定的字符串,使用列表、字符串方法、字典?

[英]How to use letters based on pseudo-random numbers to encode a given string, using lists, string methods, dictionaries?

字母表:

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()

好的,你想對給定的單詞進行編碼和解碼嗎? 請參閱我在 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
>>> 

在此您可以輸入鍵盤上的任何字母或數字!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM