在Ruby中,我该如何将一个由表示整数或范围的令牌数组解析为包含每个整数和每个范围中的每个元素的整数数组?
例如:给定输入
谢谢。
例如:给定输入
[ "5", "7-10", "24", "29-31"]
,我想要产生输出[ 5, 7, 8, 9, 10, 24, 29, 30, 31 ]
。谢谢。
[ "5", "7-10", "24", "29-31"]
,我想要产生输出[ 5, 7, 8, 9, 10, 24, 29, 30, 31 ]
。[ "5", "7-10", "24", "29-31"].map{|x| x.split("-").map{|val| val.to_i}}.map{ |y| Range.new(y.first, y.last).to_a}.flatten
类似以下代码应该可以工作。只需将您的输入传递到方法中并获取一个整数数组即可。我故意保持它冗长,以便您可以看到逻辑。
编辑:我已经在代码中添加了注释。
def generate_output(input)
output = []
input.each do |element|
if element.include?("-")
# If the number is a range, split it
split = element.split("-")
# Take our split and turn it into a Ruby Range object, then an array
output << (split[0].to_i..split[1].to_i).to_a
else
# If it's not a range, just add it to our output array
output << element.to_i
end
end
# Since our ranges will add arrays within the output array, calling flatten
# on it will make it one large array with all the values in it.
return output.flatten
end
嗯,实际上这可能需要一些工作。我现在会试着解决它:
def parse_argv_list(list)
number_list = []
list.each do |item|
if item.include?('-')
bounds = item.split('-')
number_list.push((bounds[0].to_i..bounds[1].to_i).to_a)
else
number_list.push(item.to_i)
end
end
number_list.flatten
end
>> [ "5", "7-10", "24", "29-31"].map{|x|x.gsub!(/-/,"..");x[".."]?(eval x).to_a : x.to_i}.flatten
=> [5, 7, 8, 9, 10, 24, 29, 30, 31]