Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

python - creating the reversal portion of a cryptography decoder

First of hopefully many questions to come as I dive deeper into Python. I'm following along to the Giraffe Academy course and have tried to create a more complex program. It's a silly little decoder tool but it's helping me learn a lot of the python basics. I have the first part of the program below:

import string

std = string.ascii_lowercase
rvs = string.ascii_lowercase[::-1]
vwls = {'a':5,'e':1,'i':4, 'o':2, 'u':3}

def translate(phrase):
    phrase = phrase.lower()
    translation = ""
    for letter in phrase:
        if letter.isspace() == True:
            translation = translation + " "
        elif letter in ".!?":
            translation = translation + letter
        elif letter in vwls:
            if str(vwls[letter]) not in translation:
                translation = translation + str(vwls[letter])
                vwls[letter] = vwls[letter] + 5
        else:
            indx = std.index(letter)
            translation = translation + rvs[indx]
    return translation

print(translate(input("Enter a phrase: ")))

My goal is to create the decoder for the output of the previous code. The good news is I know what is causing my problem. Because the message is a string, any vowel that goes past 2 digits wont be picked up by my code below. I need to find a way to include the next character in the string so that it can choose the correct key/value pairing in the dict. Below is what I have so far:

import string

std = string.ascii_lowercase
rvs = string.ascii_lowercase[::-1]
vwls = {'5':'a','1':'e','4':'i','2':'o','3':'u'}

def translate(phrase):
    translation = ""
    for letter in phrase:
        if letter.isspace() == True:
            translation = translation + " "
        elif letter in ".!?":
            translation = translation + letter
        elif letter in vwls:
            if letter in vwls[letter]:
                translation = translation + vwls[letter]
                vwls[str(int(letter) + 5)] = vwls[letter]
                del vwls[letter]
            else:
                for index in range(len(phrase)-1):
                    if phrase[index] == phrase[index+1]:
                        translation = translation + 'poop'
        else:
            indx = rvs.index(letter)
            translation = translation + std[indx]

    return translation

print(translate(input("Enter a phrase: ")))

Any feedback is greatly appreciated! I have tried searching for similar issues and I am still learning the rules for stack overflow so I apologize if I have broken any rules regarding this question.

question from:https://stackoverflow.com/questions/65546411/creating-the-reversal-portion-of-a-cryptography-decoder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

good luck with your diving

tried here something, not sure if its correct

few points

  1. for each vowel, the %5 is the same, for example u can be 3,8,13,18.. they all have % 5 of 3

  2. the biggest match you can find is the correct one, 23 is either ou or just u after few iteration, so if the dict say u value is 23 the correct value is u

  3. there is a problem with the translate function you provided eeee is decoded to 1611 which is eee, missing one e

def decode(phrase):
    vwls = {'a':5,'e':1,'i':4, 'o':2, 'u':3}
    reverse_vwls = {0:'a',1:'e',4:'i',2:'o',3:'u'}
    translation = ""

    i=0
    while i < len(phrase):
        if phrase[i].isspace() == True:
            translation = translation + " "
            i+=1
            continue
        if phrase[i] in ".!?":
            translation = translation + phrase[i]
            i+=1
            continue
        if str.isdigit(phrase[i]): #check if the letter is a digit
            tmp_index = i
            str_num = ""
            
            while tmp_index<len(phrase) and str.isdigit(phrase[tmp_index]): # take all the co
                str_num = str_num+phrase[tmp_index]
                tmp_index+=1
            
            int_num = int(str_num) # turn the digits string into int
            while int_num:
                current_digit = int_num%10 
                curent_vwl = reverse_vwls[current_digit%5] #the ones digit % 5 is permenent f
                if vwls[curent_vwl] == int_num:
                    vwls[curent_vwl] = vwls[curent_vwl]+5
                    translation = translation+curent_vwl
                    i = i+len(str(int_num))
                    break
                int_num = int(int_num/10)
            continue

        indx = rvs.index(phrase[i])
        translation = translation + std[indx]
        i+=1
    return translation


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...