Skip to main content

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();
}

}

Comments

Popular posts from this blog

Terraform

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied. The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc. The key features of Terraform are: Infrastructure as Code : Infrastructure is described using a high-level configuration syntax. This allows a blueprint of your datacenter to be versioned and

Java 8 coding challenge: Roy and Profile Picture

Problem:  Roy wants to change his profile picture on Facebook. Now Facebook has some restriction over the dimension of picture that we can upload. Minimum dimension of the picture can be  L x L , where  L  is the length of the side of square. Now Roy has  N  photos of various dimensions. Dimension of a photo is denoted as  W x H where  W  - width of the photo and  H  - Height of the photo When any photo is uploaded following events may occur: [1] If any of the width or height is less than L, user is prompted to upload another one. Print " UPLOAD ANOTHER " in this case. [2] If width and height, both are large enough and (a) if the photo is already square then it is accepted. Print " ACCEPTED " in this case. (b) else user is prompted to crop it. Print " CROP IT " in this case. (quotes are only for clarification) Given L, N, W and H as input, print appropriate text as output. Input: First line contains  L . Second line contains  N , number of

Salt stack issues

The function “state.apply” is running as PID Restart salt-minion with command:  service salt-minion restart No matching sls found for ‘init’ in env ‘base’ Add top.sls file in the directory where your main sls file is present. Create the file as follows: 1 2 3 base: 'web*' : - apache If the sls is present in a subdirectory elasticsearch/init.sls then write the top.sls as: 1 2 3 base: '*' : - elasticsearch.init How to execute saltstack-formulas create file  /srv/pillar/top.sls  with content: base : ' * ' : - salt create file  /srv/pillar/salt.sls  with content: salt : master : worker_threads : 2 fileserver_backend : - roots - git gitfs_remotes : - git://github.com/saltstack-formulas/epel-formula.git - git://github.com/saltstack-formulas/git-formula.git - git://github.com/saltstack-formulas/nano-formula.git - git://github.com/saltstack-f