Sunday, December 15, 2013

Programming Challenges Question 1

Programming Challenges Question 

Write a program which will print the below structures according to the input provided to the program.
The program should accept 3 inputs in the form of numbers between 1 and 9, both inclusive (one number per line) and then generate the corresponding structures based on the input.
Suppose the following sequence of numbers is supplied to the program:
3
2
4

Then the output should be:
  1
 2 2
3 3 3
 1
2 2
   1
  2 2
 3 3 3
4 4 4 4

/**********************Code1 in JAVA******************************/
import java.io.*;
public class Main {
public static void main(String[] args) {
    try
    {
        int a[]= new int[3];
    InputStreamReader sr= new InputStreamReader(System.in);
    BufferedReader br= new BufferedReader(sr) ;
     System.out.print("Enter 3 Number:\n");
     for(int i=0;i<3;i++)
     {
      a[i] = Integer.parseInt(br.readLine());
     }
     for(int count=0;count<3;count++)
     {
        for(int i=1;i<=a[count];i++)
          {
             for(int j=a[count];j>i;j--)
             {
                 System.out.print(" ");
             }
             for(int k=1;k<=i;k++)
             {
              System.out.print(k+" ");
             }
             System.out.println();
          }
     }
    }
    catch(Exception ex)
    {
        System.out.println(ex);
    }
    }

    }


/******************************** Code2 in JAVA*********************************/
 import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
   int a,b,c;
 Scanner in = new Scanner(System.in);
 a = in.nextInt();
 b = in.nextInt();
 c = in.nextInt();
 int[] array={a,b,c};
    int k;
  for(int x=0; x<3; x++){
  int y = array[x];
  int z= y+1;
   for(int i=1;i<=y;i++){
   System.out.println();
   System.out.format("%"+z+"s", "");

    for(int j=1;j<=i;j++)
    {
    k=i;
    System.out.format("%"+1+"s", "");
    System.out.print(k);
    }
    z--;
   }
  }
   }
}



Programming Challenges Question 2

Programming Challenges Question 
Write a program which will accept a single pair of strings separated by a comma; the program should calculate the sum of ascii values of the characters of each string. The program should then subtract the sum of the ascii values of the second string from the sum of the ascii values of the first string.

Suppose the following input is given to the program: 

123ABC,456DEF

Then the sum of the ascii values of the characters in '123ABC' is 348 and in '456DEF' it is 366. The Difference between these numbers is 348 – 366 = -18
The corresponding output to be printed by the program is: 
-18

/*****************************Code in JAVA********************************/
import java.io.*;
public class Main {
public static void main(String[] args) {
 
    try
    {

    InputStreamReader sr= new InputStreamReader(System.in);
    BufferedReader br= new BufferedReader(sr) ;
    String str=br.readLine();
    String data[]= str.split(",");
    int sum[]= new int[data.length];
    for(int i=0;i<data.length;i++)
    {
        for(int j=0;j<data[i].length();j++)
        {
           sum[i]=sum[i]+ (int) data[i].charAt(j);
        }
     
    }
    int dif=sum[0];
    for(int i=1;i<sum.length;i++)
        {
             dif=dif-sum[i];
         }
    System.out.println("Diff :"+dif);
    }
    catch(Exception ex)
    {
        System.out.println(ex);
    }
  }
}



Programming Challenges Question 3

Programming Challenges Question 
Kermit, a frog hops in a particular way such that:
1. He hops 20cm in the first hop, 10cm in the second hop and 5cm in the third hop.
2. After three hops Kermit rests for a while and then again follows the same hopping pattern.
Calculate the total distance travelled by Kermit (in centimeters) for the provided number of hops. Exactly 4 numbers of hops will be provided to the program (one number per line) as per the below example.

Suppose the following number of hops is provided to the program:
4
6
3
5
Then the total distance covered should be displayed as follows:
55
70
35
65

/*********************Code in JAVA*******************************8/
import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
      int a,b,c,d;
  Scanner in = new Scanner(System.in);
  a = in.nextInt();
  b = in.nextInt();
  c = in.nextInt();
  d = in.nextInt();
  int[] array = {a,b,c,d};
  int k = 0;

  for(int i=0; i<array.length; i++){
  int n = array[i];
  int x = n/3;
  int y = n%3;
  int z = x*35;

   switch(y){
   case 0: k = 0 + z; break;
   case 1: k = 20 + z; break;
   case 2: k = 30 + z; break;
   case 3: k = 35 + z; break;
   }
  System.out.println(k);
  }
}
}




Programming Challenges Question 4

Programming Challenges Question 

Write a program to calculate the distance travelled by a car at different time intervals. The initial velocity of the car is 36 km/hr and the constant acceleration is 5 m/s2. 
The formula to calculate distance is:
Distance Travelled = u*t+((a*t*t)/2) 
where,
u = initial velocity of the car (36 km/hr)
a = acceleration of the car (5 m/s2)

t = time duration in seconds

The program should accept 2 time intervals as the input (one time interval per line) and print the distance traveled in meters by the car (one output per line). Definitions:
1 kilometer = 1000 meters
1 hour = 3600 seconds

Let us suppose following are the inputs supplied to the program
10
8
Then the output of the program will be
350
240

/*************************Code in JAVA**********************************/
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

