Problem Statement: Dhananjay has recently learned about ASCII values. He is very fond of experimenting. With his
knowledge of ASCII values and character he has developed a special word and
named it Dhananjay's Magical word.
A word
which consist of alphabets whose ASCII values
is a prime number is a Dhananjay's Magical word. An alphabet is Dhananjay's
Magical alphabet if its ASCII value
is prime.
Dhananjay's
nature is to boast about the things he know or have learnt about. So just to
defame his friends he gives few string to his friends and ask them to convert
it to Dhananjay's Magical word. None of his friends would like to get insulted.
Help them to convert the given strings to Dhananjay's Magical Word.
Rules for
converting:
1.Each
character should be replaced by the nearest Dhananjay's Magical alphabet.
2.If the
character is equidistant with 2 Magical alphabets. The one with lower ASCII value
will be considered as its replacement.
Input
format:
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.
Output
Format:
For each
test case, print Dhananjay's Magical Word in a new line.
Constraints:
1 <= T
<= 100
1 <=
|S| <= 500
Programming
using Python 2.7.6: I solved Dhananjay's Magical Word using
python 2.7.6 and below is my code. However before looking in to code you must
understand the problem statement:
Code in Python 2.7:
Alphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
FinalPrimeAlphaASCII = []
#num = int(raw_input())
def isprimenumber(number):
flag = 0
for i in range(2,number/2 +1):
if number % i == 0:
flag = 1
break
if flag == 0:
return True
for Alpha in Alphabet:
if
isprimenumber(ord(Alpha)): FinalPrimeAlphaASCII.append(ord(Alpha))
magicalword = ''
magicalwords = []
NumberOfTestCases = int(raw_input())
while (NumberOfTestCases > 0):
N = int(raw_input())
userinput = raw_input()
for s in userinput: # Iterate through
each character in given string
if ord(s) <=
FinalPrimeAlphaASCII[0]: # Any character whose ASCI value less than or equal to 67
magicalword = magicalword +
chr(FinalPrimeAlphaASCII[0])
elif ord(s) >= FinalPrimeAlphaASCII[len(FinalPrimeAlphaASCII)-1]: # Any character
whose ASCI value greater than or equal to 89
magicalword = magicalword +
chr(FinalPrimeAlphaASCII[len(FinalPrimeAlphaASCII)-1])
elif ord(s) in
FinalPrimeAlphaASCII: # Any character whose ASCII value is prime number
magicalword += s
else:
i = 1
while i < len(FinalPrimeAlphaASCII):
#print ord(s)
if ord(s) < FinalPrimeAlphaASCII[i]:
#If the character is
equi-distant with 2 Magical alphabets.
#The one with lower
ASCII value will be considered as its replacement
if (ord(s) -
FinalPrimeAlphaASCII[i-1]) == (FinalPrimeAlphaASCII[i]-ord(s)):
magicalword
+=chr(FinalPrimeAlphaASCII[i-1])
break
elif
(FinalPrimeAlphaASCII[i]-ord(s)) < (ord(s) - FinalPrimeAlphaASCII[i-1]):
magicalword +=chr(FinalPrimeAlphaASCII[i])
break
else:
#Each character
should be replaced by the nearest Dhananjay's Magical alphabet.
magicalword
+=chr(FinalPrimeAlphaASCII[i-1])
break
i +=1
magicalwords.append(magicalword)
magicalword = ''
NumberOfTestCases -=1
for word in magicalwords: print word
No comments:
Post a Comment