Monday 24 October 2016

String sum

The alphabets A...Z are mapped integers 65…90 (ASCII value of A...Z is 65...90) and a...z are mapped to 97…122(ASCII value of a…z is 97...122). The Beautiful function is a function that takes a character as an input and outputs an integer. The integer The Beautiful function returns is the biggest integer in the ASCII representation of the input character.

For example,
If 'A' is the input to the function, the function would return 6 because ASCII of A is 65 and 6 is the biggest among 6 and 5.
If 'C' is the input to the function, the function would return 7 because ASCII of C is 67 and 7 is the biggest among 6 and 7.
If 'x' is the input to the function, the function would return 2 because ASCII of x is 120 and 2 is the biggest among 1, 2 and 0.

Given a string as input, your task is to find out sum of all the values returned by The Beautiful Function.
** The input string may contain numeric values. In that case just add the numeric value. **

Input:
The first line of input will have number of test cases T. T < 10,000
Next T lines will have a string of length L. L < 1,000.

Output:
T lines, each line containing one integer.

Sample Input:
4
abcd
12
a12345b
azAZ09

Sample Output:
28
3
33
35

Explanation for the last test case:
a -> 9
z -> 2
A -> 6
Z -> 9
0 -> 0
9 -> 9
9 + 2 + 6 + 9 + 0 + 9 = 35

String manipulation

Problem Defination:
kriyapad is word present in sentence which shows
actions performed.
Darshil bein fan of kriyapad has prepared list of
kriyapad.
now Anand, Teacher of Darshil gave
assignment to Darshil find maximum number of
kriyapad can be prepared by rearranging given
sentence at a time.

INPUT:

input contain several lines which are kriyapad.
at the end input symbol in new line is shown
by '.' sign.
then there is one line which is number n-
number of sentence given by Anand to Darshil.
then n lines each line contain one sentence.

output:

output contain n lines, each line shows maximum
kriyapad can be formed by rearranging given
sentence

sample Input:
play
give
take
write
speak
listen
.
3
kavan is eating kakdi
Darshil is playing mini-militiae
layte canven wrispeak pgite lis

output:
1
2
6

Wednesday 19 October 2016

PALIN - The Next Palindrome (spoj solution)

A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.

Input

The first line contains integer t, the number of test cases. Integers K are given in the next t lines.

Output

For each K, output the smallest palindrome larger than K.

Example

Input: 
2
808
2133
Output:
818
2222

Solution in java:


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    static void p(Object o){ System.out.println(o);}
 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  int zz = sc.nextInt();
  for(int z = 0; z < zz;z++)
  {
   String s = sc.next();

   String ans = s.substring(0,s.length()/2);
   if(s.length()%2 == 1)
   {
    ans = ans + s.charAt((s.length()/2))+rev(ans);
   }else
    ans += rev(ans);
   if(larger(ans,s))
   {
    System.out.println(ans);
   }else{
    boolean done = false;
    for(int i = (s.length()-1)/2;i>=0;i--)
    {
     if(ans.charAt(i) != '9')
     {
      done = true;
      if(i == s.length()/2 && s.length()%2 == 1)
      {
       ans = ans.substring(0,s.length()/2)+((char)(ans.charAt(i)+1))+ans.substring(s.length()/2+1);
      }else{
       if(s.length()%2 == 1)
       {
        ans = ans.substring(0,i)+((char)(ans.charAt(i)+1))+zeros(s.length()/2 - i-1);
        ans = ans+"0"+rev(ans);
       }else{
        ans = ans.substring(0,i)+((char)(ans.charAt(i)+1))+zeros(s.length()/2 - i-1);
        ans = ans+rev(ans);
       }
      }
      break;
     }
    }
    if(done) System.out.println(ans);
    else
    {
     ans = "1";
     while(ans.length() < s.length())
      ans +="0";
     ans += "1";
     System.out.println(ans);
    }
   }
  }
 }

 private static String zeros(int i) {
  StringBuilder z = new StringBuilder();
  for(int j = 0; j < i;j++)
   z.append("0");
  return z.toString();
 }
 private static boolean larger(String ans, String s) {
  for(int i = 0; i < ans.length();i++)
  {
   char a = ans.charAt(i);
   char b = s.charAt(i);
   if(a < b) return false;
   if(a > b) return true;
  }
  return false;
 }

 private static String rev(String ans) {
  StringBuilder st = new StringBuilder(ans);
  return st.reverse().toString();
 }
}

Friday 10 July 2015

how to start competitive coding

hello friends,
                 I have seen many people confused about how to start competitive coding, and what are benefit of it and many more question related to competitive coding. me too was confused when i was in 1st year but now i loved competitive coding.
                     First of all i want to introduce few competitive sites
Spoj - http://www.spoj.com/
Top Coder - http://www.topcoder.com/
Project-Euler - https://projecteuler.net/
code chef- http://www.codechef.com/
Hacker Rank - https://www.hackerrank.com/
Hacker Earth - https://www.hackerearth.com/
these are the sites on which i used to do coding.
here are few links I am providing from where I cleared my doubt about competitive coding https://www.quora.com/What-was-Anudeep-Nekkantis-Competitive-Programming-strategy-to-become-35th-in-Global-ranking-in-just-6-7-months  read reply by anudeep nekkanti. I too suggest not directly just read algorithm try to implement it after reading it from 2-3 sources.Take part in coding event hosted on many competitive coding sites.
                  As in above link anudeep mention benefit of competitive coding,so Now lets talk about some competitive coding exam.I suggest you to apply for that at least once it really gives you great experience, If you really want to apply for competitive coding exam then first try online coding events on competitive coding sites. I have mostly taken part on many content on Hacker Earth-college challenge : https://www.hackerearth.com/college-challenges/ .
if you have enough practice in content then take part in content like
acm-icpc : http://www.codechef.com/icpc
ioi : http://www.ioinformatics.org/index.shtml

for icpc you should require knowledge of all standard algorithm. I suggest you to read this book all standard algorithm required for icpc are given in it and try to solve all problem given in it
  book_for_icpc

thanks for visit and read my post
"keep coding"

my profile
Hacker Earth : hacker earth_priyank
Spoj : spoj_priyank
Project Euler : project euler_priyank
Facebook : facebook_priyank