如何使用列表进行解码?

问题描述 投票:0回答:1

我有这个问题:

给定一个只有你知道的秘密单词P,一条消息被编码如下:消息中所有与P的位置i处的字母相同的字母都被替换为P处的i+1处的字母。换句话说,消息中每次出现的第一个字母 P 都会被消息中的第二个字母替换; P 的第二个字母每次出现都被 P 的第三个字母替换,依此类推。此外,消息中所有出现的 P 最后一个字母都被替换为 P 的第一个字母。例如,给定秘密单词“hora”,消息“Hoje esta chovendo”被编码为“Orje esth corvendh”。请注意,大写字母保持大写,小写字母保持小写。

程序的输入由两行组成,第一行包含单词 P,第二行包含要解码的消息。您的程序的输出应包含解码后的消息。您可以假设单词 P 没有重复的字母,并且仅由小写字母组成,并且该消息没有变音符号。

我做了这个代码:

P = input()
secretword = input()

P = P.lower()
secretword = secretword.lower()

P = list(P)
secretword = list(secretword)

print(P)
print(secretword)

ount_secret = 0
count_p = 0
deco_secretword = []

for i in secretword:
  if secretword[count_secret] == ' ':
    deco_secretword.append(' ')
    count_secret = count_secret + 1
  elif secretword[count_secret] == P[count_p + 1]:
    letra = P[count_p]
    deco_secretword.append(letra)
    count_secret = count_secret + 1
    count_p = count_p + 1
    if count_p + 1 == len(P):
      count_p = 0
  else:
    letra = secretword[count_secret]
    deco_secretword.append(letra)
    count_secret = count_secret + 1
    count_p = count_p + 1
    if count_p + 1 == len(P):
      count_p = 0

decodificacao = "".join(deco_secretword)

decodificacao = decodificacao[0].upper() + decodificacao[1:]

print(decodificacao)

但有些事情不对劲,我不知道是什么。输出不是预期的输出。

我正在尝试用代码解决这个练习。

python list encoding
1个回答
0
投票

正如好的评论中所指出的,for 循环测试中未使用某些变量。事实上,在整个 for 循环中,只测试了要打乱的单词中的第一个字符。

craig@Vera:~/Python_Programs/Secret$ python3 Secret.py 
higjlk
Ifmmp
['h', 'i', 'g', 'j', 'l', 'k']
['i', 'f', 'm', 'm', 'p']
Hfmmp

考虑到这一点,以下是代码的重构版本。

P = input("Enter the secret word: ")        # Added prompting verbiage for better user experience
secretword = input("Enter word to be modified: ")

P = P.lower()
secretword = secretword.lower()

P = list(P)
secretword = list(secretword)
P.insert(0, '__')                           # Just to ensure that a list search underflow does not occur

print(P)
print(secretword)

count_secret = 0
deco_secretword = []

for i in secretword:
    count_p = 0
    letter = ''
    while count_p < (len(P) - 1):           # Traverse through the secret word until the letter is found
        if i == P[count_p]:                 # Perform the "move left one letter" process if this letter is found in the secret word
            letter = P[count_p - 1]
            break
        else:
            letter = i                      # Did not seem to need a special reason for blanks

        count_p += 1

    deco_secretword.append(letter)
    count_secret = count_secret + 1

decodificacao = "".join(deco_secretword)

decodificacao = decodificacao[0].upper() + decodificacao[1:]

print(decodificacao)

注意要点:

  • 在输入语句中添加了措辞,以阐明用户应输入什么类型的数据,以提供更好的用户体验。
  • 在秘密单词列表的开头添加了一个无意义的字符,以确保在匹配的字符是秘密单词中的第一个字符的情况下不会发生列表欠载。
  • 添加了“while”循环,以递增秘密单词,尝试根据列表检查输入单词中的当前字母。
  • 仅当找到匹配时,该字符才会更改为后续字符(count_p - 1);否则角色将保持原样。
  • 对字符进行评估后,它是否会附加到解码后的字符串中进行打印。

完成这些更改后,以下是在终端进行的简单测试。

craig@Vera:~/Python_Programs/Secret$ python3 Secret.py 
Enter the secret word: abcdefghijklmnopqrstuvwxyz
Enter word to be modified: ifmmp
['__', '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']
['i', 'f', 'm', 'm', 'p']
Hello

从中得到的收获是要了解“for”循环如何与列表一起工作以及列表索引如何工作。您可能想要获取一些进一步的 Python 教程文献,以更深入地了解列表等。

© www.soinside.com 2019 - 2024. All rights reserved.