Enlightensoft's Blog

Helping in your each step

  • Categories

  • Authors

  • Advertisements

Coding Problem : Display Time

Posted by Pankil Patel on March 9, 2018

Problem:

By provided 6 different integer of (0 to 9)

Generate the time string which displays minimum time in 24 hours format.

For Example:

Input: 1, 8, 9, 2, 6, 4

Output: “16:28:49”

Solution :

package co.cc.enlightensoft.codility.self;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Solution_DisplayTime {

public String solution(int A, int B, int C, int D, int E, int F) {
 String result = "";

List data = new ArrayList<>(6);
 data.add(A);
 data.add(B);
 data.add(C);
 data.add(D);
 data.add(E);
 data.add(F);

Collections.sort(data);

// Special Cases
 if (data.get(0) == 0 && data.get(1) == 0 && data.get(2) == 0) {
 result = "0" + data.get(3) + ":0" + data.get(4) + ":0" + data.get(5);
 } else {
 result = finalString(data, -1, -1, -1);
 }

return result;
 }

private String finalString(List data, int lastH, int lastM, int lastS) {

// Hour Section
 int intHour = getHour(data, lastH);

String h = String.format("%02d", intHour);

String h1 = h.substring(0, 1);
 String h2 = h.substring(1, 2);

// Minute Section
 List minData = new ArrayList<>(data);

minData.remove(new Integer(h1));
 minData.remove(new Integer(h2));

int intMinute = getMinuteOrSec(minData, lastM);

String m = String.format("%02d", intMinute);
 String m1 = m.substring(0, 1);
 String m2 = m.substring(1, 2);

// Second Section
 List secData = new ArrayList<>(minData);
 secData.remove(new Integer(m1));
 secData.remove(new Integer(m2));

int intSec = getMinuteOrSec(secData, lastS);

// Recursive Conditions
 if (intSec >= 60) {
 return finalString(data, intHour - 1, intMinute, -1);
 }
 if (intMinute >= 60 && intHour > 23) {
 return "NOT POSSIBLE";
 }
 if (intMinute >= 60) {
 return finalString(data, intHour, -1, intSec);
 }
 if (intHour >= 24) {
 return finalString(data, -1, intMinute, intSec);
 }

return String.format("%02d", intHour) + ":" + String.format("%02d", intMinute) + ":"
 + String.format("%02d", intSec);
 }

private int getMinuteOrSec(List data, int lstMin) {
 int minSec = 60;
 for (int i = 0; i < data.size(); i++) {
 int j = i + 1;
 for (; j < data.size(); j++) {
 String hour = data.get(i) + "" + data.get(j);
 int curr = Integer.parseInt(hour);
 if (curr <= 59 && curr < minSec && lstMin < curr) {
 minSec = curr;
 break;
 }
 }

}
 return minSec;
 }

private int getHour(List data, int lstH) {
 int intHour = 24;
 for (int i = 0; i < data.size(); i++) {
 int j = i + 1;
 for (; j < data.size(); j++) {
 String hour = data.get(i) + "" + data.get(j);
 int currHour = Integer.parseInt(hour);
 if (currHour <= 23 && currHour < intHour && lstH < currHour) {
 intHour = currHour;
 break;
 }
 }

}
 return intHour;
 }

}

JUnit :

package co.cc.enlightensoft.codility.self;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class JUMainTest_DisplayTime {

Solution_DisplayTime s = new Solution_DisplayTime();

@Test
 public void testCase1() {

assertEquals("Failed", "12:36:48", s.solution(1, 8, 3, 2, 6, 4));
 }

@Test
 public void testCase2() {
 assertEquals("Failed", "16:28:49", s.solution(1, 8, 9, 2, 6, 4));
 }

/*
 * @Test public void testCase3() {
 *
 * assertEquals("Failed", "00:02:46", s.solution(0, 0, 0, 2, 6, 4)); }
 */

@Test
 public void testCase4() {
 assertEquals("Failed", "02:04:06", s.solution(0, 0, 0, 2, 6, 4));
 }

@Test
 public void testCase5() {

assertEquals("Failed", "NOT POSSIBLE", s.solution(9, 5, 8, 7, 6, 4));
 }

@Test
 public void testCase6() {

assertEquals("Failed", "00:00:00", s.solution(0, 0, 0, 0, 0, 0));
 }

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: