-1

I have some student's profile with values on several subjects like physics, chemistry and math. I need find a list of dominant students depending on individual score on subjects. For example:

let students = [{name: "A", phy: 70, chem: 80, math: 90},
              {name: "B", phy: 75, chem: 85, math: 60},
              {name: "C", phy: 78, chem: 81, math: 92},
              {name: "D", phy: 75, chem: 85, math: 55}]; 

A student will be dominant over another if he meets following two conditions. 1. student_1 >= student_2 for all parameter 2. student_1 > student_2 for at least one parameter

I have tried using nested loop. May be it's the brute-force algorithm. I added another parameter named "passed" to track if it's dominant over other. Here's the code:


let students = [{ name: "A", phy: 70, chem: 80, math: 90, passed: true },
{ name: "B", phy: 75, chem: 85, math: 60, passed: true },
{ name: "C", phy: 78, chem: 81, math: 92, passed: true },
{ name: "D", phy: 75, chem: 85, math: 55, passed: true }];

let weak_student: any;

for (let student_1 of students) {

    if (student_1.passed == false ||
        students[students.length] === student_1) {
        continue;
    }

let compareList = students.filter(i => i.name != student_1.name && i.passed == true);

    for (let student_2 of compareList) {

        if ((student_1.phy >= student_2.phy &&
            student_1.chem >= student_2.chem &&
            student_1.math >= student_2.math)
            &&
            (student_1.phy > student_2.phy ||
                student_1.chem > student_2.chem ||
                student_1.math > student_2.math)
        ) {
            weak_student = students.find(i => i.name === student_2.name);
            weak_student.passed = false;

        } else if (student_1.phy < student_2.phy &&
            student_1.chem < student_2.chem &&
            student_1.math < student_2.math) {

            student_1.passed = false;
            break;
        }
    }
}
console.log(students);

I found expected result as student A & D 's flag "passed"== false. Now I need the same result by using different algorithms like Divide & Conquer, Nearest Neighbor, Branch & Bound etc or any other efficient ways. I need to compare between algorithm's of time & space complexity for large datasets.

ImtiazNur
  • 250
  • 2
  • 13

1 Answers1

1

You could sort the array by taking the keys, get the delta of all values and normalize the value to [-1, 0, 1] by using Math.sign, sum the values and return this sum as result for sorting.

The most top group is the one with the greatest values.

let students = [{ name: "A", phy: 70, chem: 80, math: 90 }, { name: "B", phy: 75, chem: 85, math: 60 }, { name: "C", phy: 78, chem: 81, math: 92 }, { name: "D", phy: 75, chem: 85, math: 55 }];

students.sort((a, b) => ['phy', 'chem', 'math']
    .map(k => Math.sign(b[k] - a[k]))
    .reduce((a, b) => a + b)
);

console.log(students);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Thanks for your answer @Nina. Unfortunately I couldn't understand how an object is being discarded by comparing with other using Math.sign. Can you please help? – ImtiazNur Nov 04 '19 at 11:04
  • 1
    basically your given values are simplified by using their delta and normalized to a one of the three value (as above mentioned) and then added to get groups of top all three values (3), top with two values (2) and others, like zero for same group. – Nina Scholz Nov 04 '19 at 11:17
  • But it doesn't work properly in typescript. [link (]http://www.typescriptlang.org/play/?ssl=11&ssc=16&pln=1&pc=1#code/DYUwLgBAzmCuAmIB2YoQLwQNoG8JIEMBbEALggCIBBCgGggAcALAT3IHYAGegYyZCLkAHNwhECYJuQCcnCAF9aAKDyES5CgCE6jVhwCsvfoIhDDYiVIgA2OYpX5iZSgGEdzNhHZCjA4QEZ6cUkZACYFZVUnDQARdz0vcz4-U3Ngq319BQBdAG4lAtBIAA8MaDhEFCgAOigAewAnMAAKZoJ6ACMASgwAPmwAcg8B+gHkohGIAfSB7KUIBYhq8QZmgGs+iABZS1qASwBzJGaOrDXsiABaCAIz7K6u+cXqhpB4WB4QVvaIbs2CCAAal+jy6+R4dSQ9VA1WAdQOzWKYKAA) – ImtiazNur Nov 04 '19 at 11:29
  • i have no idea of *typescript*. – Nina Scholz Nov 04 '19 at 11:54
  • I didn't notice something at first. Your answer returns all the objects just in sorted form which I was not looking for. Correct output of my problem will be a list with object B and C. Please have a look. – ImtiazNur Nov 04 '19 at 15:02
  • you can only have a relation between two objects, but not give some own rating, because this rating rely on more then one comparison. – Nina Scholz Nov 04 '19 at 16:12