int firstValue;
int secondValue;
   Scanner in = new Scanner(System.in);
    firstValue = in.nextInt();
 secondValue = in.nextInt();
 calculateDistance(firstValue,secondValue);
 }
    public static void calculateDistance(int t1,int t2){
int u = 10; // in m/s
int a = 5; // in m/s2
int distanceTravel1 = u*t1+((a*t1*t1)/2);
int distanceTravel2 = u*t2+((a*t2*t2)/2);
System.err.println(distanceTravel1);
System.err.println(distanceTravel2);
}
}

Programming Challenges Question 5

Programming Challenges Question 

Gen is an event organizer. He has received data regarding the participation of employees in two different events. 
Some employees have participated in only one event and others have participated in both events. Gen now needs to count the number of employees who have taken part in both events. The records received by Gen consist of employee ids, which are unique. 

Write a program that accepts the employee ids participating in each event (the first line relates to the first event and the second line relates to the second event). The program should print the number of common employee ids in both the events.

Suppose the following input is given to the program, where each line represents a different event:
1001,1002,1003,1004,1005
1106,1008,1005,1003,1016,1017,1112

Now the common employee ids are 1003 and 1005, so the program should give the output as:
2

/************************************************Code in JAVA********************************************************/
import java.io.*;
public class Main {
    public static void main(String[] args) {
    try{
       String data[]= new String[2];
       InputStreamReader sr= new InputStreamReader(System.in);
       BufferedReader br= new BufferedReader(sr);
       for(int i=0;i<2;i++)
       {
         data[i]=br.readLine();
       }

         String EventOneIds[]=data[0].split(",");
         String EventTwoIds[]=data[1].split(",");
         int totalCommonIds=0;
         for(int i=0;i<EventOneIds.length;i++)
         {
             for(int j=0;j<EventTwoIds.length;j++)
             {
                 if(EventOneIds[i].equals(EventTwoIds[j]))
                     totalCommonIds++;
             }
         }

         System.out.println( totalCommonIds);
        
        }
    catch(Exception ex)
    {
        System.out.println(ex.toString());
    }

 }

}




Programming Challenges Question 6

Programming Challenges Question 

Write a program which will take the year (yyyy) and the numeric sequence of the month (0-11) as its input. 
The program will return the day on which the 28th of that particular month and year falls. The input can consist of two year-month combinations, one combination per line.

The numeric sequence of months is as follows:
0 – Jan
1 – Feb
2 – March
and so on......

The format for supplying the input is:
1999-5
Where 1999 is the year and 5 is the numeric sequence of the month (corresponding to June). The program should display the day on which June 28, 1999 fell, and in this case the output will be MONDAY. 

The output should be displayed in uppercase letters.
Suppose the following INPUT sequence is given to the program:
1999-5
1998-6
Then the output should be:
MONDAY
TUESDAY

/****************************Code in JAVA*************************************/
import java.io.*;
import java.text.DateFormatSymbols;
import java.util.*;

public class Main {
    public static void main(String[] args) {
    try{
       String data[]= new String[2];
       InputStreamReader sr= new InputStreamReader(System.in);
       BufferedReader br= new BufferedReader(sr);
       for(int i=0;i<2;i++)
       {
         data[i]=br.readLine();
         Calendar cal=Calendar.getInstance();
         cal.set(Integer.parseInt(data[i].split("-")[0]), Integer.parseInt(data[i].split("-")[1]), 28);
         String dayName = new DateFormatSymbols().getWeekdays()[cal.get(Calendar.DAY_OF_WEEK)];
         System.out.println(dayName.toUpperCase());
        }
        }
    catch(Exception ex)
    {
        System.out.println(ex.toString());
    }

 }
}




Programming Challenges Question 7

Programming Challenges Question 
Write a program that accepts 10 student records (roll number and score) and prints them in decreasing order of scores. In case there are multiple records pertaining to the same student, the program should choose a single record containing the highest score. The program should be capable of accepting a multi-line input. Each subsequent line of input will contain a student record, that is, a roll number and a score (separated by a hyphen). The output should consist of the combination of roll number and corresponding score in decreasing order of score.

INPUT to program

1001-40
1002-50
1003-60
1002-80
1005-35
1005-55
1007-68
1009-99
1009-10
1004-89

OUTPUT from program

1009-99
1004-89
1002-80
1007-68
1003-60
1005-55
1001-40

Solution

//******************************Code in JAVA*************************************//
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
       String Data[]= new String[10];
       int splitData[][]= new int[10][2];

       InputStreamReader sr= new InputStreamReader(System.in);
       BufferedReader br= new BufferedReader(sr) ;
       try{
          for(int i=0;i<10;i++)
          {
           Data[i]=br.readLine();
           String str[]= Data[i].split("-");
           splitData[i][0]=Integer.parseInt(str[1]);
           splitData[i][1]=Integer.parseInt(str[0]);
          }

     Arrays.sort(splitData, new Comparator<int[]>() {
            public int compare(int[] o1, int[] o2) {
            return o2[0] - o1[0];
        }
    });

         Byte flag=0;  
          for(int i=0;i<10;i++)
          {
            flag=0;
            for(int j=i-1;j>=0;j--)
            {
                if(splitData[i][1]==splitData[j][1])
                {
                  flag=1;
                   break;
                }
            }
            if(flag==1)
                continue;
            else
                System.out.println(splitData[i][1]+"-"+splitData[i][0]);
}
      }
      catch(Exception ex)
      {
          System.out.println(ex.toString());
      }
     
      }

}