I'm trying to reduce the execution time of the program by replacing the "input" with "sys.stdin.readline". But something doesnt wrong.
The first line contains text consisting of words separated by single spaces. The next line of input is given a number - the number of encrypted words. The next n lines contain encrypted words. Lines are displayed that contain the decryption (breaking Caesar's cipher).
Code with "input":
import sys
letters = 'abcdefghijklmnopqrstuvwxyz'
inp_str = input('') # 'a abb bab abc'
inp_n = int(input()) # 3
inp_str_list = inp_str.split(' ')
list_secret_words = [input() for x in range(inp_n)] # 'def' 'abc' 'aza'
for i in list_secret_words:
message = i
for key in range(len(letters)):
translated = ''
for symbol in message:
if symbol in letters:
num = letters.find(symbol)
num = num - key
if num < 0:
num = num + len(letters)
translated = translated + letters[num]
else:
translated = translated + symbol
for x in inp_str_list:
if translated == x:
sys.stdout.write(translated+'\n') # result: "abc" "abc" "bab" (right)
Code with "sys.stdin.readline":
import sys
letters = 'abcdefghijklmnopqrstuvwxyz'
inp_str = sys.stdin.readline() # 'a abb bab abc'
inp_n = int(sys.stdin.readline()) # 3
inp_str_list = inp_str.split(' ')
list_secret_words = [sys.stdin.readline() for x in range(inp_n)] # 'def' 'abc' 'aza'
for i in list_secret_words:
message = i
for key in range(len(letters)):
translated = ''
for symbol in message:
if symbol in letters:
num = letters.find(symbol)
num = num - key
if num < 0:
num = num + len(letters)
translated = translated + letters[num]
else:
translated = translated + symbol
for x in inp_str_list:
if translated == x:
sys.stdout.write(translated+'\n') # result: "abc" " " "abc" (wrong)