Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0b695e4

Browse files
HackerEarth String Problem
1 parent d1a2e33 commit 0b695e4

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# X and Y are best friends and they love to chat with each other. But their recent concerns about the privacy
2+
# of their messages has distant them. So they decided to encrypt their messages with a key, K, such that the
3+
# character of their messages are now shifted K times towards right of their initial value. Their techniques
4+
# only convert numbers and alphabets while leaving special characters as it is.
5+
#
6+
# Provided the value K you are required to encrypt the messages using their idea of encryption.
7+
#
8+
# INPUT FORMAT
9+
#
10+
# The first line of the input contains, T, the number of messages. The next line contains N, and K, no of
11+
# characters in the message and key for encryption. The next line contains the message.
12+
#
13+
# OUTPUT FORMAT
14+
#
15+
# Output the encrypted messages on a new line for all the test cases.
16+
#
17+
# CONSTRAINS
18+
#
19+
# 1≤T≤100
20+
# 1≤N≤106
21+
# 0≤K≤106
22+
#
23+
# SAMPLE INPUT
24+
# 2
25+
# 12 4
26+
# Hello-World!
27+
# 16 50
28+
# Aarambh@1800-hrs
29+
#
30+
# SAMPLE OUTPUT
31+
# Lipps-Asvph!
32+
# Yypykzf@1800-fpq
33+
34+
myString = 'abcdefghijklmnopqrstuvwxyz'
35+
myStringU = myString.upper()
36+
nums = '0123456789'
37+
38+
def access_char(string, i):
39+
return string[i % len(string)]
40+
41+
for _ in range(int(input())):
42+
n, k = map(int, input().split())
43+
string = input()
44+
result = []
45+
46+
for char in string:
47+
if char.islower() and char.isalpha():
48+
result.append(access_char(myString, myString.find(char) + k))
49+
elif char.isupper() and char.isalpha():
50+
result.append(access_char(myStringU, myStringU.find(char) + k))
51+
elif char.isnumeric():
52+
result.append(access_char(nums, nums.find(str(char)) + k))
53+
else:
54+
result.append(char)
55+
56+
print(''.join([str(i) for i in result]))
57+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /