varlxj

去面试,问了一个问题:

员工请假了几天,刚好这几天与法定节假日有重合,那么法定节假日那几天就不算请假,请问重合的天数有几天(或者说实际请假几天)

让在白板上写代码,居然脑子也白了……

一出门就清醒了,方案就出来,可惜晚了

 

require \'date\'

#方法一,通过间断节假日是否在请假区间内,计算出与节假日重合的开始和结束日期
def calc_date(holiday_start,holiday_end, leave_start, leave_end)
    holiday_region = holiday_start..holiday_end

    if holiday_region.include? leave_start or holiday_region.include? leave_end
        d1 = holiday_start > leave_start ? holiday_start : leave_start;
        d2 = holiday_end > leave_end ? leave_end : holiday_end;
        return (d2-d1).to_i + 1
    end
    0
end

#方法二.直接求两个区间的交集,看交集有几个元素就是几天
def calc_date2(holiday_start,holiday_end, leave_start, leave_end)
    leave = holiday_start..holiday_end
    holiday = leave_start..leave_end
    (holiday.to_a & leave.to_a).length
end

#假期起始
holiday_start = Date.new(2015,9,22)
holiday_end = Date.new(2015,9,25)
#请假起始
leave_start = Date.new(2015,9,23)
leave_end = Date.new(2015,9,24)

puts calc_date(holiday_start,holiday_end, leave_start, leave_end)
puts calc_date2(holiday_start,holiday_end, leave_start, leave_end)

 

分类:

技术点:

相关文章:

  • 2021-06-13
  • 2021-08-24
  • 2021-06-15
  • 2021-06-01
  • 2021-05-31
  • 2021-09-25
  • 2021-08-12
  • 2022-12-23
猜你喜欢
  • 2021-10-20
  • 2022-02-24
  • 2022-01-02
  • 2021-08-16
  • 2022-02-20
  • 2021-10-01
相关资源
相似解决方案