-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworking_experience.rb
45 lines (37 loc) · 1.05 KB
/
working_experience.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require 'date'
# Class to hold and calculate total working experience over career
class WorkingExperience
# START_TIME = ::Date.new(1999, 01, 01)
# END_TIME = ::Date.new(2020, 12, 31)
# MONTHS ||= (START_TIME..END_TIME).select { |d| d.day == 1 }
attr_reader :job_ranges, :experience
def initialize(args)
@job_ranges = prepare_date_ranges(args)
@experience = {}
end
def prepare_date_ranges(jobs)
jobs.map do |e|
e.split('-').map { |x| ::Date.parse(x) }
end
end
def update_work_experience
job_ranges.each do |job|
job_start, job_end = job
months_worked = (job_start..job_end).select { |d| d.day == 1 }
months_worked.each { |x| experience[x] = true }
end
self
end
def calculate_work_experience
update_work_experience if experience.empty?
experience.count { |_, v| v == true }
end
def years_of_experience
(calculate_work_experience / 12).floor
end
end
ARGF.each_line do |line|
years = line.chomp.split('; ')
obj = WorkingExperience.new(years)
puts obj.years_of_experience
end