Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mnutt
Created January 27, 2009 22:45
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 mnutt/53618 to your computer and use it in GitHub Desktop.
Save mnutt/53618 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Kata #4: Football Data
def smallest_spread(file)
weather_data = File.open(file).readlines
temp_spread = weather_data.map {|line|
next unless line.match(/\s([A-Za-z_]+).*?(\d+)\s\s-\s\s(\d+)/)
day, max, min = $1, $2.to_i, $3.to_i
[day, (max - min).abs]
}
puts temp_spread.compact.sort_by {|k, v| v }.first
end
smallest_spread("football.dat")
#!/usr/bin/env ruby
# Kata #4: Refactored Data
def spread(file, regex)
data = File.open(file).readlines
spread = data.map {|line|
next unless line.match(regex)
day, max, min = $1, $2.to_i, $3.to_i
[day, (max - min).abs]
}
spread.compact.sort_by {|k, v| v }
end
puts spread("football.dat", /\s([A-Za-z_]+).*?(\d+)\s\s-\s\s(\d+)/).first.join(", ")
puts spread("weather.dat", /^\s+(\d+)\s+(\d+)\s+(\d+)/).last.join(", ")
#!/usr/bin/env ruby
# Kata #4: Weather Data
def smallest_spread(file)
weather_data = File.open(file).readlines
temp_spread = weather_data.map {|line|
next unless line.match(/^\s+(\d+)\s+(\d+)\s+(\d+)/)
day, max, min = $1.to_i, $2.to_i, $3.to_i
[day, max-min]
}
puts temp_spread.compact.sort_by {|k, v| v }.last
end
smallest_spread("weather.dat")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment