开发者

Ruby task: joining numbers to intervals

开发者 https://www.devze.com 2023-01-20 14:17 出处:网络
I\'ve got an array of uniq numbers. Like this: [1,2,3,4,7,8,10,12]. It can be unsorted. What I need is to get intevals for this array:

I've got an array of uniq numbers. Like this: [1,2,3,4,7,8,10,12]. It can be unsorted. What I need is to get intevals for this array:

intervals_for [1,2,3,4,7,8,10,12]
#=> "1-4, 7-8, 10,12"

I've got my own solution:

def intervals_for(array)
  array.s开发者_JS百科ort!
  new_array = []
  array.each do |a|
    if new_array.last and a == new_array.last.last+1
      new_array.last << a
    else
      new_array << [a]    
    end
  end
  new_array.map{|a| a.size > 1 ? "#{a.first}-#{a.last}" : a.first}.join(", ")
end

But I think somewhere here is more clean solution


here's mine, using ver 1.9.1

def torange(a)
  r=[];s=a[0]
  a.uniq.sort!.each_cons(2) do |a|
      r<<[s,a[0]] and s=a[1] if a[1]-a[0]!=1
  end
  left=a.index(s)
  r<<[a[left..-1][0],a[left..-1][-1]]
end

torange([1,2,3,4,7,8,10,12]).each do |x|
  puts x[0]==x[1] ? "#{x[0]}" : "#{x[0]}-#{x[1]}"
end

output

$ ruby test.rb
1-4
7-8
10
12


Here is my one-liner:

array = [-10, -9, -1, 0, 1, 2, 3, 4, 10, 11, 15, 133]
array.uniq.sort.inject([]){ |a, e| a[-1] && a[-1].last && a[-1].last == e-1 ? a[-1] = (a[-1].first..e) : a << (e..e); a }
#=> [-10..-9, -1..4, 10..11, 15..15, 133..133]

And little monkeypatchng

class Array
  def.collect_to_ranges
    self.uniq.sort.inject([]){ |a, e| a[-1] && a[-1].last && a[-1].last == e-1 ? a[-1] = (a[-1].first..e) : a << (e..e); a }
  end
end

array = [1,2,3,4,5,6,7,8, 10]
array.collect_to_ranges
#=> [1..8, 10..10]


This one is recursive, feels like it could be better though...

arr =  [1,2,3,4,7,8,10,12]

def intervals(arr)
  return arr if arr.size == 0 || arr.size == 1

  int = [arr.shift]
  while int.last + 1 == arr.first
    int << arr.shift
  end

  ( int.size == 1 ? int : ["#{int.first}-#{int.last}"] ) + intervals(arr)
end

p intervals(arr)
0

精彩评论

暂无评论...
验证码 换一张
取 消