간단하지 않습니다 substitution cipher? 방금 "도움"의 4 글자를 앞으로 옮기고 나머지 글자를 모두 오른쪽으로 옮겼습니다. 여기에 편집
람다, itertools 및 별표 (*) 인수 및 시간을 낭비 (아마도 파이썬에서 암호를 코딩에 흥미있는 토론을 할 수있는 것을 구출)에있는 운동으로 파이썬 구현입니다 :
import string
from itertools import izip, count, starmap
def cipher(s,key):
# characters you want to translate, e.g.
# 'abcd ... xyz '
raw = string.ascii_lowercase + ' '
# cipher with your key, e.g.
# 'helpabcdfgi...z '
sub = key + string.translate(raw, None, key)
# create a dictionary from a character to an index
# in the original raw value string
m = dict(izip(raw, count()))
# looks up the index in the map using: starmap(m.get, s)
# then gets the substitution character: map(lambda i:sub[i], ...)
# and joins them together
return ''.join(map(lambda i:sub[i], starmap(m.get, s)))
그리고 그것을 확인하기 위해 몇 가지 테스트 코드가 작동합니다
ins = 'this is a test'
outs = cipher(ins, "help")
print ins,' -> ',outs
exp = "tdfs fs h tast"
if exp == outs:
print "pass :)"
else:
print "~~ FAIL ~~", " expected ", exp
출력 :
D:\temp>cipher.py
this is a test -> tdfs fs h tast
pass :)
이 질문은 것으로 보인다 오프 주제는 암호화에 관한 것입니다 및 프로그래밍 문제가 포함되어 있지 않기 때문에. –
이름이 아직 지정되지 않은 경우 "Lucypher"라고 제목을 지정합니다. –
카이사르 암호의 변형이지만 카이사 암호는 이와 같은 키를 사용하지 않습니다. – Chris