Home > Net >  Find the maximum sum at a given timestamp
Find the maximum sum at a given timestamp

Time:03-29

I am trying to find the date at which timestamp is the sum of the marks obtained by students the highest

[
{Student: 'A',Timestamp: 2022-03-27T22:24:00.000Z,Marks: 5},
{Student: 'B',Timestamp: 2022-03-27T22:24:00.000Z,Marks: 10},
{Student: 'C',Timestamp: 2022-03-27T22:24:00.000Z,Marks: 15},

{Student: 'A',Timestamp: 2022-02-27T22:24:00.000Z,Marks: 3},
{Student: 'B',Timestamp: 2022-02-27T22:24:00.000Z,Marks: 5},
{Student: 'C',Timestamp: 2022-02-27T22:24:00.000Z,Marks: 5},

{Student: 'A',Timestamp: 2022-01-27T22:24:00.000Z,Marks: 10},
{Student: 'B',Timestamp: 2022-01-27T22:24:00.000Z,Marks: 15},
{Student: 'C',Timestamp: 2022-01-27T22:24:00.000Z,Marks: 2}
]

The expected result would be:

At Timestamp 2022-03-27T22:24:00.000Z, the sum of marks obtained is 30
At Timestamp 2022-02-27T22:24:00.000Z, the sum of marks obtained is 13
At Timestamp 2022-01-27T22:24:00.000Z, the sum of marks obtained is 27

Could someone help me with how I could write this as code in Javascript?

CodePudding user response:

Your timestamps look like ISO 8601 strings and should therefore by quoted.

In this implementation I assume that your timestamps match to the millisecond, but if that's not the case you could just use mark.Timestamp.substring(x,y) to cut out the part you are interested in. I am using reduce() here to compute the desired result in O(n) (i.e. the counting). Sorting is as fast as the JavaScript sorting but as it's using comparisons it cannot be faster than O(n * log n)

const marks = [
  { Student: "A", Timestamp: "2022-03-27T22:24:00.000Z", Marks: 5 },
  { Student: "B", Timestamp: "2022-03-27T22:24:00.000Z", Marks: 10 },
  { Student: "C", Timestamp: "2022-03-27T22:24:00.000Z", Marks: 15 },

  { Student: "A", Timestamp: "2022-02-27T22:24:00.000Z", Marks: 3 },
  { Student: "B", Timestamp: "2022-02-27T22:24:00.000Z", Marks: 5 },
  { Student: "C", Timestamp: "2022-02-27T22:24:00.000Z", Marks: 5 },

  { Student: "A", Timestamp: "2022-01-27T22:24:00.000Z", Marks: 10 },
  { Student: "B", Timestamp: "2022-01-27T22:24:00.000Z", Marks: 15 },
  { Student: "C", Timestamp: "2022-01-27T22:24:00.000Z", Marks: 2 },
];

const result = marks.reduce((acc, mark) => {
  acc[mark.Timestamp] ? acc[mark.Timestamp]  = mark.Marks: acc[mark.Timestamp] = mark.Marks
  return acc;
}, {});
// sort the values in descending order
const sorted = Object.entries(result).sort((a, b) => b[1]- a[1])
console.log(sorted);
// get day with most marks
console.log(sorted[0])

CodePudding user response:

Here is a working solution using reduce function that sorts and displays the timestamp with the highest score.

const data = [
  { Student: "A", Timestamp: "2022-03-27T22:24:00.000Z", Marks: 5 },
  { Student: "B", Timestamp: "2022-03-27T22:24:00.000Z", Marks: 10 },
  { Student: "C", Timestamp: "2022-03-27T22:24:00.000Z", Marks: 15 },

  { Student: "A", Timestamp: "2022-02-27T22:24:00.000Z", Marks: 3 },
  { Student: "B", Timestamp: "2022-02-27T22:24:00.000Z", Marks: 5 },
  { Student: "C", Timestamp: "2022-02-27T22:24:00.000Z", Marks: 5 },

  { Student: "A", Timestamp: "2022-01-27T22:24:00.000Z", Marks: 10 },
  { Student: "B", Timestamp: "2022-01-27T22:24:00.000Z", Marks: 15 },
  { Student: "C", Timestamp: "2022-01-27T22:24:00.000Z", Marks: 2 },
];

const sortedMarksAndStamps = data.reduce((acc, curr) => {
    const exists = acc.findIndex(accEntry => accEntry.Timestamp === curr.Timestamp);
    if (exists > -1)
       {
           acc[exists] = {...acc[exists], Marks: acc[exists].Marks   curr.Marks}
       } else {
            acc.push({Timestamp: curr.Timestamp, Marks: curr.Marks});
       }
    return acc.sort((a, b) => b.Marks - a.Marks);
}, [])

console.log(sortedMarksAndStamps.map(sortedEntry => `At Timestamp ${sortedEntry.Timestamp}, the sum of marks obtained is ${sortedEntry.Marks}`))

CodePudding user response:

/*Your Timestamp have to be a string*/
const marks = [
{ Student: "A", Timestamp: "2022-03-27T22:24:00.000Z", Marks: 5 },
  { Student: "B", Timestamp: "2022-03-27T22:24:00.000Z", Marks: 10 },
  { Student: "C", Timestamp: "2022-03-27T22:24:00.000Z", Marks: 15 },

  { Student: "A", Timestamp: "2022-02-27T22:24:00.000Z", Marks: 3 },
  { Student: "B", Timestamp: "2022-02-27T22:24:00.000Z", Marks: 5 },
  { Student: "C", Timestamp: "2022-02-27T22:24:00.000Z", Marks: 5 },

  { Student: "A", Timestamp: "2022-01-27T22:24:00.000Z", Marks: 10 },
  { Student: "B", Timestamp: "2022-01-27T22:24:00.000Z", Marks: 15 },
  { Student: "C", Timestamp: "2022-01-27T22:24:00.000Z", Marks: 2 },
];

/*Here we will have an object that contain a time => sum(mark)*/
const tab = marks.reduce((acc, mark) => {
  acc[mark.Timestamp] ? acc[mark.Timestamp]  = mark.Marks: acc[mark.Timestamp] = mark.Marks
  return acc;
}, {});

/*We get timestamp and of hight sum_mark*/
var  max_sum_mark = Math.max(...Object.values(tab))
var expected_key = Object.keys(tab).find(key => tab[key] === max_sum_mark);

/*OUTPUT*/
console.log(expected_key);
console.log(max_sum_mark);
  • Related