Cerinta completa
Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar’s cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c.
Original alphabet: abcdefghijklmnopqrstuvwxyz
Alphabet rotated +3: defghijklmnopqrstuvwxyzabc
Example
The alphabet is rotated by , matching the mapping above. The encrypted string is .
Note: The cipher only encrypts letters; symbols, such as -, remain unencrypted.
Function Description
Complete the caesarCipher function in the editor below.
caesarCipher has the following parameter(s):
- string s: cleartext
- int k: the alphabet rotation factor
Returns
- string: the encrypted string
Input Format
The first line contains the integer, , the length of the unencrypted string.
The second line contains the unencrypted string, .
The third line contains , the number of letters to rotate the alphabet by.
Constraints
is a valid ASCII string without any spaces.
Sample Input
11
middle-Outz
2
Sample Output
okffng-Qwvb
Explanation
Original alphabet: abcdefghijklmnopqrstuvwxyz
Alphabet rotated +2: cdefghijklmnopqrstuvwxyzab
m -> o
i -> k
d -> f
d -> f
l -> n
e -> g
- -
O -> Q
u -> w
t -> v
z -> b
Limbajul de programare folosit: python3
Cod:
#!/bin/python3
def caesarCipher(s, k):
k %= 26
out = []
for ch in s:
o = ord(ch)
if 65 <= o <= 90:
out.append(chr((o - 65 + k) % 26 + 65))
elif 97 <= o <= 122:
out.append(chr((o - 97 + k) % 26 + 97))
else:
out.append(ch)
return ''.join(out)
if __name__ == '__main__':
n = int(input().strip())
s = input()
k = int(input().strip())
print(caesarCipher(s, k))
Scor obtinut: 1.0
Submission ID: 464590129
Link challenge: https://www.hackerrank.com/challenges/caesar-cipher-1/problem
