首页 > 解决方案 > Python:通过使用每个其他字母“aceg”来更改像“abcdefg”这样的单词,然后按顺序添加其余的“bdf”。

问题描述

我有这个任务:

您想制作一个代码来隐藏您的消息,但不要让接收者难以解码。每个代码都是小写字母(它将忽略最后一个单词中的最后一个字符以保持标点符号相同)。您将通过使用每隔一个字母“aceg”来更改像“abcdefg”这样的单词,然后按顺序添加其余字母“bdf”。所以“abcdefg”变成了“acegbdf”。请注意,1 或 2 个字母的单词将保持不变。您还必须将每个句子的第一个字母大写。

输入:一个句子将包含所有小写字符。

样本输入:

sentence = ["this", "is", "a", "test", "of", "the", "emergency", "broadcast", "system"]

输出: "Tihs is a tset of teh eegnymrec bodatracs sseytm."

这就是我到目前为止所拥有的。即使我研究了 Python 书,我也不知道如何处理它,抱歉这里的输入很少!

for word in sentence:
    print word[::3],
    print word[::1]

标签: python

解决方案


new_sentence = ''
for word in sentence:
    new_sentence += word[::2] + word[1::2] + ' '
print(new_sentence.strip().capitalize())

输出:

Tihs is a tset of teh eegnymrec bodatracs sseytm

推荐阅读