Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution and Test for issue #58 #75

Merged
merged 3 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions solutions/58.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Max Fun Problem
// Return an array of employees that creates the maximum fun
// Input: Employee object
// Output: [Employee objects]

// Solution by Yoo Jin

class Employee {
constructor(funVal, staffArray=[]) {
this.funVal = funVal;
this.staff = [];
this.addStaff(staffArray);
}

addStaff(staffArray) {
staffArray.forEach((staff) => {
this.staff.push(staff);
});
}
}

/**
* Given an Employee, return an array of employees that creates the maximum fun.
* There is one condition: An Employee cannot be on the guestlist if his direct manager is going to the party.
* @param {Employee} emp - an Employee with fun value and a list of his staff
* @returns {Employee []]} list - Array of employees that creates the maximum fun
*/
const solution = (emp) => {
if(!emp) {
return [];
}

if(emp.staff.length === 0) {
return [emp];
}

let funStaffListWithoutEmp = [];
let funValWithoutEmp = emp.staff.reduce((funVal, staff) => {
let staffList = solution(staff);
funStaffListWithoutEmp = funStaffListWithoutEmp.concat(staffList);
return staffList.reduce((acc, emp) => (acc + emp.funVal), funVal);
}, 0);

let funValWithEmp = emp.funVal;
let funStaffListWithEmp = [emp];
emp.staff.forEach((staff) => {
funValWithEmp = staff.staff.reduce((funVal, staff) => {
let staffList = solution(staff);
funStaffListWithEmp = funStaffListWithEmp.concat(staffList);
return staffList.reduce((acc, emp) => (acc + emp.funVal), funVal);
}, funValWithEmp);
});

return funValWithoutEmp > funValWithEmp ?
funStaffListWithoutEmp : funStaffListWithEmp;
};

module.exports = {
Employee,
solution
};
26 changes: 26 additions & 0 deletions test/58.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const expect = require('chai').expect;
const solution = require('../solutions/58.js').solution;
const Employee = require('../solutions/58.js').Employee;
// solution = require('../yourSolution').solution;

describe('return an array of employees that creates the max fun', () => {
let emp1 = new Employee(1);
let emp2 = new Employee(5);
let emp3 = new Employee(11);
let emp4 = new Employee(10,[emp1]);
let emp5 = new Employee(2,[emp2,emp3]);
let emp6 = new Employee(4,[emp4,emp5]);

it('simplest case of one Employee object', () => {
expect(solution(emp1)).to.deep.equal([emp1]);
});
it('simple case of one Employee with Staff, where Employee has greater fun value', () => {
expect(solution(emp4)).to.deep.equal([emp4]);
});
it('simple case of one Employee with Staff, where staff have greater fun value', () => {
expect(solution(emp5)).to.deep.equal([emp2,emp3]);
});
it('hard case of one Employee with many staff', () => {
expect(solution(emp6)).to.deep.equal([emp4,emp2,emp3]);
});
});