Wednesday, March 20, 2013

The Great Indian Programming League 2013 - March Edition Level:Medium

Here is the Link of question and description of 

Four Persons


I am providing you the solution of the above question.


Mybest score on this is 99.99.



Repost Code with better one.(Download COde)
cvcv

____________________________________________________________



 

  1. public class InterchangePosition {


  2. /**

  3. * @param args

  4. */

  5. public static void main(String[] args) {

  6. // TODO Auto-generated method stub

  7. String []arr = {"5#1","6#1","5#3"};

  8. long x1 = System.currentTimeMillis();

  9. System.out.println(get_height(arr));

  10. long x2 = System.currentTimeMillis();

  11. System.out.println("TIme in milli "+(x2-x1));

  12. }

  13. public static int get_height(String[] input1)

  14.     {

  15.         //Write code here


  16. int count=0;


  17. int f[] = new int[input1.length];


  18. int value=0;

  19. String s[]= new String[2];


  20. if(input1.length==0)

  21. return 0;


  22. for(int i=0;i

  23. {


  24. if(Integer.parseInt(((s=input1[i].split("#"))[0]))<4 || Integer.parseInt(s[0])>7

  25. || Integer.parseInt(s[1])<0 || Integer.parseInt(s[1])>11

  26. )

  27. {

  28. //System.out.println("boom1");

  29. return -1;

  30. }



  31. f[i] = (Integer.parseInt(s[0])) * 12 +  Integer.parseInt(s[1]);



  32. }



  33. for(int i : f)

  34. {

  35. System.out.println(""+i);

  36. }

  37. System.out.println("--------------");



  38. for(int i=0;i<f.length;i++)

  39. {

  40. for(int j=i+1;j<f.length;j++)

  41. {



  42. if(f[i]>f[j])

  43. {

  44. //System.out.println("I am in "+f[i]);

  45. count++;

  46. }

  47. }

  48. }

  49. return count;

  50.     }


  51. }


____________________________________________________________

Another One
Download Code: Click Here


Mirror: Click Here

___________________________________________________________


public class InterchangePosition {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String []arr = {"5#5","6#6","5#5","6#6","5#11"};
long x1 = System.currentTimeMillis();
System.out.println(get_height(arr));
long x2 = System.currentTimeMillis();
System.out.println("TIme in milli "+(x2-x1));
}
public static int get_height(String[] input1)
{
//Write code here

int count=0;

int f[] = new int[input1.length];

int value=0;


if(input1.length==0)
return 0;

try
{
for(int i=0;i{

if(Integer.parseInt(input1[i].substring(0, input1[i].indexOf('#')))<4 ||
Integer.parseInt(input1[i].substring(0, input1[i].indexOf('#')))>7)
{
//System.out.println("boom1");
return -1;
}
if(Integer.parseInt(input1[i].substring(input1[i].indexOf('#')+1))<0 ||
Integer.parseInt(input1[i].substring(input1[i].indexOf('#')+1))>11)
{
//System.out.println("boom2");
return -1;
}

value = (Integer.parseInt(input1[i].substring(0, input1[i].indexOf('#')))) * 12 +  Integer.parseInt(input1[i].substring(input1[i].indexOf('#')+1));
f[i] = value;

}
}
catch(NumberFormatException nf)
{
nf.printStackTrace();
return -1;
}


for(int i : f)
{
System.out.println(""+i);
}
System.out.println("--------------");


for(int i=0;i<f.length;i++){
for(int j=i+1;j<f.length;j++){


if(f[i]>f[j])
{
//System.out.println("I am in "+f[i]);
count++;
}
}
}
return count;
}

}





___________________________________________________________

Download Code: Click Here

Mirror: Click Here






The Great Indian Programming League 2013 - March Edition Level:Medium

Here is the Link of question and description of 

Four Persons

I am providing you the solution of the above question. My best score on this is 99.99.

Repost Code with better one.(Download COde)


____________________________________________________________




  1. public class InterchangePosition {

  2. /**
  3. * @param args
  4. */
  5. public static void main(String[] args) {
  6. // TODO Auto-generated method stub
  7. String []arr = {"5#1","6#1","5#3"};
  8. long x1 = System.currentTimeMillis();
  9. System.out.println(get_height(arr));
  10. long x2 = System.currentTimeMillis();
  11. System.out.println("TIme in milli "+(x2-x1));
  12. }
  13. public static int get_height(String[] input1)
  14.     {
  15.         //Write code here
  16. int count=0;
  17. int f[] = new int[input1.length];
  18. int value=0;
  19. String s[]= new String[2];
  20. if(input1.length==0)
  21. return 0;
  22. for(int i=0;i
  23. {
  24. if(Integer.parseInt(((s=input1[i].split("#"))[0]))<4 || Integer.parseInt(s[0])>7
  25. || Integer.parseInt(s[1])<0 || Integer.parseInt(s[1])>11
  26. )
  27. {
  28. //System.out.println("boom1");
  29. return -1;
  30. }
  31. f[i] = (Integer.parseInt(s[0])) * 12 +  Integer.parseInt(s[1]);
  32. }
  33. for(int i : f)
  34. {
  35. System.out.println(""+i);
  36. }
  37. System.out.println("--------------");
  38. for(int i=0;i<f.length;i++)
  39. {
  40. for(int j=i+1;j<f.length;j++)
  41. {
  42. if(f[i]>f[j])
  43. {
  44. //System.out.println("I am in "+f[i]);
  45. count++;
  46. }
  47. }
  48. }
  49. return count;
  50.     }

  51. }


____________________________________________________________

Another One
Download Code: Click Here 
Mirror: Click Here
___________________________________________________________


public class InterchangePosition {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String []arr = {"5#5","6#6","5#5","6#6","5#11"};
long x1 = System.currentTimeMillis();
System.out.println(get_height(arr));
long x2 = System.currentTimeMillis();
System.out.println("TIme in milli "+(x2-x1));
}
public static int get_height(String[] input1)
    {
        //Write code here

int count=0;

int f[] = new int[input1.length];

int value=0;


if(input1.length==0)
return 0;

try
{
for(int i=0;i {

if(Integer.parseInt(input1[i].substring(0, input1[i].indexOf('#')))<4 ||
  Integer.parseInt(input1[i].substring(0, input1[i].indexOf('#')))>7)
{
//System.out.println("boom1");
return -1;
}
if(Integer.parseInt(input1[i].substring(input1[i].indexOf('#')+1))<0 ||
  Integer.parseInt(input1[i].substring(input1[i].indexOf('#')+1))>11)
{
//System.out.println("boom2");
return -1;
}

value = (Integer.parseInt(input1[i].substring(0, input1[i].indexOf('#')))) * 12 +  Integer.parseInt(input1[i].substring(input1[i].indexOf('#')+1));
f[i] = value;

}
}
catch(NumberFormatException nf)
{
nf.printStackTrace();
return -1;
}


for(int i : f)
{
System.out.println(""+i);
}
System.out.println("--------------");


for(int i=0;i
<f.length;i++) {
for(int j=i+1;j<f.length;j++) {


if(f[i]>f[j])
{
//System.out.println("I am in "+f[i]);
count++;
}
}
}
return count;
    }

}



___________________________________________________________
Download Code: Click Here 
Mirror: Click Here



Monday, March 11, 2013

TechGig: The Great Indian Programming League 2013 - Jan Edition


Fashion Contest 2013


My Score: 99.99 
Test Cases Passed: 8

import java.io.*;
  1. public class CandidateCode
  2. {
  3. public static int fashion(int[] input1)
  4. {
  5. //Write code here
  6. int totalJudges = input1.length;
  7. if(totalJudges==0)
  8. return 0;
  9. else if(totalJudges==1)
  10. return input1[0];
  11. else
  12. {
  13. int centerJudge = (totalJudges+1)/2-1;
  14. int centerJudgeScore = input1[centerJudge];
  15. for(int i=0,j=1;i
  16. {
  17. if(input1[centerJudge-j]==input1[centerJudge+j])
  18. centerJudgeScore+=input1[centerJudge+j];
  19. }
  20. return centerJudgeScore;
  21. }
  22. }
  23. }

TechGig: The Great Indian Programming League 2013 - Jan Edition


Fashion Contest 2013


My Score: 99.99 
Test Cases Passed: 8

import java.io.*;
  1. public class CandidateCode
  2. {
  3. public static int fashion(int[] input1)
  4. {
  5. //Write code here
  6. int totalJudges = input1.length;
  7. if(totalJudges==0)
  8. return 0;
  9. else if(totalJudges==1)
  10. return input1[0];
  11. else
  12. {
  13. int centerJudge = (totalJudges+1)/2-1;
  14. int centerJudgeScore = input1[centerJudge];
  15. for(int i=0,j=1;i
  16. {
  17. if(input1[centerJudge-j]==input1[centerJudge+j])
  18. centerJudgeScore+=input1[centerJudge+j];
  19. }
  20. return centerJudgeScore;
  21. }
  22. }
  23. }

Java Utility Functions - Password Generation and Validation

Function 1: Generate a random password that's 7 characters. The random password MUST have 1 lowercase, 1 uppercase, 1 digit, and 1 symbol (valid symbols are ! @ # $ % *

Method signature should be
public static String generatePassword()

Function 2: Validate a passed in password. This password will be between 7 and 20 characters. It must comply with at least 3 of the following 4 rules: 1) at least 1 capital 2) at least one lowercase 3) at least 1 digit 4) at least 1 symbol (see above for valid symbols). If the password is too long or too short, return false. If it only meets 2 of the rules, return false. If it meets 3 or 4 of the rules, return true.

Method signature should be
public static boolean validatePassword(String password)



  1. package password;

  2. import java.util.Arrays;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.Random;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;

  8. public class RandomPassword
  9. {
  10. private static String password;
  11. // Method for gnerating random password each time
  12. public static String generatePassword()
  13. {
  14. password="";
  15. String temp[] = new String[7];
  16. char []number = new char[]{'0','1','2','3','4','5','6','7','8','9'};
  17. char []lowerCase = new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  18. char []upperCase = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  19. char []symbol = new char[]{'!','@','#','$','%','*'};
  20. char []allCase = new char[]{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
  21. 'q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U',
  22. 'V','W','X','Y','Z','!','@','#','$','%','*'};
  23. Random random = new Random();
  24. int position = random.nextInt(10);
  25. temp[0] = ""+number[position];
  26. position = random.nextInt(26);
  27. temp[1] = ""+lowerCase[position];
  28. position = random.nextInt(26);
  29. temp[2] = ""+upperCase[position];
  30. position = random.nextInt(6);
  31. temp[3] = ""+symbol[position];
  32. for(int i=0;i3>
  33. {
  34. position = random.nextInt(68);
  35. temp[4+i] = "" +allCase[position];
  36. }
  37. // Suffle Your password
  38. Collections.shuffle(Arrays.asList(temp));
  39. // temp[] to String
  40. for(String pass : temp)
  41. password=password+pass;
  42. return password;
  43. }
  44. //Method for validate password
  45. public static boolean validatePassword(String password)
  46. {
  47. int flag=0;
  48. // Validate Length 
  49. if(password.length()20)7>
  50. return false;
  51. Pattern p = Pattern.compile("[a-z]");
  52. Matcher m = p.matcher(password);
  53. while(m.find())
  54. {
  55. flag++;
  56. break;
  57. }

  58. p = Pattern.compile("[A-Z]");
  59. m = p.matcher(password);
  60. while(m.find())
  61. {
  62. flag++;
  63. break;
  64. }
  65.  
  66. p = Pattern.compile("\\d");
  67. m = p.matcher(password);
  68. while(m.find())
  69. {
  70. flag++;
  71. break;
  72. }
  73.  
  74. p = Pattern.compile("['!','@','#','$','$',''%','*']");
  75. m = p.matcher(password);
  76. while(m.find())
  77. {
  78. flag++;
  79. break;
  80. }
  81.  
  82.  
  83. if(flag==0 || flag==1 || flag==2)
  84. return false;
  85.  
  86. if(flag==3 || flag==4)
  87. return true;
  88.  
  89. return true;
  90. }
  91. public static void main(String[] args) {
  92. System.out.println("passwors id "+RandomPassword.generatePassword());
  93. System.out.println(RandomPassword.validatePassword("aaaAaa)aa"));
  94. }
  95. }
Or

Java Utility Functions - Password Generation and Validation

Function 1: Generate a random password that's 7 characters. The random password MUST have 1 lowercase, 1 uppercase, 1 digit, and 1 symbol (valid symbols are ! @ # $ % *

Method signature should be
public static String generatePassword()

Function 2: Validate a passed in password. This password will be between 7 and 20 characters. It must comply with at least 3 of the following 4 rules: 1) at least 1 capital 2) at least one lowercase 3) at least 1 digit 4) at least 1 symbol (see above for valid symbols). If the password is too long or too short, return false. If it only meets 2 of the rules, return false. If it meets 3 or 4 of the rules, return true.

