Python 使用定义的最大长度的随机空间拉伸字符串
详细解释:Python 使用定义的最大长度的随机空间拉伸字符串,python,string,Python,String,详细解释: 我编写了一个程序,用python模拟一个简单的Caesar编码算法。在加密的第一步之后,我们计算新字符串中字母的总和,然后“我想用随机空间拉伸原始字符串的大小!”它将被发送到目的地,并具有该长度,在删除空格后,它将破译字符串2次以获得原始字符串 简短解释: 只需完成stretch_str(text,length)函数即可生成字符串,如下所示: text = "Hi I'm mark." print(stretch_str(text, 23)) # The outp
我编写了一个程序,用python模拟一个简单的Caesar编码算法。在加密的第一步之后,我们计算新字符串中字母的总和,然后“我想用随机空间拉伸原始字符串的大小!”它将被发送到目的地,并具有该长度,在删除空格后,它将破译字符串2次以获得原始字符串 简短解释:
只需完成stretch_str(text,length)函数即可生成字符串,如下所示:
text = "Hi I'm mark."
print(stretch_str(text, 23))
# The output can be :
# "H i I' m m a rk ."
# or
# "H i I ' m m a r k ."
# or any thing else like these...
以及守则:
import string
def caesar(plaintext, shift):
alphabet = string.ascii_lowercase
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
table = str.maketrans(alphabet, shifted_alphabet)
return plaintext.translate(table)
def stretch_str(text, length):
# I want to complete this function.
# Stretch this text with random spaces.
# The last character is dot, always.
pass
# Sender section :
in_str = "some example sentence."
ciphered = caesar(in_str,-3)
total_sum = 0
for ch in ciphered[:-1].replace(" ", ""):
total_sum += string.ascii_lowercase.index(ch)
ciphered_more = caesar(ciphered,total_sum%26)
msg = stretch_str(ciphered_more, total_sum)
# Receiver section :
number = len(msg)
ciphered_more = msg.replace(" ", "")
ciphered = caesar(ciphered_more, -(number%26))
original_str = caesar(ciphered, 3)
print(original_str)
# Printing the "someexamplesentence."
Tnx,你的答案看起来棒极了。但我会等待别人的回答,最终我会选择最好的。但你是我的首要任务。我现在不能投票支持你。但我会的。又是Tnx。
import random
def stretch_str(text, length):
text = list(text)
while len(text) < length:
text.insert(random.randint(1, len(text) - 1), " ")
return "".join(text)
text = "Hi I'm mark."
print(f"[{stretch_str(text, 23)}]")