This question shows research effort; it is useful and clear
1
This question does not show any research effort; it is unclear or not useful
Bookmark this question.
Show activity on this post.
我有一个问题,红宝石代码:
def give_me_all_periods(period, paid_periods)
# Can you help me?
end
period = [1..10]
paid_periods = [1..2, 5..8]
give_me_all_periods(period, paid_periods).should == [1...2, 2...5, 5...8, 8...10]
def give_me_all_periods(period, paid_periods)
p = period | paid_periods
union = p.inject([]){|u,x| u = u|range_to_arr(x)}.sort
ranges =[]
union.each_cons(2){|a| ranges << Range.new(a[0],a[1]) }
ranges
end
def range_to_arr(r)
[r.first,r.last]
end