数字偏移时需要对10取模。参考如下:
def caesar_cipher_encrypt(text, shift):
result = ''
for char in text:
if char.isalpha():
# 处理字母
if char.isupper():
result += chr((ord(char) + shift - 65) % 26 + 65)
else:
result += chr((ord(char) + shift - 97) % 26 + 97)
elif char.isdigit():
# 处理数字
result += str((int(char) + shift) % 10)
else:
# 处理其他字符
result += char
return result
text = '2019 abc'
shift = 3
encrypted_text = caesar_cipher_encrypt(text, shift)
print(encrypted_text)
你这个加密方式简单,就是把字符串向后移动3位,如果超出数字或字母,就从头开始
比2变成5,0变成3,但是9向后移动3位抄出了0-9的数字范围,所以回到0开始,所以9变成2
结果%10呀
创建mxagegendernet.py文件。
# import the necessary packages
import mxnet as mx
class MxAgeGenderNet:
@staticmethod
def build(classes):
# data input
data = mx.sym.Variable("data")
# Block #1: first CONV => RELU => POOL layer set
conv1_1 = mx.sym.Convolution(data=data, kernel=(7, 7), stride=(4, 4), num_filter=96)
act1_1 = mx.sym.Activation(data=conv1_1, act_type="relu")
bn1_1 = mx.sym.BatchNorm(data=act1_1)
pool1 = mx.sym.Pooling(data=bn1_1, pool_type="max", kernel=(3, 3), stride=(2, 2))
do1 = mx.sym.Dropout(data=pool1, p=0.25)
# Block #2: second CONV => RELU => POOL layer set
conv2_1 = mx.sym.Convolution(data=do1, kernel=(5, 5), pad = (2, 2), num_filter = 256)
act2_1 = mx.sym.Activation(data=conv2_1, act_type="relu")
bn2_1 = mx.sym.BatchNorm(data=act2_1)
pool2 = mx.sym.Pooling(data=bn2_1, pool_type="max", kernel = (3, 3), stride = (2, 2))
do2 = mx.sym.Dropout(data=pool2, p=0.25)
# Block #3: second CONV => RELU => POOL layer set
conv2_1 = mx.sym.Convolution(data=do2, kernel=(3, 3), pad = (1, 1), num_filter = 384)
act2_1 = mx.sym.Activation(data=conv2_1, act_type="relu")
bn2_1 = mx.sym.BatchNorm(data=act2_1)
pool2 = mx.sym.Pooling(data=bn2_1, pool_type="max", kernel = (3, 3), stride = (2, 2))
do3 = mx.sym.Dropout(data=pool2, p=0.25)
# Block #4: first set of FC => RELU layers
flatten = mx.sym.Flatten(data=do3)
fc1 = mx.sym.FullyConnected(data=flatten, num_hidden=512)
act4_1 = mx.sym.Activation(data=fc1, act_type="relu")
bn4_1 = mx.sym.BatchNorm(data=act4_1)
do4 = mx.sym.Dropout(data=bn4_1, p=0.5)
# Block #5: second set of FC => RELU layers
fc2 = mx.sym.FullyConnected(data=do4, num_hidden=512)
act5_1 = mx.sym.Activation(data=fc2, act_type="relu")
bn5_1 = mx.sym.BatchNorm(data=act5_1)
do5 = mx.sym.Dropout(data=bn5_1, p=0.5)
# softmax classifier
fc3 = mx.sym.FullyConnected(data=do5, num_hidden=classes)
model = mx.sym.SoftmaxOutput(data=fc3, name="softmax")
# return the network architecture
return model
我可以回答第一个问题,具体解决方案如下:
凯撒密码是一种简单的替换密码,将明文中的每个字母通过移位来进行加密,其中移位的幅度是固定的,可以用一个数字表示。对于数字的加密,我们可以将其转换成对应的字母再使用凯撒密码进行加密,加密完成后再将字母转换回数字即可。
下面是一个示例代码:
def caesar_cipher(num, shift):
# 将数字转换成对应的字母,例如 0 转换成 'a'
char = chr(num + 97)
# 使用凯撒密码进行加密
cipher = chr((ord(char) - 97 + shift) % 26 + 97)
# 将加密后的字母转换回数字,例如 'b' 转换成 1
result = ord(cipher) - 97
return result
# 示例:将数字 9 加密,移位为 3,加密后应该为 2
print(caesar_cipher(9, 3))
输出结果为 2,符合要求。
需要注意的是,这里使用的凯撒密码只能加密小写字母,因此需要将数字转换成小写字母再进行加密。另外,加密完成后需要将字母再转换回数字才能得到最终结果。