Sunday, 30 April 2017

Sorting array list based on name using collections.sort

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class TestClass2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
        ArrayList<Person> list = new ArrayList<Person>();
        list.add(new Person("B"));
        list.add(new Person("A"));
        list.add(new Person("D"));
        list.add(new Person("C"));
        Collections.sort(list,new Comparator<Person>(){
@Override
public int compare(Person arg0, Person arg1) {
return arg0.name.compareToIgnoreCase(arg1.name);
}
       
        });
        for(Person person:list){
        System.out.println(person.name);
        }
}

}

class Person {
String name;
Person(String name){
this.name=name;
}
}

Parsing hash tags twitter trends

Twitter Trends
Twitter shows trends in order to make its users aware of the trending news. These trends are nothing but trending hashtags that the users are tweeting about. For example: If thousands of users are talking about United States by adding a hashtag #US in their tweet, then US will be a trending hashtag. Couple of example tweets with hashtag #US could be:

Donald Trump becomes the 45th #US President
Roger Federer wins #US Open for 5th time
Given a list of
N
N tweets, your task is to find top the five trending hashtags. Each tweet, let's call it
S
S, will contain at least one one word with hashtag. There will be maximum of three hashtags in any tweet. All hashtags in a single tweet will be unique.

Input:
First line of the input will contain   N
N denoting the number of tweets.
Next
N
N lines, each will contain a string S
S.

Output:
Print the top five trending hashtags. In case of tie between any two hashtags, print them in lexicographical order in a new line.

Note:

Any tweet is composed of lowercase and uppercase English letters, digits and spaces.
Any hashtag begins with # and the subsequent characters will only contain lowercase and uppercase English letters and digits.



Sample Input
10
Donald Trump becomes the 45th #US President
Potentially habitable exoplanet #ProximaB discovered
#RogerFederer wins #US Open for 5th time
#GravitationalWaves detection successful
Traces of liquid water discovered on #Mars
Life Could Survive on Exoplanet #ProximaB
Go go #RogerFederer
Ten ways #ProximaB is different from Earth
ISRO becomes 4th space agency to reach #Mars
#RogerFederer beats #Nadal



Sample Output
#ProximaB
#RogerFederer
#Mars
#US
#GravitationalWaves
Explanation
In the given sample test case, the count of occurrences of all the hashtags are:

#US: 2
#Mars: 2
#RogerFederer: 3
#ProximaB: 3
#Nadal: 1
#GravitationalWaves: 1

Sort them by count and in case of tie in count we sort them in lexicographical order of hashtag. It means that Since #RogerFederer and #ProximaB has equal count, we compare the strings and since #ProximaB is lexicographically smaller than #RogerFederer, #ProximaB will have precedence over #RogerFederer.

Final sorted order is:

#ProximaB: 3
#RogerFederer: 3
#Mars: 2
#US: 2
#GravitationalWaves: 1
#Nadal: 1

Top 5 hashtags from the above list is the correct output.


Code :- 

package com.deutsche.Domain;


//imports for BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;

//import for Scanner and other utility classes
import java.util.*;
import java.util.Map.Entry;

class TestClass {
  public static void main(String args[] ) throws Exception {
      //BufferedReader
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      HashMap<String,Integer> hashMap = new HashMap<String,Integer>();
      String line = br.readLine();
      for(int i=0;i<Integer.parseInt(line);i++){
          String line1= br.readLine();
          while(true){
          int index = line1.indexOf('#');
          if(index<0){
              break;
          }
          line1=line1.substring(index,line1.length());
          int spaceIndex = line1.indexOf(' ');
          if(spaceIndex<0){
         spaceIndex=line1.length();
          }
          String tag = line1.substring(0,spaceIndex);
          if(hashMap.containsKey(tag)){
               hashMap.put(tag,hashMap.get(tag)+1);
          }else{
         hashMap.put(tag,1);
          }
          if(spaceIndex==line1.length()){
         break;
          }
          line1 = line1.substring(spaceIndex+1,line1.length());
          }
      }
      Set<Entry<String, Integer>> set = hashMap.entrySet();
      List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
      Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
      {
          public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
          {
         if(o2.getValue() == o1.getValue()){
         return (o1.getKey()).compareToIgnoreCase (o2.getKey());
         }else{
                return (o2.getValue()).compareTo( o1.getValue());
         }
          }
      } );
      int count =0;
       for(Map.Entry<String, Integer> entry:list){
           if(count>5){
               break;
           }
          System.out.println(entry.getKey()+": "+entry.getValue());
          count=count+1;
      }
     
  }
}


Thursday, 6 April 2017

Find factorial for number which are larger than 64 bit in java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        BigInteger big = new BigInteger(String.valueOf(in.nextInt()));
        System.out.println(factorial(big));
    }
   
    static BigInteger factorial(BigInteger n){
        if(n == BigInteger.ONE || n== BigInteger.ZERO)
            return BigInteger.ONE;
        else
            return n.multiply(factorial(n.subtract(BigInteger.ONE)));
    }
}

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...