Skip to content

Instantly share code, notes, and snippets.

@csexton
Forked from anonymous/gist:77889
Created March 12, 2009 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csexton/77890 to your computer and use it in GitHub Desktop.
Save csexton/77890 to your computer and use it in GitHub Desktop.
def fill_in_the_gaps(date_array,value_array,num_of_values)
#out of 2 arrays, one
date_value_array = []
date_array.length.times do
date_value_array << [[Time.parse(date_array.pop.to_s).strftime("%Y-%m-%d")],[value_array.pop]]
end
# fill in the gaps with zeros
val_index = 0
dates = []
values = []
num_of_values += 1
num_of_values.times do |i|
dates << i.days.ago.to_date
#puts "date1 = #{dates[i]} and date2 = #{date_value_array[val_index][0]}"
if val_index < date_value_array.length && dates[i].to_s == date_value_array[val_index][0].to_s
values << date_value_array[val_index][1].to_s.to_f
val_index += 1
else
values << 0.0
end
end
values.reverse!
dates.reverse!
return dates, values
end
date_array = ['2009-03-10','2009-03-11','2009-03-12']
value_array = [1245,14500,2500]
num_of_values = 14
dates,values = fill_in_the_gaps(date_array,value_array,num_of_values)
puts dates
puts ""
puts values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment