mercredi 5 octobre 2016

Unpassed hidden tests in java programs (codefights)

Here is the assignment:

I have 4 input arrays indicating information of different employees

String[] names, boolean[] statuses, int[] projects, int[] tasks

They will be passed as parameters to the method that I had to write. The output should be the name of the employee that meets the requirement

The requirement is:

  • The i^th element of each array belongs to the i^th employee. The number of employees varies
  • His/her statuses has to be false for this employee to be considered for the next condition
  • If, for example, the statuses of A and B are false, the one with fewer tasks, or the one with tasks equivalent to that of the other but having fewer projects will be chosen. Do this for all employees.
  • Return the names (String) of this employee

Example of the arrays maybe like:

names = ["John", "Martin"], statuses = [false, false],
projects = [2, 1] and tasks = [16, 5],

The output should be:

smartAssigning(names, statuses, projects, tasks) = "Martin".

And here is my code: (this is only the method)

String smartAssigning(String[] names, boolean[] statuses, int[] projects, int[] tasks) {
String name = null;
int task = tasks[0];
int order = 0;
int mainOrder = 0;
int project;
int[] list = new int[names.length];
for (int i = 1; i < tasks.length; i++) {
    if (tasks[i] < task && statuses[i] == false) {
        task = tasks[i];
    }
}
for (int i = 0; i < tasks.length; i++) {
    if (task == tasks[i]) {
        list[order] = i;
        order++;
    }
}
project = list[0];
if (order > 1) {
    for (int i = 1; i < order; i++) {
    if (project > projects[list[i]]) {
        project = projects[list[i]];
        mainOrder = list[i];
        }
    }
} else mainOrder = list[0];
return names[mainOrder];

}

The problem is I wasn't able to pass 2 out of 11 available tests. But those 2 were hidden. Anyone can give me some suggestions?

Aucun commentaire:

Enregistrer un commentaire