Search This Blog

Sunday, September 1, 2013

Hacker Rank Problem - Regex - HackerRank Tweets

HackerRank Tweets

The following is the solution to the Hacker Rank problem "HackerRank Tweets".

/**
 * @author Arun.G
 *
 */
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) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
         Scanner sc = new Scanner(System.in);
        int number = sc.nextInt(); int count=0;
        while(sc.hasNextLine())
        {
           String line = sc.nextLine();
           Pattern hiPattern = Pattern.compile("hackerrank",Pattern.CASE_INSENSITIVE);
           Matcher m = hiPattern.matcher(line);
           while(m.find())
           {
               count++;
           }              
        }
        System.out.println(count);
    }
}

Hacker Rank Problem - Regex - Saying Hi

Saying Hi

The following is the solution to the Hacker Rank problem "Saying Hi". For other Hacker Rank problem solutions visit my Hacker Rank Solutions Page.

/**
 * @author Arun.G
 *
 */
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) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
       
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        while(sc.hasNextLine())
        {
            String line = sc.nextLine();
            Pattern hiPattern = Pattern.compile("^(h|H){1}(I|i){1} [^d]");
            Matcher m = hiPattern.matcher(line);
            
            if(m.find())
                System.out.println(line);
        }
    }
}

Hacker Rank Problem - Regex - Valid PAN format

Valid PAN format

The following is the solution to the Hacker Rank problem "Valid PAN format". For other Hacker Rank problem solutions visit my Hacker Rank Solutions Page.

/**
 * @author Arun.G
 *
 */
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) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int numberOfPans = sc.nextInt();
       
        while(numberOfPans-- >=0)
        {
            String pan = sc.nextLine();
            if(!pan.equals(""))
            {
               Pattern panPattern = Pattern.compile("\\b[A-Z]{5}\\d{4}[A-Z]{1}");
                Matcher m = panPattern.matcher(pan);
                if(m.find())
                    System.out.println("YES");
                else
                    System.out.println("NO");
            }
           
        }
    }
}

Hacker Rank Problem - Regex - The British and American Style of Spelling

The British and American Style of Spelling

The following is the solution to the Hacker Rank problem "The British and American Style of Spelling". For other Hacker Rank problem solutions visit my Hacker Rank Solutions Page.

/**
 * @author Arun.G
 *
 */
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) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc= new Scanner(System.in);
        int numberOfLines = sc.nextInt();
        String line ="";
        int count=0;
        //getting all the lines in String
        while(numberOfLines-- >=0)
        {
          line+=(" "+sc.nextLine()); 
        }
        int numberOfTestCases=sc.nextInt();
       
       //processing test cases one by one
        while(numberOfTestCases-- >=0)
        {
          
            String americanword =sc.nextLine();
            count=0;
            Pattern ra = Pattern.compile(americanword);
            String pattern = americanword.replace("z","s");
       
            if(!pattern.equals(""))
            {
            Pattern rb = Pattern.compile(pattern);
          // Now create matcher object.
          Matcher ma = ra.matcher(line);
           Matcher mb = rb.matcher(line);
             
              while(ma.find())
              {
                 count++;
              }
              while(mb.find())
              {
                 count++;
              }
                System.out.println(count);
            }
           
        }
      
    }
}



Hacker Rank Problem - Search - Pairs

Pairs

The following is the solution to the Hacker Rank problem "Pairs". For other Hacker Rank problem solutions visit my Hacker Rank Solutions Page.
Scored: 39/39
/**
 * @author Arun.G
 *
 */

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.Scanner;
public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc=new Scanner(System.in);
        int n=0,k=0,count=0;
        n= sc.nextInt();
        k=sc.nextInt();
        ArrayList<Integer> list = new ArrayList<Integer>();
        while(sc.hasNextInt())
            list.add(sc.nextInt());
       
        int numa=0,numb=0;
        Collections.sort(list);
        for(int i=0;i<list.size();i++)
        {
            numa = (int)list.get(i);
            for(int j=i+1;j<list.size();j++)
            {
              numb = (int)list.get(j);
              if(numa<numb)
              {
                if(Math.abs(numa-numb)==k)
                    count++;
                if(Math.abs(numa-numb)>k)
                    break;
              }
              else
              {
                j=i;
              }
            }
                   
        }
        System.out.println(count);
        sc.close();
    }

}


Hacker Rank Problem - Regex - Find Hackerrank

Find Hackerrank

The following is the solution to the Hacker Rank problem "Find Hackerrank". For other Hacker Rank problem solutions visit my Hacker Rank Solutions Page.

/**
 * @author Arun.G
 *
 */

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.Scanner;
public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
         Scanner sc = new Scanner(System.in);
        String conversation="";
        int numberOfLines=sc.nextInt();
        conversation=sc.nextLine();
        while((sc.hasNextLine()))
        {
            conversation=sc.nextLine();
            // System.out.println(conversation);
            if(conversation.startsWith("hackerrank") &&    conversation.endsWith("hackerrank"))
                System.out.println("0");
            else if(conversation.startsWith("hackerrank"))
                System.out.println("1");
            else if(conversation.endsWith("hackerrank"))
                System.out.println("2");
            else
                System.out.println("-1");
        }
       
    }
}





























WordPress Plugin

Display Top 10 Popular Posts Widget

Display Top 10 Popular Posts is a WordPress Widget that displays the top 10 popular posts in duration of 7 days, 1 month or 1 year based on data from Google Analytics.
You can download the source code in GitHub - Display-Top-10-Posts-Widgets
Instructions:
The following are the instructions to set up WordPress plugin.
       1)      Copy the “DisplayTop10PostsWidgets” to word press plug-in directory (“Install_Folder/wordpress/wp-content/plugins”)

       2)      Login to Wordpress Admin (localhost/wordpress/wp-admin)


       3)      The Admin site for your Word Press blog will open. Click on the Plugins->Installed Plugins

 

       4)      The Plug-in ‘Display Top 10 Posts Widget’ will be displayed. Click on the Activate link.


        5)      Plug-in Activated will be displayed.
        6)      Now go to the Widgets by clicking on Appearance-> Widgets.
                

         7)      Now Drag and drop the Widget from the ‘Available Widgets’ to the ‘Main Side Bar’
          8)      Now fill the required settings (Google Analytics Email, Google Analytics Password, Google Analytics           Profile Id, Max Results and Time Duration) and click save.

         9)      You can drag and drop the widget how many times you want and configure for different Duration.

        10)   Next Open a word press page and the Widget will display the top 10 posts based on duration configured.
Please find the screen shot below:

Common Help:

1)      If the Widget displays “No Posts Available” it means no analytics data was returned for the particular time duration.
2)      If the Widget displays “Could not get Report Data. Error Occurred. Please verify the settings are right” it means there is something wrong with the settings
i)                    Verify  whether Gmail account is right
ii)                   Verify whether password is right

iii)                 Verify whether Profile Id is right. (profile id will be in the form of 8 digits)

You can download the source code in GitHub - Display-Top-10-Posts-Widgets

Labels