對于課程,我正在創建一個函式,該函式在給定的移位后將字母表中的每個字母轉換為其相應的字母,這種方式凱撒密碼會移位每個字母。到目前為止,我有:
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
# Problem 1.
def build_cipher(shift):
'''
Description: takes in shift (an integer representing the amount the letter key in the dictionary is shifted from its corresponding letter) and returns a dictionary containing all letters and their corresponding letters after the shift. This is acheived through subtracting the shift from the number corresponding to the letter, and using modulo 26.
>>> build_cipher(3)
'''
for i in range(0, 26):
letter = alphabet[i]
shiftedletter = alphabet[(i - shift) % 26]
return {letter : shiftedletter}
這大部分是正確的,但它只輸出:
{'a': 'x'}
我怎樣才能讓它為每個字母輸出?我希望我的輸出是:
{‘a’: ‘x’,
‘b’: ‘y’,
‘c’: ‘z’,
‘d’: ‘a’,
‘e’: ‘b’,
‘f’: ‘c’,
‘g’: ‘d’,
‘h’: ‘e’,
‘i’: ‘f’,
‘j’: ‘g’,
‘k’: ‘h’,
‘l’: ‘i’,
‘m’: ‘j’,
‘n’: ‘k’,
‘o’: ‘l’,
‘p’: ‘m’,
‘q’: ‘n’,
‘r’: ‘o’,
‘s’: ‘p’,
‘t’: ‘q’,
‘u’: ‘r’,
‘v’: ‘s’,
‘w’: ‘t’,
‘x’: ‘u’,
‘y’: ‘v’,
‘z’: ‘w’}
我認為問題在于它沒有為字母表中的每個字母回圈,但我不確定如何解決這個問題。任何幫助表示贊賞,謝謝!
uj5u.com熱心網友回復:
您想先構建字典。完成后,回傳剛剛構建的字典。
def build_cipher(shift):
cipher = dict()
for i in range(0, 26):
letter = alphabet[i]
shiftedletter = alphabet[(i - shift) % 26]
cipher[letter] = shiftedletter
return cipher
或者,作為一種理解:
def build_cipher(shift):
return {alphabet[i]: alphabet[(i - shift) % 26] for i in range(0, 26)}
uj5u.com熱心網友回復:
作為替代方案,您可以使用任何移位值str.maketrans()制作轉換表t:
def make_table(alpha, shift):
k = shift % len(alpha)
return str.maketrans(alpha, alpha[k:] alpha[:k])
然后用于s.translate(t)翻譯任何字串s:
In [1]: s = "hello world"
In [2]: alpha = "abcdefghijklmnopqrstuvwxyz"
In [3]: t = make_table(alpha, 13)
In [4]: s.translate(t)
Out[4]: 'uryyb jbeyq'
In [5]: t = make_table(alpha, -42)
In [8]: s.translate(t)
Out[8]: 'rovvy gybvn'
uj5u.com熱心網友回復:
這是解決問題的一種方法。
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
# Problem 1.
def build_cipher(shift):
result = {}
for i in range(0, 26):
letter = alphabet[i]
shiftedletter = alphabet[(i - shift) % 26]
result.update({letter : shiftedletter})
return result
uj5u.com熱心網友回復:
一個簡單的解決方案,可以修復您的代碼。
def build_cipher(shift):
'''
Description: takes in shift (an integer representing the amount the letter
key in the dictionary is shifted from its corresponding letter) and returns
a dictionary containing all letters and their corresponding letters after
the shift. This is acheived through subtracting the shift from the number
corresponding to the letter, and using modulo 26.
>>> build_cipher(3)
'''
alphabet = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
]
cipher = {}
for i in range(0, 26):
cipher[alphabet[i]] = alphabet[(i - shift) % 26]
return cipher
cipher = build_cipher(3)
for k,v in cipher.items():
print("{}: {}".format(k, v))
輸出:
a: x
b: y
c: z
d: a
e: b
f: c
g: d
h: e
i: f
j: g
k: h
l: i
m: j
n: k
o: l
p: m
q: n
r: o
s: p
t: q
u: r
v: s
w: t
x: u
y: v
z: w
uj5u.com熱心網友回復:
你第一次呼叫return它的那一刻就會跳出函式。您可以將 return 陳述句更改為列印陳述句,也可以將回傳字串部分放在回圈中。像這樣的東西:
ret = ""
for i in range(0, 26):
letter = alphabet[i]
shiftedletter = alphabet[(i - shift) % 26]
ret = "{" letter " : " shiftedletter "}\n"
return ret
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315518.html
上一篇:使用函式編碼字串
下一篇:C 反轉字串但先列印數字
