Created
July 17, 2018 19:50
-
-
Save gregimba/beaa388e7d0f86244e88f688402cdfb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('underscore'); | |
// function sayHello() { | |
// console.log('Hello, World'); | |
// } | |
// _.times(5, sayHello); | |
// -- Start -- // | |
const employees_input = [ | |
'1,Richard,Engineering', | |
'2,Erlich,HR', | |
'3,Monica,Business', | |
'4,Dinesh,Engineering', | |
'6,Carla,Engineering', | |
'9,Laurie,Directors' | |
]; | |
const friendships_input = [ | |
'1,2', | |
'1,3', | |
'1,6', | |
'2,4' | |
]; | |
const expected_output = { | |
1: [2,3,6], | |
2: [1, 4], | |
3: [1], | |
4: [2], | |
6: [1], | |
9: [], | |
}; | |
let final = {}; | |
for(let item of employees_input){ | |
item = item.split(','); | |
final[item[0]] = []; | |
} | |
for(let connection of friendships_input){ | |
connection = connection.split(','); | |
final[connection[0]].push(connection[1]); | |
final[connection[1]].push(connection[0]); | |
} | |
// console.log(final); | |
let final_two = {}; | |
for(let department of employees_input){ | |
department = department.split(','); | |
department = department[2]; | |
if(final_two.hasOwnProperty(department) === false){ | |
final_two[department] = {'employees': 1, 'employees_with_outside_friends': 0}; | |
} else { | |
final_two[department].employees += 1; | |
} | |
} | |
for(let department in final_two){ | |
for(let employee of employees_input){ | |
employee = employee.split(','); | |
if(employee[2] === department){ | |
for(let friend of employees_input){ | |
friend = friend.split(','); | |
if(employee[2] !== friend[2]){ | |
if(final[employee[0]].includes(friend[0])){ | |
final_two[department]['employees_with_outside_friends'] += 1; | |
} | |
} | |
} | |
} | |
} | |
} | |
console.log(final_two); | |
const expected_output_2 = { | |
'Engineering':{ | |
'employees':3, | |
'count_of_employees_with_friends_in_other_departments':2 | |
}, | |
'HR':{ | |
'employees':1, | |
'count_of_employees_with_friends_in_other_departments':1 | |
}, | |
'Business':{'employees':1, 'employees_with_outside_friends':1}, | |
'Directors':{'employees':1, 'employees_with_outside_friends':0} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment