[英]How to get different element of a list evertime to set as an increment so I can edit another lists elements?
I was assigned to edit a simple ROT13 code, however, instead of setting the increment as the number 13, I am trying to set the increment as the number of characters in each word.我被分配编辑一个简单的 ROT13 代码,但是,我没有将增量设置为数字 13,而是尝试将增量设置为每个单词中的字符数。
For example;例如; if the plain text is " I love you too" Then, every word has to have its own increment since the word " love " should become " pszi " because every letter should be incremented by 4 because the word has 4 letters itself.
如果纯文本是“我也爱你”,那么每个单词都必须有自己的增量,因为“love”这个词应该变成“pszi”,因为每个字母都应该加 4,因为这个词本身有 4 个字母。 Also, the word "you" should become "brx" because it has an increment of 3 since the word has 3 letters.
此外,“you”这个词应该变成“brx”,因为它有 3 个字母,增量为 3。
import sys
# Dictionary to lookup the index of alphabets
alphabet = {'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4, 'E' : 5,
'F' : 6, 'G' : 7, 'H' : 8, 'I' : 9, 'J' : 10,
'K' : 11, 'L' : 12, 'M' : 13, 'N' : 14, 'O' : 15,
'P' : 16, 'Q' : 17, 'R' : 18, 'S' : 19, 'T' : 20,
'U' : 21, 'V' : 22, 'W' : 23, 'X' : 24, 'Y' : 25, 'Z' : 26}
# Dictionary to lookup alphabets corresponding to the index after inc
enchabet = {0 : 'Z', 1 : 'A', 2 : 'B', 3 : 'C', 4 : 'D', 5 : 'E',
6 : 'F', 7 : 'G', 8 : 'H', 9 : 'I', 10 : 'J',
11 : 'K', 12 : 'L', 13 : 'M', 14 : 'N', 15 : 'O',
16 : 'P', 17 : 'Q', 18 : 'R', 19 : 'S', 20 : 'T',
21 : 'U', 22 : 'V', 23 : 'W', 24 : 'X', 25 : 'Y'}
# Function to rotLPWen the string
# according to the inc provided
def rotLPWen(msg, inc):
calc = ''
for char in msg:
# checking for space
if(char != ' '):
# looks up the dictionary and
# adds the inc to the index
num = ( alphabet[char] + inc ) % 26
# looks up the second dictionary for
# the inced alphabets and adds them
calc += enchabet[num]
else:
# adds space
calc += ' '
#print("You can only type one word at a time!")
#sys.exit(1)
return calc
# Function to rotLPWde the string
# according to the inc provided
def rotLPWde(msg, inc):
calced = ''
for char in msg:
# checks for space
if(char != ' '):
# looks up the dictionary and
# subtracts the inc to the index
num = ( alphabet[char] - inc + 26) % 26
# looks up the second dictionary for the
# inced alphabets and adds them
calced += enchabet[num]
else:
# adds space
calced += ' '
#print("You can only type one word at a time!")
#sys.exit(1)
return calced
# driver function to run the program
def main():
# use 'upper()' function to convert any lowercase characters to uppercase
msg = input("Plase type something!")
# Create an increment for the number of characters in the sentence
increment = len(msg)
inc = increment
result = rotLPWen(msg.upper(), inc)
#Creates a list from the sentence
#list1 = list(map(len,msg.split()))
print(result)
print(increment)
# Executes the main function
if __name__ == '__main__':
main()
At this point, I am able to change the increment by the number of characters in the sentence, but not the words individually.此时,我可以通过句子中的字符数来更改增量,但不能单独更改单词。 I tried printing the words one by one encrypting them individually and printing them as one sentence but I was not able to get them out of the list one by one while assigning the necessary increment to them.
我尝试逐个打印单词,将它们单独加密并将它们打印为一个句子,但是在为它们分配必要的增量时,我无法将它们逐个从列表中删除。 I was thinking if could I set a list that has
我在想是否可以设置一个包含
" [I] [love] [you] [too]
" and combine it with another list such as list1 = list(map)len, msg.split()))
which should look like [1, 4, 3, 3] but I was not able to combine them together since one element has to become the integer for another element from another list. “
[I] [love] [you] [too]
”并将其与另一个列表组合,例如list1 = list(map)len, msg.split()))
应该看起来像 [1, 4, 3, 3]但我无法将它们组合在一起,因为一个元素必须成为另一个列表中另一个元素的 integer。
Here is one possible solution.这是一种可能的解决方案。
def rotation_decoder (encoded_string: str, rotation_key: int) -> str :
alphabet = 'abcdefghijklmnopqrstuvwxyz'
rotation = alphabet [rotation_key:] + alphabet [:rotation_key]
translation_table = encoded_string.maketrans (alphabet, rotation)
return encoded_string.translate (translation_table)
decoded_sentence = ''
sentence = 'i love you too'
words = sentence.split (' ')
for word in words :
decoded_word = rotation_decoder (word, len (word))
decoded_sentence += decoded_word + ' '
print (decoded_sentence)
I modified your code and added the functionality to increment the alphabets of each word by the length of the word.我修改了您的代码并添加了将每个单词的字母增加单词长度的功能。
Now, if the input message is a sentence, it splits it into a list of words and encodes each word individually, and constructs the encoded sentence at the end.现在,如果输入消息是一个句子,它会将其拆分为一个单词列表并单独编码每个单词,并在最后构造编码的句子。
On the other hand, if the input if a single word, it simple encodes it without creating a list.另一方面,如果输入是一个单词,它会简单地对其进行编码而不创建列表。
import sys
# Dictionary to lookup the index of alphabets
alphabet = {'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4, 'E' : 5,
'F' : 6, 'G' : 7, 'H' : 8, 'I' : 9, 'J' : 10,
'K' : 11, 'L' : 12, 'M' : 13, 'N' : 14, 'O' : 15,
'P' : 16, 'Q' : 17, 'R' : 18, 'S' : 19, 'T' : 20,
'U' : 21, 'V' : 22, 'W' : 23, 'X' : 24, 'Y' : 25, 'Z' : 26}
# Dictionary to lookup alphabets corresponding to the index after inc
enchabet = {0 : 'Z', 1 : 'A', 2 : 'B', 3 : 'C', 4 : 'D', 5 : 'E',
6 : 'F', 7 : 'G', 8 : 'H', 9 : 'I', 10 : 'J',
11 : 'K', 12 : 'L', 13 : 'M', 14 : 'N', 15 : 'O',
16 : 'P', 17 : 'Q', 18 : 'R', 19 : 'S', 20 : 'T',
21 : 'U', 22 : 'V', 23 : 'W', 24 : 'X', 25 : 'Y'}
# Function to rotLPWen the string
# according to the inc provided
def rotLPWen(msg, inc):
calc = ''
for char in msg:
# checking for space
if(char != ' '):
# looks up the dictionary and
# adds the inc to the index
num = ( alphabet[char] + inc ) % 26
# looks up the second dictionary for
# the inced alphabets and adds them
calc += enchabet[num]
else:
# adds space
calc += ' '
#print("You can only type one word at a time!")
#sys.exit(1)
return calc
# Function to rotLPWde the string
# according to the inc provided
def rotLPWde(msg, inc):
calced = ''
for char in msg:
# checks for space
if(char != ' '):
# looks up the dictionary and
# subtracts the inc to the index
num = ( alphabet[char] - inc + 26) % 26
# looks up the second dictionary for the
# inced alphabets and adds them
calced += enchabet[num]
else:
# adds space
calced += ' '
#print("You can only type one word at a time!")
#sys.exit(1)
return calced
# driver function to run the program
def main():
# use 'upper()' function to convert any lowercase characters to uppercase
msg = input("Plase type something!")
msg = msg.strip()
if ' ' in msg.strip():
words = msg.split(' ')
encrypted = ''
increments = []
for word in words:
# Create an increment for the number of characters in the sentence
increment = len(word)
inc = increment
result = rotLPWen(word.upper(), inc)
encrypted += result + ' '
increments.append(inc)
else:
increments = len(msg)
inc = increments
encrypted = rotLPWen(msg.upper(), inc)
#Creates a list from the sentence
#list1 = list(map(len,msg.split()))
print(encrypted)
print(increments)
# Executes the main function
if __name__ == '__main__':
main()
OUTPUT: OUTPUT:
Plase type something! I love you too
J PSZI BRX WRR
[1, 4, 3, 3]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.