【问题标题】:A caesar cipher function that shift vowels "aeiou"转换元音“aeiou”的凯撒密码函数
【发布时间】:2019-11-25 09:52:46
【问题描述】:

所以我正在尝试编写一个在元音“aeiou”和“AEIOU”的参考字符串中移动的函数。这是我目前所拥有的:

vowels = "aeiouAEIOU"

    def encrypt(message, shift):
      output = ''
      for char in message:
        index = vowels.find(char)
        if index < 0: 
          output += ' '

        else:
          new_index = (index + shift) % len(vowels)
          new_character = vowels[new_index]
          output += new_character
      return output

我想要的是,例如,如果我输入 encrypt("a",2),输出应该是 "i", encrypt("A",2) --> "I",但是,如果我输入 encrypt("a",6),我希望输出是“e”而不是“E”。而且当我输入不同于元音的内容时,例如数字或“k”,函数应该返回“k”或数字。最后一件事是,如果我想使用我写的函数来查找一串句子中的所有元音并移位它们怎么办?

【问题讨论】:

  • 如果你给("O", 6),函数的期望是什么?引发错误或回到起点并返回u?
  • 我想向后移回U

标签: python python-3.x shift caesar-cipher


【解决方案1】:

您可以简单地使用字符串的maketranstranslate方法制作翻译表,然后翻译字符串。

def ceaser_shift_vowels(sentance: str, offset: int):
    offset = offset % 5
    upper_clear = "AEIOU"
    lower_clear = "aeiou"
    upper_cipher = upper_clear[offset:] + upper_clear[:offset]
    lower_cipher = lower_clear[offset:] + lower_clear[:offset]
    translator = sentance.maketrans(upper_clear + lower_clear, upper_cipher + lower_cipher)
    return sentance.translate(translator)

sentance = "The quick brown fox jumps over the lazy dog"
for i in range(10):
    print(ceaser_shift_vowels(sentance, i))

输出

The quick brown fox jumps over the lazy dog
Thi qaock bruwn fux jamps uvir thi lezy dug
Tho qeuck brawn fax jemps avor tho lizy dag
Thu qiack brewn fex jimps evur thu lozy deg
Tha qoeck briwn fix jomps ivar tha luzy dig
The quick brown fox jumps over the lazy dog
Thi qaock bruwn fux jamps uvir thi lezy dug
Tho qeuck brawn fax jemps avor tho lizy dag
Thu qiack brewn fex jimps evur thu lozy deg
Tha qoeck briwn fix jomps ivar tha luzy dig

这还具有解密字符串的好处,您只需将偏移量作为负数传递

sentance = "The quick brown fox jumps over the lazy dog"
offset = 6
crypt_string = ceaser_shift_vowels(sentance, offset)
print(crypt_string)
decrypt_string = ceaser_shift_vowels(crypt_string, -offset)
print(decrypt_string)

输出

Thi qaock bruwn fux jamps uvir thi lezy dug
The quick brown fox jumps over the lazy dog

【讨论】:

  • 小写字母不是要保持小写吗?
  • Umm...ceaser_shift_vowels('a', 6) 返回E - OP 确实说:如果我输入 encrypt("a",6),我希望输出不是“e” "E
  • 哦,是吗?我以为他们会一直循环:(
  • 啊,没注意到
  • 更新为单独处理大小写
【解决方案2】:

我相信,这将实现您需要的转变。

vowels = "aeiouAEIOU"


def encrypt(message, shift):
    output = ''
    for char in message:
        index = vowels.find(char)
        if index < 0:
            output += char

        else:
            shift %= 5

            if index >= 5:
                new_index = (index + shift) % 5 + 5
            else:
                new_index = (index + shift) % 5
# Or, alternatively to the above 4 lines:
#           new_index = (index + shift) % 5 + 5 * int(index >= 5)
            new_character = vowels[new_index]
            output += new_character
    return output
encrypt('the quick brown fox jumped over the lazy dogs', 1)
Out[3]: 'thi qaock bruwn fux jampid uvir thi lezy dugs'
encrypt('the quick brown fox jumped over the lazy dogs'.upper(), 1)
Out[4]: 'THI QAOCK BRUWN FUX JAMPID UVIR THI LEZY DUGS'
encrypt('the quick brown fox jumped over the lazy dogs'.upper(), 13)
Out[5]: 'THU QIACK BREWN FEX JIMPUD EVUR THU LOZY DEGS'

您可以通过将检查是否为大写嵌入到加法中来简化 4 行 if 语句,使用 int(True) 为 1 且 int(False) 为 0 的质量

new_index = (index + shift) % 5 + 5 * int(index >= 5)

【讨论】:

  • 呵呵,你用的和我一样的句子。
猜你喜欢
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
  • 2014-03-07
  • 2020-05-31
  • 2014-02-06
相关资源
最近更新 更多