Created
May 30, 2014 09:20
-
-
Save dougdroper/e6e4dbbb950038eeb537 to your computer and use it in GitHub Desktop.
employee
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
require 'csv' | |
class Tree | |
def initialize(employees) | |
@managers = employees.group_by {|e| e["Manager"] } | |
@employee_names = employees.map {|e| e["Employee Name"]} | |
(@managers.keys.compact - @employee_names).each do |name| | |
@managers[nil] << {"Employee Name" => name} | |
end | |
end | |
def walk(name=nil,level=0, &block) | |
@managers.fetch(name, []).each do |employee| | |
yield employee, level | |
walk(employee["Employee Name"], level + 1, &block) | |
end | |
end | |
end | |
employees = CSV.read('sherwood_plc.csv', :headers => true) | |
Tree.new(employees).walk do |employee, level| | |
puts ">> " * level + employee["Employee Name"] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your company, Sherwood Plc, is looking to obtain Investor in
People status. To achieve this they need to understand the
current company structure; who reports to who, how much people
are being paid, etc. The HR director has asked the IT team
to use the company HR database to help provide this information.
The task:
You will need to create a short program to read in the data
from the HR database and build up a structured model of the
employees described in it.
The HR database is a CSV file, with one employee per row of the
file and a single header row describing the columns; the employee
name, their line managers name, their department and their salary.
Below is some example data:
Employee Name,Manager,Department,Salary
Robin Hood,,,200
Friar Tuck,Robin Hood,Wealth Redistribution,100
Little John,Robin Hood,Wealth Redistribution,100
In this file, Robin Hood is the manager of both Friar Tuck and
Little John. They all work in the wealth redistribution
department.
N.B. In the data file, there's NO guarantee as to the order the
employees appear in.
What we're looking for:
We're looking for a simple ruby program that takes a file as an
argument and outputs the employee structure. Maybe something like:
$ ruby employee_structure.rb merry_men.csv
Robin Hood (no department)
Our goal in setting this task is to get a better understanding of
your style of coding and how you write software, so write your
solution with that in mind.
Feel free to use:
Here are some more questions to consider if you have time:
by department, or by salary?
This is the CSV file:
Employee Name,Manager,Department,Salary
Robin Hood,,,200
Friar Tuck,Robin Hood,Wealth Redistribution,100
Little John,Robin Hood,Wealth Redistribution,100
Guy Gisbourne,The Sherrif,Villainy and Acquisitions,130
The Sherrif,Prince John,Villainy and Acquisitions,200
Prince John,,,300
Maid Marion,Robin Hood,Merrie Management,130
Will Scarlett,Maid Marion,Merrie Management,90