Last active
January 10, 2019 00:47
-
-
Save carlows/aaa9ca76f8b84c77a047a6651204b76d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('csv') | |
class AnnualWeather | |
def initialize (file_name) | |
@readings = [] | |
CSV.foreach(file_name, headers: true) do |row| | |
@readings << { | |
:date => Date.parse(row[2]), | |
:high => row[10].to_f, | |
:low => row[11].to_f, | |
} | |
end | |
end | |
def mean | |
return 0.0 if @readings.size.zero? | |
total = @readings.reduce(0.0) do |sum, reading| | |
sum + (reading[:high] + reading[:low]) / 2.0 | |
end | |
total / @readings.size.to_f | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment