简体   繁体   English

如何编写算法来找出不同员工同时在办公室的次数?

[英]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.这应该是一个包含至少五组数据的 .txt 文件。 You can include the data from our examples below:您可以包含以下示例中的数据:

Example 1:示例 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:示例 2:输入:

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.我正在从.txt 文件中获取数据。

    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.您可以通过使用另外 2 个匹配每对的循环来比较超过 2 个人。 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; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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