Skip to content

Instantly share code, notes, and snippets.

@stevegraham
Created March 25, 2010 12:23
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 stevegraham/343487 to your computer and use it in GitHub Desktop.
Save stevegraham/343487 to your computer and use it in GitHub Desktop.
# Run this file in the same directory as weather.dat
class NOAAParser
def initialize(file)
# Reject all lines in file that don't begin with whitespace and integer
@lines = IO.readlines(file).select { |line| line =~ /^\s+\d/ }
end
def day_with_lowest_temp_spread
# Maybe this regexp could be cleaner?
@days = @lines.collect { |line| /^\s+(\d+)\s+(\d+\*?)\s+(\d+\*?)/.match(line).to_a }
# Iterate over days, and coerce in place each item in the area into an integer.
@days.each { |day| day.map! &:to_i }
# Compare the temp spreads and return the lowest one.
@days.min { |a,b| a[2]-a[3] <=> b[2]-b[3] }[1]
end
end
puts NOAAParser.new('weather.dat').day_with_lowest_temp_spread
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment