Created
January 27, 2009 22:45
-
-
Save mnutt/53618 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/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