2012-04-01
展开全部
给你写了一个.
def convert(c, key, start = 'a', n = 26):
a = ord(start)
offset = ((ord(c) - a + key)%n)
return chr(a + offset)
def caesarEncode(s, key):
o = ""
for c in s:
if c.islower():
o+= convert(c, key, 'a')
elif c.isupper():
o+= convert(c, key, 'A')
else:
o+= c
return o
def caesarDecode(s, key):
return caesarEncode(s, -key)
if __name__ == '__main__':
key = 3
s = 'Hello world!'
e = caesarEncode(s, key)
d = caesarDecode(e, key)
print e
print d
运行结果:
Khoor zruog!
Hello world!
def convert(c, key, start = 'a', n = 26):
a = ord(start)
offset = ((ord(c) - a + key)%n)
return chr(a + offset)
def caesarEncode(s, key):
o = ""
for c in s:
if c.islower():
o+= convert(c, key, 'a')
elif c.isupper():
o+= convert(c, key, 'A')
else:
o+= c
return o
def caesarDecode(s, key):
return caesarEncode(s, -key)
if __name__ == '__main__':
key = 3
s = 'Hello world!'
e = caesarEncode(s, key)
d = caesarDecode(e, key)
print e
print d
运行结果:
Khoor zruog!
Hello world!
展开全部
for x in range(0,len(C)):
#输入大写字母
if ord(C[x])<=ord("碰腔Z") and ord(C[x])-3>=ord("塌吵弊A"):
print chr(ord(C[x])-3),
elif ord(C[x])-3<团族ord("A") and ord(C[x])<=ord("Z"):
print chr(ord("Z")+ord(C[x])-3-ord("A")+1),
#输入大写字母
if ord(C[x])<=ord("碰腔Z") and ord(C[x])-3>=ord("塌吵弊A"):
print chr(ord(C[x])-3),
elif ord(C[x])-3<团族ord("A") and ord(C[x])<=ord("Z"):
print chr(ord("Z")+ord(C[x])-3-ord("A")+1),
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
编碧笑码
>>> enc=lambda t,n:''.join([chr(ord(c)+n) for c in t])
解码
>搜慧察>> dec=lambda t,n:''.join([chr(ord(c)-n) for c in t])
>>> t="hello world"
>>> t
'hello world'
编码
>>> c=enc(t,5)
>>> c
'mjqqt%|twqi'世茄
解码
>>> dec(c,5)
'hello world'
>>> enc=lambda t,n:''.join([chr(ord(c)+n) for c in t])
解码
>搜慧察>> dec=lambda t,n:''.join([chr(ord(c)-n) for c in t])
>>> t="hello world"
>>> t
'hello world'
编码
>>> c=enc(t,5)
>>> c
'mjqqt%|twqi'世茄
解码
>>> dec(c,5)
'hello world'
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询