-5

リバース メソッドの修正を手伝ってください... (テキストから数値へ、次にリバース バック、数値からテキスト) 以下は未編集の完全なコードです。

chars=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","t","s","u","v","w","x","y","z","!","@","#","$","%","^","&","*","(",")","<",">","?","/","[","]","{","}","-","_","=","+"," "];
numbs1=["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49"];
rechars1=dict(zip(chars,numbs1));
rechars2=dict(zip(numbs1,chars)); # ??
stringa=raw_input(""); #Letters and symbols only-->encrypt
stringb=''.join(rechars1.get(c,c) for c in stringa);
stringc=''.join(rechars2.get(c,c) for c in stringb); # ??
print "Plain:     ",stringa;
print "Encoded:   ",stringb;
print "Unencoded: ",stringc; #same as stringb??
4

2 に答える 2

2

すべての入力文字が変換されている (数字やその他の文字が含まれていない) ことがわかっている場合は、次のように逆にすることができます。

decipher = dict(zip(numbs1,chars)) 
stringc = ''.join(decipher[stringb[i:i+2]] for i in range(0, len(stringb), 2));
print "Decoded: ", stringc

ただし、単一の入力文字が翻訳されていない場合、このように文字のペアを取得しても機能しません (元の文字の各ペアは 1 つに並ばないため)。

于 2013-03-08T23:41:43.390 に答える
1

障害となるのは、 「20051920」などの文字列を 2 桁のリストに分割する方法です。そのハドルを乗り越えたら、rechars2を使用して元に戻すことができます。

私のソリューションでは、正規表現を使用しています。

import re
stringb = '20051920'
print re.findall(r'\d\d', stringb) # ['20', '05', '19', '20']
于 2013-03-09T00:13:01.337 に答える