I ask for help. There is a task:
https://stepik.org / lesson / Caesar-cipher-23896 / step / 1? adaptive = true & amp; unit = 6422
Caesar’s cipher consists in replacing each character of the input string with a character located several positions to the left or to the right of it in the alphabet.
The shift is the same for all characters. The shift is cyclical, i.e. if a unit shift is applied to the last character of the alphabet, it will be replaced by the first character, and vice versa.
Write a program that encrypts text with the Caesar cipher.
Alphabet used – space and small Latin characters: ‘abcdefghijklmnopqrstuvwxyz’
Input format:
The first line specifies the encryption offset to be used: an integer. A positive number corresponds to a shift to the right. The second line contains a non-empty encryption phrase. Ignore leading and trailing spaces.
Output format:
The only line containing the phrase: Result: “…”, where instead of an ellipsis inside quotes, an encrypted sequence is written.
Sample Input 1:
3
i am caesar
Sample Output 1:
Result: "lcdpcfdhvdu"
So, here’s how I solved it:
a = int (input ())
b = input ()
c = 'abcdefghijklmnopqrstuvwxyz'
res = []
len_c = len (c)
for i in b:
res.append (c [(c.find (i) + a)% len_c])
print ('Result:', '"', ''. join (res), '"', sep = '')
The interpreter outputs everything correctly. The test does not pass on the site. Please tell me where you went wrong.
Answer 1, authority 100%
My listing for this task:
alpha = 'abcdefghijklmnopqrstuvwxyz'
n = int (input ())
s = input (). strip ()
res = ''
for c in s:
res + = alpha [(alpha.index (c) + n)% len (alpha)]
print ('Result: "' + res + '"')
In your code, you should add the removal of whitespace at the beginning and end of the line.
Answer 2
# modules
from colorama import *
from pyfiglet import *
init ()
#title
title = figlet_format ('Cesar')
print (Fore.WHITE)
print (title)
print (Fore.GREEN)
#main
while True:
offset = int (input ('Offset'))
str = input ('String')
alphabet = 'abcdefghijklmnopqrstuvwxyz'
res = []
ln = len (alphabet)
n = '. \ / [] {} () = -.,;: \' "1234567890 ~!` @ # $% ^ & amp; * & lt; & gt;? | = + _- '
for l in str:
if not l in n:
res.append (alphabet [(alphabet.find (l) + offset)% ln])
else:
res.append (l)
print (''. join (res))
This code filters out characters