Menu
Topics Index
...
`

Weekend Hack - 15 Jun 2013 - Results

Thanks to Pramod Jain, Soumya Tata and Srihari Adelli for spending so much time, coming up with original and innovative answers and for submitting them. Thanks to many others who have tried to get the solution.
We have reviewed the solutions as per the criteria mentioned at Weekend Hack and included the comments below. Given the experience of the participants, they were really innovative and clean. Few minor comments found are included below in the order of the submission. These comments are not intended to point out the mistakes in the solutions but to learn from them.

The winner of the Weekend Hack 15 Jun 2013 - 1 is Pramod Jain. He will get a recharge or gift voucher worth Rs. 150.

To get the common working day between Dubai and US

Write a program to get the common working day between Dubai and US. Given that Dubai does not work on Thursday and Friday and the US does not on Saturday and Sunday. The list of other holidays are also given.

Input (String, int, List, List) Output (String)
12-Nov-1947, 366, [10-Feb-1947, 05-May-1947, 06-May-1947, 11-May-1948, 12-Oct-1948, 24-Dec-1948] and [11-Feb-1947, 01-May-1947, 15-Oct-1948, 15-Nov-1948, 28-Nov-1948, 27-Dec-1948] 16-Nov-1948
01-Apr-2000, 19, [14-Feb-2000, 24-Apr-2000, 03-Oct-2000, 25-Dec-2000, 26-Dec-2000] and [07-Feb-2000, 08-Feb-2000, 09-Feb-2000, 25-Apr-2000, 02-Oct-2000, 04-Oct-2000] 26-Apr-2000
1-Feb-2000, 2, [14-Feb-2000, 24-Apr-2000, 03-Oct-2000, 25-Dec-2000, 26-Dec-2000] and [07-Feb-2000, 08-Feb-2000, 09-Feb-2000, 25-Apr-2000, 02-Oct-2000, 04-Oct-2000] 15-Feb-2000
25-Oct-2000, 60, [14-Feb-2000, 24-Apr-2000, 03-Oct-2000, 25-Dec-2000, 26-Dec-2000] and [07-Feb-2000, 08-Feb-2000, 09-Feb-2000, 25-Apr-2000, 02-Oct-2000, 04-Oct-2000] 27-Dec-2000
23-Dec-2014, 365, [23-Oct-2014, 25-Dec-2014, 01-Apr-2015, 20-Aug-2015] and [01-Jan-2014, 11-Mar-2014, 24-Nov-2014, 23-Dec-2015] 28-Dec-2015
30-Mar-2015, 143, [23-Oct-2014, 25-Dec-2014, 01-Apr-2015, 20-Aug-2015] and [01-Jan-2014, 11-Mar-2014, 24-Nov-2014, 23-Dec-2015] 24-Aug-2015

Solution 1 - Submitted on Sun Jun 16 14:33:57 2013

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

class GetCommonWorkingDayAdvanced {

    public static void main(String s[]) {
        String currentWorkingDay = "22-Jan-2013";
        int findAfter = 1;
        List holidaysUS = new ArrayList();
        holidaysUS.add("01-Jan-2013");
        holidaysUS.add("13-Jan-2013");
        holidaysUS.add("01-Feb-2013");
        holidaysUS.add("29-Mar-2013");
        holidaysUS.add("12-May-2013");
        holidaysUS.add("04-Jul-2013");
        holidaysUS.add("02-Sep-2013");
        holidaysUS.add("03-Nov-2013");
        holidaysUS.add("25-Dec-2013");
        List holidaysDubai = new ArrayList();
        holidaysDubai.add("01-Jan-2013");
        holidaysDubai.add("24-Jan-2013");
        holidaysDubai.add("06-Jun-2013");
        holidaysDubai.add("09-Jul-2013");
        holidaysDubai.add("08-Aug-2013");
        holidaysDubai.add("14-Oct-2013");
        holidaysDubai.add("04-Nov-2013");
        holidaysDubai.add("02-Dec-2013");
        holidaysDubai.add("03-Dec-2013");
        String commonWorkingDayIs = getCommonWorkingDay(currentWorkingDay, findAfter, holidaysUS, holidaysDubai);
        System.out.println("Common working day after " + findAfter + " days from " + currentWorkingDay + " is " + commonWorkingDayIs);
    }

    public static String getCommonWorkingDay(String startingDate, int findAfter, List holidaysUS, List holidaysDubai) {
        String commonWorkingDay = null;
        Calendar temporaryDate = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
        // Parsing startingDate String into Calendar object
        try {
            temporaryDate.setTime(sdf.parse(startingDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // Adding the given number of days to calendar object as we need to find working day after these many days
        temporaryDate.add(Calendar.DAY_OF_YEAR, findAfter);
        // Now temporaryDate has new date in it which is obtained by adding startingDate and given period using variable findAfter
        // Now finding the next common working day for both US and DUBAI in while loop
        while (true) {
            // The day should not be Thursday(5) , Friday(6) , Saturday(7) , Sunday(1) so it should be Monday(2) , Tuesday(3) or Wednesday(4)
            // It should neither be a US holiday nor a Dubai holiday
            if (temporaryDate.get(Calendar.DAY_OF_WEEK) > 1 && temporaryDate.get(Calendar.DAY_OF_WEEK) < 5 && !holidaysUS.contains(sdf.format(temporaryDate.getTime())) && !holidaysDubai.contains(sdf.format(temporaryDate.getTime()))) {
                // if all conditions are satisfied then get the day into variable commonWorkingDay and break the loop
                commonWorkingDay = sdf.format(temporaryDate.getTime());
                break;
            }
            // If the given day is between Sunday and Tuesday then moving to next day
            if (temporaryDate.get(Calendar.DAY_OF_WEEK) < 4)
                temporaryDate.add(Calendar.DAY_OF_YEAR, 1);
            // Otherwise moving directly to next Monday
            else {
                temporaryDate.add(Calendar.DAY_OF_YEAR, 9 - temporaryDate.get(Calendar.DAY_OF_WEEK));
            }
        }
        return commonWorkingDay;
    }
}

Review Comments

Overall Rating (out of 10): 6

Solution 2 - Submitted on Sun Jun 16 23:53:23 2013

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

class GetCommonWorkingDayAdvanced {

    public static void main(String s[]) {
        String currentWorkingDay = "22-Jan-2013";
        int findAfter = 1;
        List holidaysUS = new ArrayList();
        holidaysUS.add("01-Jan-2013");
        holidaysUS.add("13-Jan-2013");
        holidaysUS.add("01-Feb-2013");
        holidaysUS.add("29-Mar-2013");
        holidaysUS.add("12-May-2013");
        holidaysUS.add("04-Jul-2013");
        holidaysUS.add("02-Sep-2013");
        holidaysUS.add("03-Nov-2013");
        holidaysUS.add("25-Dec-2013");
        List holidaysDubai = new ArrayList();
        holidaysDubai.add("01-Jan-2013");
        holidaysDubai.add("24-Jan-2013");
        holidaysDubai.add("06-Jun-2013");
        holidaysDubai.add("09-Jul-2013");
        holidaysDubai.add("08-Aug-2013");
        holidaysDubai.add("14-Oct-2013");
        holidaysDubai.add("04-Nov-2013");
        holidaysDubai.add("02-Dec-2013");
        holidaysDubai.add("03-Dec-2013");
        String commonWorkingDayIs = getCommonWorkingDay(currentWorkingDay, findAfter, holidaysUS, holidaysDubai);
        System.out.println("Common working day after " + findAfter + " days from " + currentWorkingDay + " is " + commonWorkingDayIs);
    }

    public static String getCommonWorkingDay(String startingDate, int findAfter, List holidaysUS, List holidaysDubai) {
        String commonWorkingDay = "";
        Date d = null;
        int i = 1;
        int k = 0;
        long time1, time2;
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
        try {
            d = sdf.parse(startingDate);//parsing the string into date
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Calendar cd = Calendar.getInstance();
        cd.setTime(d);//setting the time to startingdate
        time1 = time2 = cd.getTimeInMillis();
        while (i < findAfter) //going forward by specified number of days
        {
            time2 = time2 + 86400000;
            Date d11 = new Date(time2);
            if (d11.getDate() == k) {
                i--;
            }
            k = d11.getDate();
            i++;
        }
        while (true) //going forward by each day after specified number of days 
        {
            time2 = time2 + 86400000;//each day has 86400000 milliseconds
            Date d1 = new Date(time2);
            if (d1.getDay() != 0 && d1.getDay() != 4 && d1.getDay() != 5 && d1.getDay() != 6)//checking whether the day is not sunday/satrday/thurs/fri
            {
                SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MMM-yyyy");
                String common = sdf1.format(d1);//converting string into date format
                if (!holidaysDubai.contains(common) && !holidaysUS.contains(common))//checking whether or not the day falls in list of US holidays or dubai holidays
                {
                    commonWorkingDay = common;
                    break;
                }
            }
        }
        return commonWorkingDay;
    }
}

Review Comments

Overall Rating (out of 10): 5

Solution 3 - Submitted on Mon Jun 17 10:55:18 2013

import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
import java.util.StringTokenizer;

class GetCommonWorkingDayAdvanced {

    public static void main(String s[]) {
        String currentWorkingDay = "22-Jan-2013";
        int findAfter = 1;
        List holidaysUS = new ArrayList();
        holidaysUS.add("01-Jan-2013");
        holidaysUS.add("13-Jan-2013");
        holidaysUS.add("01-Feb-2013");
        holidaysUS.add("29-Mar-2013");
        holidaysUS.add("12-May-2013");
        holidaysUS.add("04-Jul-2013");
        holidaysUS.add("02-Sep-2013");
        holidaysUS.add("03-Nov-2013");
        holidaysUS.add("25-Dec-2013");
        List holidaysDubai = new ArrayList();
        holidaysDubai.add("01-Jan-2013");
        holidaysDubai.add("24-Jan-2013");
        holidaysDubai.add("06-Jun-2013");
        holidaysDubai.add("09-Jul-2013");
        holidaysDubai.add("08-Aug-2013");
        holidaysDubai.add("14-Oct-2013");
        holidaysDubai.add("04-Nov-2013");
        holidaysDubai.add("02-Dec-2013");
        holidaysDubai.add("03-Dec-2013");
        String commonWorkingDayIs = getCommonWorkingDay(currentWorkingDay, findAfter, holidaysUS, holidaysDubai);
        System.out.println("Common working day after " + findAfter + " days from " + currentWorkingDay + " is " + commonWorkingDayIs);
    }

    public static String getCommonWorkingDay(String startingDate, int findAfter, List holidaysUS, List holidaysDubai) {
        String commonWorkingDay = "";
        /*to Store all the holidays in the holidays String array*/
        String holidays[] = new String[holidaysUS.size() + holidaysDubai.size()];
        int i = 0;
        for (String date : holidaysUS) {
            holidays[i] = date;
            i++;
        }
        for (String date : holidaysDubai) {
            holidays[i] = date;
            i++;
        }
        String sampleDate = "";
        StringTokenizer stringWithTokens = new StringTokenizer(startingDate, "-");
        int day = 0, year = 0;
        String month = "";
        while (stringWithTokens.hasMoreElements()) {
            day = Integer.parseInt(stringWithTokens.nextToken());
            month = stringWithTokens.nextToken();
            year = Integer.parseInt(stringWithTokens.nextToken());
        }
        int noOfDaysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        String nameOfMonths[] = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
        i = 0;
        int temp = 0;
        boolean dayIsAssigned = false;
        for (i = 0; i < noOfDaysInMonth.length; i++) {
            if (month.equals(nameOfMonths[i]))
                break;
        }
        if (year % 4 == 0)
            noOfDaysInMonth[1] = 29;
        else
            noOfDaysInMonth[1] = 28;
        if (noOfDaysInMonth[i] - day > 0) {
            if (findAfter > noOfDaysInMonth[i] - day) {
                findAfter = findAfter - (noOfDaysInMonth[i] - day);
                if (i == 11) {
                    year += 1;
                    if (year % 4 == 0)
                        noOfDaysInMonth[1] = 29;
                    else
                        noOfDaysInMonth[1] = 28;
                    i = 0;
                } else
                    i++;
            } else {
                day += findAfter + 1;
                dayIsAssigned = true;
            }
        }
        while (findAfter - noOfDaysInMonth[i] > 0) {
            findAfter = findAfter - noOfDaysInMonth[i];
            if (i == 11) {
                year += 1;
                if (year % 4 == 0)
                    noOfDaysInMonth[1] = 29;
                else
                    noOfDaysInMonth[1] = 28;
                i = 0;
            } else
                i++;
        }
        if (findAfter - noOfDaysInMonth[i] < 0 && !dayIsAssigned) {
            day = findAfter;
            month = nameOfMonths[i];
        }
        /* To find the day of the week for the given week by using the Gauss's Algorithm
             Year=ActualYear-1 only if month is january or february
           (month=actualMonth-2)%12 is stored in months array;
         
        */
        boolean commonWorkingDayIsFound = false;
        String daysOfWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        int months[] = {11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int sampleDay = day;
        int sampleYear = year;
        boolean dayIsHoliday = false;
        Formatter stringFormatting = new Formatter();
        stringFormatting.format("%02d-%s-%d", day, month, year);
        sampleDate = stringFormatting.toString();
        while (!commonWorkingDayIsFound) {
            if (nameOfMonths[i].equals("Jan") || nameOfMonths[i].equals("Feb"))
                sampleYear -= 1;
            /* Gaussian formula --- gives the vaule 0,1,2,...6 to sunday, monday,.... saturday respectively*/
            sampleDay = ((sampleDay % 7 + (int) (2.6 * months[i] - 0.2) % 7 + (5 * ((sampleYear % 100) % 4)) % 7 + (4 * (sampleYear % 100)) % 7 + (6 * (sampleYear % 400)) % 7) % 7);
            /*sampleDay >0 and <4, since thrusday-4,friday-5,saturday-6,sunday-0 are holidays*/
            if (sampleDay > 0 && sampleDay < 4) {
                for (String date : holidays) {
                    if (sampleDate.equals(date)) {
                        dayIsHoliday = true;
                        break;
                    }
                }
                if (!dayIsHoliday) {
                    Formatter dateString = new Formatter();
                    dateString.format("%02d-%s-%d", day, month, year);
                    commonWorkingDay = dateString.toString();
                    break;
                }
            }
            if (day == noOfDaysInMonth[i]) {
                day = 1;
                i++;
                month = nameOfMonths[i];
                if (i == 12) {
                    year += 1;
                    if (year % 4 == 0)
                        noOfDaysInMonth[1] = 29;
                    else
                        noOfDaysInMonth[1] = 28;
                }
            } else
                day += 1;
            sampleDay = day;
            sampleYear = year;
            dayIsHoliday = false;
            stringFormatting = new Formatter();
            stringFormatting.format("%02d-%s-%d", day, month, year);
            sampleDate = stringFormatting.toString();
        }
        stringFormatting.close();
        return commonWorkingDay;
    }
}

Review Comments

Overall Rating (out of 10): 4

© meritcampus 2019

All Rights Reserved.

Open In App