Skip to content

Commit 3436850

Browse files
committed
HackerEarth String Problems
1 parent 365f8d3 commit 3436850

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Caesar's Cipher is a very famous encryption technique used in cryptography. It is a type of substitution
2+
# cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down
3+
# the alphabet. For example, with a shift of 3, D would be replaced by G, E would become H, X would become A
4+
# and so on.
5+
#
6+
# Encryption of a letter X by a shift K can be described mathematically as
7+
# EK(X)=(X+K) % 26.
8+
#
9+
# Given a plaintext and it's corresponding ciphertext, output the minimum non-negative value of shift that was
10+
# used to encrypt the plaintext or else output −1 if it is not possible to obtain the given ciphertext from
11+
# the given plaintext using Caesar's Cipher technique.
12+
#
13+
# Input:
14+
#
15+
# The first line of the input contains Q, denoting the number of queries.
16+
#
17+
# The next Q lines contain two strings S and T consisting of only upper-case letters.
18+
#
19+
# Output:
20+
#
21+
# For each test-case, output a single non-negative integer denoting the minimum value of shift that was used
22+
# to encrypt the plaintext or else print −1 if the answer doesn't exist.
23+
#
24+
# Constraints:
25+
# 1≤Q≤5
26+
# 1≤|S|≤10^5
27+
# 1≤|T|≤10^5
28+
# |S| = |T|
29+
#
30+
# SAMPLE INPUT
31+
# 2
32+
# ABC
33+
# DEF
34+
# AAA
35+
# PQR
36+
#
37+
# SAMPLE OUTPUT
38+
# 3
39+
# -1
40+
41+
# My Solution
42+
for _ in range(int(input())):
43+
string_one = input()
44+
string_two= input()
45+
check_one = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
46+
# ZYXWVUTSRQPONMLKJIHGFEDCBA
47+
check_two = check_one[::-1]
48+
result = []
49+
for i in range(len(string_one)):
50+
if(check_one.find(string_one[i]) > check_one.find(string_two[i])):
51+
result.append(check_two.find(string_one[i]) + check_one.find(string_two[i]) + 1)
52+
else:
53+
result.append(check_one.find(string_two[i]) - check_one.find(string_one[i]))
54+
55+
if result.count(result[0]) == len(string_one):
56+
print(result[0])
57+
else:
58+
print(-1)
59+
60+
# More Efficient Solution:
61+
tests = int(input().strip())
62+
for i in range(tests):
63+
plain = input().strip()
64+
cipher = input().strip()
65+
shift = (ord(cipher[0])-ord(plain[0])+26)%26
66+
valid = True
67+
for j in range(len(plain)):
68+
if (ord(cipher[j])-ord(plain[j])+26)%26 != shift:
69+
valid = False
70+
break
71+
print(shift) if valid else print("-1")
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# You are converting an old code for a new version of the compiler.
2+
#
3+
# In the old code we have used "->" for pointers. But now we have to replace each "->" with a ".". But this
4+
# replacement shouldn't be done inside commments. A comment is a string that starts with "//" and terminates
5+
# at the end of the line.
6+
#
7+
# Input:
8+
#
9+
# At max.
10+
# 2000
11+
# 2000 lines of code.
12+
#
13+
# Each line of code consists of at maximum
14+
# 60
15+
# 60 characters.
16+
#
17+
# Output:
18+
#
19+
# New code with required changes.
20+
#
21+
# SAMPLE INPUT
22+
# int t; //variable t
23+
# t->a=0; //t->a does something
24+
# return 0;
25+
#
26+
# SAMPLE OUTPUT
27+
# int t; //variable t
28+
# t.a=0; //t->a does something
29+
# return 0;
30+
31+
import sys
32+
import re
33+
while True:
34+
line = sys.stdin.readline()
35+
if len(line) >=2 :
36+
if '//' in line:
37+
line = re.split("//", line)
38+
line[0] = re.sub("->", ".", line[0])
39+
sys.stdout.write('//'.join(line))
40+
else:
41+
sys.stdout.write(re.sub("->", ".", line))
42+
else:
43+
break
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# John has recently learned about ASCII values. With his knowledge of ASCII values and character he has
2+
# developed a special word and named it John's Magical word.
3+
#
4+
# A word which consists of alphabets whose ASCII values is a prime number is a John's Magical word. An
5+
# alphabet is john's Magical alphabet if its ASCII value is prime.
6+
#
7+
# John's nature is to boast about the things he know or have learnt about. So just to defame his friends he
8+
# gives few string to his friends and ask them to convert it to John's Magical word. None of his friends would
9+
# like to get insulted. Help them to convert the given strings to John's Magical Word.
10+
#
11+
# Rules for converting:
12+
#
13+
# 1.Each character should be replaced by the nearest John's Magical alphabet.
14+
#
15+
# 2.If the character is equidistant with 2 Magical alphabets. The one with lower ASCII value will be considered as its replacement.
16+
#
17+
# Input format:
18+
#
19+
# First line of input contains an integer T number of test cases. Each test case contains an integer N (denoting the length of the string) and a string S.
20+
#
21+
# Output Format:
22+
#
23+
# For each test case, print John's Magical Word in a new line.
24+
#
25+
# Constraints:
26+
#
27+
# 1 <= T <= 100
28+
#
29+
# 1 <= |S| <= 500
30+
#
31+
# SAMPLE INPUT
32+
# 1
33+
# 8
34+
# KINGKONG
35+
#
36+
# SAMPLE OUTPUT
37+
# IIOGIOOG
38+
39+
numl = [97, 101, 103, 107, 109, 113]
40+
numu = [67, 71, 73, 79, 83, 89]
41+
num = [67, 89, 97, 113]
42+
43+
for _ in range(int(input())):
44+
length = input()
45+
string = input()
46+
result = ''
47+
48+
for char in string:
49+
if char.islower() and char.isalpha():
50+
minimum = 200
51+
ascii_char = ord(char)
52+
temp = 0
53+
54+
for j in numl:
55+
if minimum > abs(ascii_char - j):
56+
minimum = abs(ascii_char - j)
57+
temp = j
58+
59+
result = result + chr(temp)
60+
61+
elif char.isupper() and char.isalpha():
62+
minimum = 200
63+
ascii_char = ord(char)
64+
temp = 0
65+
66+
for j in numu:
67+
if minimum > abs(ascii_char - j):
68+
minimum = abs(ascii_char - j)
69+
temp = j
70+
71+
result = result + chr(temp)
72+
73+
else:
74+
minimum = 200
75+
ascii_char = ord(char)
76+
temp = 0
77+
78+
for j in num:
79+
if minimum > abs(ascii_char - j):
80+
minimum = abs(ascii_char - j)
81+
temp = j
82+
83+
result = result + chr(temp)
84+
85+
print(result)

0 commit comments

Comments
 (0)