Method signature should be
public static boolean validatePassword(String password)



  1. package password;

  2. import java.util.Arrays;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.Random;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;

  8. public class RandomPassword
  9. {
  10. private static String password;
  11. // Method for gnerating random password each time
  12. public static String generatePassword()
  13. {
  14. password="";
  15. String temp[] = new String[7];
  16. char []number = new char[]{'0','1','2','3','4','5','6','7','8','9'};
  17. char []lowerCase = new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  18. char []upperCase = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  19. char []symbol = new char[]{'!','@','#','$','%','*'};
  20. char []allCase = new char[]{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
  21. 'q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U',
  22. 'V','W','X','Y','Z','!','@','#','$','%','*'};
  23. Random random = new Random();
  24. int position = random.nextInt(10);
  25. temp[0] = ""+number[position];
  26. position = random.nextInt(26);
  27. temp[1] = ""+lowerCase[position];
  28. position = random.nextInt(26);
  29. temp[2] = ""+upperCase[position];
  30. position = random.nextInt(6);
  31. temp[3] = ""+symbol[position];
  32. for(int i=0;i<3 i="" span="">
  33. {
  34. position = random.nextInt(68);
  35. temp[4+i] = "" +allCase[position];
  36. }
  37. // Suffle Your password
  38. Collections.shuffle(Arrays.asList(temp));
  39. // temp[] to String
  40. for(String pass : temp)
  41. password=password+pass;
  42. return password;
  43. }
  44. //Method for validate password
  45. public static boolean validatePassword(String password)
  46. {
  47. int flag=0;
  48. // Validate Length 
  49. if(password.length()<7 password.length="">20)
  50. return false;
  51. Pattern p = Pattern.compile("[a-z]");
  52. Matcher m = p.matcher(password);
  53. while(m.find())
  54. {
  55. flag++;
  56. break;
  57. }

  58. p = Pattern.compile("[A-Z]");
  59. m = p.matcher(password);
  60. while(m.find())
  61. {
  62. flag++;
  63. break;
  64. }
  65.  
  66. p = Pattern.compile("\\d");
  67. m = p.matcher(password);
  68. while(m.find())
  69. {
  70. flag++;
  71. break;
  72. }
  73.  
  74. p = Pattern.compile("['!','@','#','$','$',''%','*']");
  75. m = p.matcher(password);
  76. while(m.find())
  77. {
  78. flag++;
  79. break;
  80. }
  81.  
  82.  
  83. if(flag==0 || flag==1 || flag==2)
  84. return false;
  85.  
  86. if(flag==3 || flag==4)
  87. return true;
  88.  
  89. return true;
  90. }
  91. public static void main(String[] args) {
  92. System.out.println("passwors id "+RandomPassword.generatePassword());
  93. System.out.println(RandomPassword.validatePassword("aaaAaa)aa"));
  94. }
  95. }
Or

Saturday, March 9, 2013

TechGig: The Great Indian Programming League 2013 - March Edition - Level 1

Hi,
I am providing the solution of  The Great Indian Programming League 2013 - March Edition - Level 1.  You can find the question here . My best score in this level is 90.



  1. public class CandidateCode {
  2. public static int slope_sum(int input1,int input2)
  3. {
  4. //Write code here
  5. int sum=0;
  6. long cal=0;
  7. if(input1>=0 && input2<=100)
  8. return 0;
  9. else if(input1>input2 || input12147483647)0>
  10. {
  11. return -1;
  12. }

  13. else
  14. {
  15. for(int i=input1;i<=input2;i++)
  16. {
  17. if(i>100)
  18. {
  19. //System.out.println("hiii"+getValue(i));
  20. sum = sum + getValue(i);
  21. System.out.println(sum);
  22. cal++;
  23. }
  24. }
  25. }
  26. return sum;
  27. }

  28. public static int getValue(int value)
  29. {
  30. int maxima=0;
  31. int lastDigit;
  32. int firstDigit;
  33. int middleDigit;
  34. int rem,flag=0;

  35. while(value!=0)
  36. {
  37. int temp=-1; 
  38. rem = value%1000;
  39. temp=value/1000;
  40. if(temp==0)
  41. {

  42. //System.out.println("rem");
  43. rem=value;

  44. }
  45. lastDigit = rem%10;
  46. firstDigit = rem/100;
  47. middleDigit = (rem%100)/10;

  48. //System.out.println("Rem = "+rem+" last Digit "+lastDigit+" firstDigit "+firstDigit+" middle = "+middleDigit);
  49. // System.out.println("Rem = "+rem+" last Digit "+lastDigit+" firstDigit "+firstDigit+" middle = "+middleDigit+" maxima= "+maxima);
  50. if(middleDigit>firstDigit && middleDigit>lastDigit)
  51. {
  52. maxima++;
  53. }
  54. if (middleDigit
  55. maxima++;
  56. }
  57. if(temp==0)
  58. {
  59. //System.out.println("Here 3 digit code");
  60. flag=maxima;
  61. maxima=0;

  62. value=0;
  63. }
  64. else
  65. {
  66. value = value/10;
  67. }
  68. }
  69. return flag;
  70. }
  71. }

TechGig: The Great Indian Programming League 2013 - March Edition - Level 1

Hi,
I am providing the solution of  The Great Indian Programming League 2013 - March Edition - Level 1.  You can find the question here . My best score in this level is 90.



  1. public class CandidateCode {
  2. public static int slope_sum(int input1,int input2)
  3. {
  4. //Write code here
  5. int sum=0;
  6. long cal=0;
  7. if(input1>=0 && input2<=100)
  8. return 0;
  9. else if(input1>input2 || input1<0 input2="">2147483647)
  10. {
  11. return -1;
  12. }

  13. else
  14. {
  15. for(int i=input1;i<=input2;i++)
  16. {
  17. if(i>100)
  18. {
  19. //System.out.println("hiii"+getValue(i));
  20. sum = sum + getValue(i);
  21. System.out.println(sum);
  22. cal++;
  23. }
  24. }
  25. }
  26. return sum;
  27. }

  28. public static int getValue(int value)
  29. {
  30. int maxima=0;
  31. int lastDigit;
  32. int firstDigit;
  33. int middleDigit;
  34. int rem,flag=0;

  35. while(value!=0)
  36. {
  37. int temp=-1; 
  38. rem = value%1000;
  39. temp=value/1000;
  40. if(temp==0)
  41. {

  42. //System.out.println("rem");
  43. rem=value;

  44. }
  45. lastDigit = rem%10;
  46. firstDigit = rem/100;
  47. middleDigit = (rem%100)/10;

  48. //System.out.println("Rem = "+rem+" last Digit "+lastDigit+" firstDigit "+firstDigit+" middle = "+middleDigit);
  49. // System.out.println("Rem = "+rem+" last Digit "+lastDigit+" firstDigit "+firstDigit+" middle = "+middleDigit+" maxima= "+maxima);
  50. if(middleDigit>firstDigit && middleDigit>lastDigit)
  51. {
  52. maxima++;
  53. }
  54. if (middleDigit
  55. maxima++;
  56. }
  57. if(temp==0)
  58. {
  59. //System.out.println("Here 3 digit code");
  60. flag=maxima;
  61. maxima=0;

  62. value=0;
  63. }
  64. else
  65. {
  66. value = value/10;
  67. }
  68. }
  69. return flag;
  70. }
  71. }

Calculate time to complete a method or a block of code in Java.

There is no direct way to calculate the time of a mehtod which is in execution. But there is a method to calculate current time by which we can approx calculate the total  time.

Class : System
Method : currentTimeMillis

Use:


  1. public static void main(String[] args) {
  2. long startTime = System.currentTimeMillis();
  3. System.out.println(method(some_value));
  4. long endTime = System.currentTimeMillis();
  5. long totalTime = endTime - startTime;
  6. System.out.println("total time in milli seconds "(totalTime);
  7. }



Calculate time to complete a method or a block of code in Java.

There is no direct way to calculate the time of a mehtod which is in execution. But there is a method to calculate current time by which we can approx calculate the total  time.

Class : System
Method : currentTimeMillis

Use:


  1. public static void main(String[] args) {
  2. long startTime = System.currentTimeMillis();
  3. System.out.println(method(some_value));
  4. long endTime = System.currentTimeMillis();
  5. long totalTime = endTime - startTime;
  6. System.out.println("total time in milli seconds "(totalTime);
  7. }