Reputation: 25
I was wishing to know that how am i supposed to know where my program is going wrong.The problem below is an assignment and has to be submitted online.However out of the 10 test cases my solution works for the first 6 .The other four give a wrong answer as an output.The test Cases are not given or shown to the participants.So could people tell me how should a generate my own set of test Cases for the problem below to know for which scenarios my code is failing?
Description:
Given M busy-time slots of N people, You need to print all the available time slots when all the N people can schedule a meeting for a duration of K minutes.
Event time will be of form HH MM ( where 0 <= HH <= 23 and 0 <= MM <= 59 ), K will be in the form minutes.
Input Format:
M K [ M number of busy time slots , K is the duration in minutes ]
Followed by M lines with 4 numbers on each line.
Each line will be of form StartHH StartMM EndHH EndMM [ Example 9Am-11Am time slot will be given as 9 00 11 00 ]
An event time slot is of form [Start Time, End Time ) . Which means it inclusive at start time but doesn’t include the end time.
So an event of form 10 00 11 00 => implies that the meeting start at 10:00 and ends at 11:00, so another meeting can start at 11:00.
Sample Input:
5 120
16 00 17 00
10 30 14 30
20 45 22 15
10 00 13 15
09 00 11 00
Sample Output:
00 00 09 00
17 00 20 45
Sample Input:
8 60
08 00 10 15
22 00 23 15
17 00 19 00
07 00 09 45
09 00 13 00
16 00 17 45
12 00 13 30
11 30 12 30
Sample Output:
00 00 07 00
13 30 16 00
19 00 22 00
Constraints :
1 <= M <= 100
Note: 24 00 has to be presented as 00 00.
I dont want a solution as i already have one(though not exactly perfect) but just that how should a generate my own set of test cases?I am using Java.My question is related as to how to test my code?
My Solution as Requested:-
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Solution {
private static final Scanner scan = new Scanner(System.in);
static Map<Integer,Integer> map = new HashMap<Integer,Integer>();
static ArrayList<Integer> stime = new ArrayList<Integer>();
static StringTokenizer str = null;
static int limit;
static int[] timeArr = new int[1440];
ArrayList<Integer> time = new ArrayList<Integer>();
public static void main(String args[])
{
initialize(timeArr);
String line = scan.nextLine().toString();
str = new StringTokenizer(line);
int trials;
trials = Integer.parseInt(str.nextToken().toString());
limit = Integer.parseInt(str.nextToken().toString());
int startHr,startMin,endHr,endMin,startTime,endTime;
for(int i=0;i<trials;i++)
{
line = scan.nextLine().toString();
str = new StringTokenizer(line);
startHr = Integer.parseInt(str.nextToken());
startMin = Integer.parseInt(str.nextToken());
startTime = startHr*60 + startMin;
endHr = Integer.parseInt(str.nextToken());
endMin = Integer.parseInt(str.nextToken());
if(endHr==00 && endMin == 00)
{
endHr = 23;
endMin = 60;
}
endTime = (endHr*60 + endMin);
//System.out.println(startHr + ":" + startMin + " to " + endHr + ":" + endMin + " is " + startTime + " to " + endTime);
fillSlots(startTime,endTime);
}
//display();
fillMap();
//display();
limitMap();
toProperTime();
}
private static void toProperTime() {
// TODO Auto-generated method stub
int starthr,startmin,endhr,endmin;
for(int i=0;i<stime.size();i++)
{
starthr = (stime.get(i))/60;
startmin = (stime.get(i))%60;
if(map.get(stime.get(i))==1439)
{
endhr = (map.get(stime.get(i)) + 1)%24;
endmin = (map.get(stime.get(i)) + 1)%24;
}
else
{
endhr = (map.get(stime.get(i)) + 1)/60;
endmin = (map.get(stime.get(i)) + 1)%60;
}
System.out.println(getTime(starthr) + " " + getTime(startmin) + " " + getTime(endhr) + " " + getTime(endmin));
}
}
private static void fillMap() {
// TODO Auto-generated method stub
int counter = 0;
int endTime ,startTime,currentTime ;
boolean loop1 = false;
boolean loop2 =false;
while(counter<1440)
{
//System.out.println(counter);
currentTime = counter;
while((counter<1440)&&timeArr[counter]!=1)
{
loop1 = true;
if((counter==1439) || timeArr[counter] == 1)
{
endTime = counter;
counter++;
break;
}
counter++;
}
if((loop1==true)&&(loop2==false))
{
stime.add(currentTime);
map.put(currentTime,counter-1);
}
while((counter<1440)&&timeArr[counter]!=0)
{
counter++;
}
loop1 = false;
}
}
private static void displayMap() {
// TODO Auto-generated method stub
for(int i=0;i<stime.size();i++)
System.out.println(stime.get(i) +" till " + map.get(stime.get(i)));
}
private static void limitMap() {
// TODO Auto-generated method stub
for(int i=0;i<stime.size();i++)
{
if((map.get(stime.get(i)) - stime.get(i)) + 1 < limit)
{
map.remove(stime.get(i));
stime.remove(i);
}
}
}
private static void display() {
// TODO Auto-generated method stub
for(int i=0;i<timeArr.length;i++)
{
if((i)%60==0)
System.out.println();
System.out.print(timeArr[i]);
}
}
private static void initialize(int[] timeArr) {
// TODO Auto-generated method stub
for(int i=0;i<timeArr.length;i++)
{
timeArr[i] = 0;
}
}
private static void fillSlots(int startTime,int endTime) {
// TODO Auto-generated method stub
for(int i =startTime;i<endTime;i++)
{
timeArr[i] = 1;
}
}
private static String getTime(int x) {
// TODO Auto-generated method stub
NumberFormat format=NumberFormat.getInstance();
format.setMaximumIntegerDigits(2);
format.setMinimumIntegerDigits(2);
return format.format(x).replace(",","") ;
}
}
Upvotes: 0
Views: 227
Reputation: 12985
Here is one way to generate test cases:
Get some graph paper or turn lined paper sideays. Label vertical lines 00 to 24. Then draw in patterns of overlapping "meetings". You should be able to identify patterns of meetings that should be tested.
It might look like this:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
------------- (4 to 8)
---- (7 to 8)
--- (3 pm to 4 pm)
Which becomes:
03 60 <- Also try different lengths of desired meeting
04 00 08 00
07 00 08 00
15 00 16 00
For example,
Turn those lines into test cases but handle the additional cases relating to the order of entry:
Upvotes: 1