Java 8 coding challenge: Magical Word

Problem:

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 an 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

SAMPLE INPUT
1
6
AFREEN
SAMPLE OUTPUT
CGSCCO
Explanation

ASCII values of alphabets in AFREEN are 65, 70, 82, 69 ,69 and 78 respectively which are converted to CGSCCO with ASCII values 67, 71, 83, 67, 67, 79 respectively. All such ASCII values are prime numbers.

Code:

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
 

public class MagicalWord {
 static class Print {
 private final BufferedWriter bw;
 
 public Print() {
 this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
 }
 
 public void print(Object object) throws IOException {
 bw.append("" + object);
 }
 
 public void println(Object object) throws IOException {
 print(object);
 bw.append("\n");
 }
 
 public void close() throws IOException {
 bw.close();
 }
 }
 static class Reader {
 final private int BUFFER_SIZE = 1 << 16;
 private DataInputStream din;
 private byte[] buffer;
 private int bufferPointer, bytesRead;
 
 public Reader() {
 din = new DataInputStream(System.in);
 buffer = new byte[BUFFER_SIZE];
 bufferPointer = bytesRead = 0;
 }
 
 public String readLine() throws IOException {
 byte[] buf = new byte[100000]; // line length
 int cnt = 0, c;
 while ((c = read()) != -1) {
 if (c == '\n')
 break;
 buf[cnt++] = (byte) c;
 }
 return new String(buf, 0, cnt);
 }
 
 public int nextInt() throws IOException {
 int ret = 0;
 byte c = read();
 while (c <= ' ')
 c = read();
 boolean neg = (c == '-');
 if (neg)
 c = read();
 do {
 ret = ret * 10 + c - '0';
 } while ((c = read()) >= '0' && c <= '9');
 
 if (neg)
 return -ret;
 return ret;
 }
 
 private void fillBuffer() throws IOException {
 bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
 if (bytesRead == -1)
 buffer[0] = -1;
 }
 
 private byte read() throws IOException {
 if (bufferPointer == bytesRead)
 fillBuffer();
 return buffer[bufferPointer++];
 }
 
 public void close() throws IOException {
 if (din == null)
 return;
 din.close();
 }
 }
 
 public static void main(String[] args) throws Exception{
 Print pr = new Print();
 Reader rd = new Reader();
 
 
 Map<Character,Character> map = new HashMap<>();
 map.put('A','C');
 map.put('B','C');
 map.put('C','C');
 map.put('D','C');
 map.put('E','C');
 map.put('F','G');
 map.put('G','G');
 map.put('H','G');
 map.put('I','I');
 map.put('J','I');
 map.put('K','I');
 map.put('L','I');
 map.put('M','O');
 map.put('N','O');
 map.put('O','O');
 map.put('P','O');
 map.put('Q','O');
 map.put('R','S');
 map.put('S','S');
 map.put('T','S');
 map.put('U','S');
 map.put('V','S');
 map.put('W','Y');
 map.put('X','Y');
 map.put('Y','Y');
 map.put('Z','Y');
 map.put('a','a');
 map.put('b','a');
 map.put('c','a');
 map.put('d','e');
 map.put('e','e');
 map.put('f','e');
 map.put('g','g');
 map.put('h','g');
 map.put('i','g');
 map.put('j','k');
 map.put('k','k');
 map.put('l','k');
 map.put('m','m');
 map.put('n','m');
 map.put('o','m');
 map.put('p','q');
 map.put('q','q');
 map.put('r','q');
 map.put('s','q');
 map.put('t','q');
 map.put('u','q');
 map.put('v','q');
 map.put('w','q');
 map.put('x','q');
 map.put('y','q');
 map.put('z','q');
 
 map.put('\0','C');
 map.put('!','C');
 map.put('"','C');
 map.put('#','C');
 map.put('$','C');
 map.put('%','C');
 map.put('&','C');
 map.put('\'','C');
 map.put('(','C');
 map.put(')','C');
 map.put('*','C');
 map.put('+','C');
 map.put(',','C');
 map.put('-','C');
 map.put('.','C');
 map.put('/','C');
 map.put(':','C');
 map.put(';','C');
 map.put('<','C');
 map.put('=','C');
 map.put('>','C');
 map.put('?','C');
 map.put('@','C');
 map.put('[','Y');
 map.put('\\','Y');
 map.put(']','Y');
 map.put('^','a');
 map.put('_','a');
 map.put('`','a');
 map.put('{','q');
 map.put('|','q');
 map.put('}','q');
 map.put('~','q');
 
 map.put('0','C');
 map.put('1','C');
 map.put('2','C');
 map.put('3','C');
 map.put('4','C');
 map.put('5','C');
 map.put('6','C');
 map.put('7','C');
 map.put('8','C');
 map.put('9','C');
 
 int t = rd.nextInt();
 while(t-->0){
 int len = rd.nextInt();
 StringBuilder ip= new StringBuilder(rd.readLine());
 char op[] = new char[len];
 for(int i=0;i<len;i++){
 op[i] = map.get(ip.charAt(i));
 }
 pr.println(String.valueOf(op));
 }
 pr.close();
 }
 
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s