Created
September 13, 2010 21:53
-
-
Save joecorcoran/578123 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
def smallest_temperature_spread | |
# store all munged values in a hash | |
map = {} | |
File.open("weather.dat", "r") do |file| | |
# iterate each line | |
while line = file.gets | |
# only use relevant lines | |
if line.match(/^\s+\d+\s+[\d\*]+\s+/) | |
# seems appropriate to convert keys to integers here for possible future use | |
day = line.split[0].to_i | |
spread = (line.split[1].to_i-line.split[2].to_i).abs | |
map[day] = spread | |
end | |
end | |
end | |
# sort the spread values, index 0 will be the smallest | |
# there may be occasions when there are joint smallest spreads - | |
# we could check for this with Hash#select and return an array instead | |
smallest_spread = map.values.sort[0] | |
smallest_spread_day = map.index(smallest_spread) | |
puts "Minimum temperature spread was "+smallest_spread.to_s+", from day "+smallest_spread_day.to_s | |
smallest_spread_day | |
end |
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
# not goal difference, strictly speaking, so let's call it spread again! | |
def smallest_goal_spread | |
map = {} | |
File.open("football.dat", "r") do |file| | |
while line = file.gets | |
if line.match(/^\s+[\d\.]+\s+/) | |
map[line.split[1]] = (line.split[6].to_i - line.split[8].to_i).abs | |
end | |
end | |
end | |
goal_spread = map.values.sort[0] | |
team_name = map.index(goal_spread) | |
puts "Smallest spread between goals for and against was "+goal_spread.to_s+", by "+team_name | |
team_name | |
end |
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
module Spread | |
# refactored to work with either file and to calculate spread on any two columns | |
def smallest(file_path, columns = { :return => 0, :first => 0, :second => 1 }) | |
map = {} | |
File.open(file_path, "r") do |file| | |
while line = file.gets | |
# this regexp works for both sets of data | |
if line.match(/^\s+[\d\.]+\s+/) | |
map[line.split[columns[:return]]] = (line.split[columns[:first]].to_i - line.split[columns[:second]].to_i).abs | |
end | |
end | |
end | |
spread = map.values.sort[0] | |
return_value = map.index(spread) | |
#puts "Smallest spread was "+spread.to_s+", from "+return_value | |
return_value | |
end | |
end |
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 '3a_refactor' | |
require 'test/unit' | |
class SpreadTest < Test::Unit::TestCase | |
include Spread | |
def test_smallest_temperature_spread | |
assert_equal "14", smallest("weather.dat", { :return => 0, :first => 1, :second => 2 }) | |
end | |
def test_smallest_football_goal_spread | |
assert_equal "Aston_Villa", smallest("football.dat", { :return => 1, :first => 6, :second => 8 }) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment