简体   繁体   中英

How to write an algorithm to find out how many times different employee were at the office at the same time?

A company needs to know what employees have been at the office within the same time frame

This exercise aims to output a table containing pairs of employees and how often they have coincided in the office. Input: the name of an employee and the schedule they worked, indicating the time and hours. This should be a .txt file with at least five sets of data. You can include the data from our examples below:

Example 1:

INPUT

RENE=MO10:00-12:00,TU10:00-12:00,TH01:00-03:00,SA14:00-18:00,SU20:00- 21:00
ASTRID=MO10:00-12:00,TH12:00-14:00,SU20:00-21:00
ANDRES=MO10:00-12:00,TH12:00-14:00,SU20:00-21:00

OUTPUT:

ASTRID-RENE: 2
ASTRID-ANDRES: 3
RENE-ANDRES: 2

Example 2: INPUT:

RENE=MO10:15-12:00,TU10:00-12:00,TH13:00-13:15,SA14:00-18:00,SU20:00-21:00
ASTRID=MO10:00-12:00,TH12:00-14:00,SU20:00-21:00

OUTPUT:

RENE-ASTRID: 3

This is the code I have so far comparing only two employees. I am getting the data from.txt file.

    const obj = {
  ANDRES: ['MO10:00-12:00', 'TH12:00-14:00', 'SU20:00-21:00'],
  RENE: ['MO10:00-12:00', 'TU10:00-12:00', 'TH01:00-03:00', 'SA14:00-18:00', 'SU20:00-21:00']
}



 function loop1(obj){
  let counter = 0
  for(let i = 0; i < obj.ANDRES.length; i++){
    for(let k = 0; k < obj.RENE.length; k++){
      if(obj.ANDRES[i] === obj.RENE[k]){
        counter++
      }
    }
  }
  console.log(`ANDRES-RENE: ${counter}`)
}

loop1(obj)

OUTPUT

ANDRES-RENE: 2

How would I do it with three or more people?

You could compare more than 2 persons by using another 2 loops matching each pair. This is similar to sorting algorithm where you swap some numbers.

 var obj = { AVI: ["AVI hours"], BEN: ["BEN hours"], CAT: ["CAT hours"], DAD: ["DAD hours"], } var keys = Object.keys(obj) for (var i = 0; i < keys.length - 1; i++) { for (var j = i + 1; j < keys.length; j++) { var a = obj[keys[i]]; var b = obj[keys[j]]; var overlaps = countOverlaps(a, b) console.log(keys[i] + " and " + keys[j] + " have " + overlaps + " overlaps"); } } function countOverlaps(a_hours, b_hours) { // split and compare... return 2; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM