Thursday, December 25, 2014

Java program to find the common interval between the meeting intervals of two persons


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Meeting {
 private static List<Double> PS1; // Person 1's Meeting Start times.
 private static List<Double> PE1; // Person 1's Meeting End times.
 private static List<Double> PS2; // Person 2's Meeting Start times.
 private static List<Double> PE2; // Person 2's Meeting End times.
 private static Double maxTime;
 public static void main(String[] args) {
  //TC 1
//  PS1 = Arrays.asList(0D,8D);
//  PE1 = Arrays.asList(1D,23D);
//  PS2 = Arrays.asList(0D,3D,5D,9D);
//  PE2 = Arrays.asList(2D,4D,6D,23D);
  
  //TC 2
  PS1 = Arrays.asList(1D);
  PE1 = Arrays.asList(2D);
  PS2 = Arrays.asList(2D);
  PE2 = Arrays.asList(3D);
  
  //TC3
  PS1 = Arrays.asList(1D,10D,19D,21D,27D);
  PE1 = Arrays.asList(5D,14D,20D,23D,30D);
  PS2 = Arrays.asList(3D,12D,18D,23D);
  PE2 = Arrays.asList(5D,15D,21D,24D);
  
  maxTime = PE1.get(PE1.size() - 1) > PE2.get(PE2.size() - 1) ? PE1.get(PE1.size() - 1) : PE2.get(PE2.size() - 1); 
  
  FreeTimeSlot P1 = findFreeTimeSlot(PS1,PE1);
  System.out.println("Free time slots for person 1 :");
  printTimeSlot(P1);
  
  FreeTimeSlot P2 = findFreeTimeSlot(PS2,PE2);
  System.out.println("Free time slots for person 2 :");
  printTimeSlot(P2);

  FreeTimeSlot commonFreeTimeSlot = findCommonFreeTimeSlot(P1, P2);
  System.out.println("Common free time slots for person 1 and person 2 :");
  printTimeSlot(commonFreeTimeSlot);
 }
 
 public static void printTimeSlot(FreeTimeSlot timeSlot) {
  int i=0;
  for(Double time : timeSlot.S) {
   System.out.println(time + " " + timeSlot.E.get(i));
   i++;
  }
 }
 
 public static FreeTimeSlot findCommonFreeTimeSlot(FreeTimeSlot P1, FreeTimeSlot P2) {
  FreeTimeSlot commonFreeTimeSlot = new FreeTimeSlot();
  int a = 0; int b = 0;
  //a is the index for Person 1
  //b is the index for Person 2
  while (a < P1.S.size() && b < P2.S.size()) {
   //Person 1's time slot lies completely ahead of Person 2's time slot
   /*
    *        a                    b
    * +-----------------+   +----------------+
    */
   if(P1.S.get(a) < P2.S.get(b) && P1.E.get(a) <= P2.S.get(b)) {
    a++;
   }
   //Person 2's time slot lies completely ahead of Person 1's time slot
   /*
    *        b                    a
    * +-----------------+   +----------------+
    */
   else if(P1.S.get(a) > P2.S.get(b) && P1.S.get(a) >= P2.E.get(b)) {
    b++;
   }
   //Start of Person 1's time slot is before start of Person 2's time slot
   /*
    *        a        b
    * +-------------+---+-------+
    */
   else if(P1.S.get(a) <= P2.S.get(b)) {
    commonFreeTimeSlot.S.add(P2.S.get(b));
    Double time = P1.E.get(a) < P2.E.get(b) ? P1.E.get(a) : P2.E.get(b);
    commonFreeTimeSlot.E.add(time);
    if(P2.E.get(b) < P1.E.get(a)) {
     b++;
    }
    else {
     a++;
    }
   }
   //Start of Person 2's time slot is before start of Person 1's time slot
   /*
    *        b        a
    * +-------------+---+-------+
    */
   else if(P1.S.get(a) > P2.S.get(b)) {
    commonFreeTimeSlot.S.add(P1.S.get(a));
    Double time = P2.E.get(b) < P1.E.get(a) ? P2.E.get(b) :  P1.E.get(a);
    commonFreeTimeSlot.E.add(time);
    if(P1.E.get(a) < P2.E.get(b)) {
     a++;
    }
    else {
     b++;
    }
   }
  }
  return commonFreeTimeSlot;
 }
 
 public static FreeTimeSlot findFreeTimeSlot(List<Double> personStart, List<Double> personEnd) {
  double s=0;
  int a = 0;
  FreeTimeSlot freeTimeSlot = new FreeTimeSlot();
  while(a<personStart.size()) {
   if(s < personStart.get(a)) {
    freeTimeSlot.S.add(s);
    freeTimeSlot.E.add(personStart.get(a));
    s = personEnd.get(a);
    a++;
   }
   else if(s == personStart.get(a)) {
    s = personEnd.get(a);
    a++;
   }
  }
  // Adding the remaining time of the day as a free time slot.
  int length = freeTimeSlot.E.size();
  if(freeTimeSlot.E.get(length - 1) < maxTime && personEnd.get(personEnd.size() - 1) < maxTime) {
   freeTimeSlot.S.add(personEnd.get(personEnd.size() - 1));
   freeTimeSlot.E.add(maxTime);
  }
  return freeTimeSlot;
 }
}

class FreeTimeSlot
{
 List<Double> S = new ArrayList<Double>();
 List<Double> E = new ArrayList<Double>();
}

Sunday, December 21, 2014

Print combinations of strings from List of List of Strings

Print combinations of strings from List of List of Strings.
Example input: [[abc, def], [ghi, jkl, mno], [pqr]]
Example output:
abc ghi pqr 
abc jkl pqr 
abc mno pqr 
def ghi pqr 
def jkl pqr 
def mno pqr 

Java Program

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Combinations {
 public static void main(String[] args) {
  int num = 0;
  ArrayList<List<String>> input = new ArrayList<List<String>>();
  input.add(Arrays.asList("abc","def"));
  input.add(Arrays.asList("ghi","jkl","mno"));
  input.add(Arrays.asList("pqr"));
  ArrayList<String> output = new ArrayList<String>();
  combine(num, input, output);
 }
 
 public static void combine(int num, ArrayList<List<String>> input, ArrayList<String> output) {
  if(num == input.size()) {
   for(String string : output) {
    System.out.print(string + " ");
   }
   System.out.println();
  }
  else {
   List<String> current = input.get(num);
   for(String string : current) {
    output.add(string);
    combine(num + 1, input, output);
    output.remove(num);
   }
  }
 }
}