Created
February 29, 2012 22:52
-
-
Save Canx/1945149 to your computer and use it in GitHub Desktop.
kata 4: data munging (part 3)
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
class TabFile < File | |
def initialize(file, regexp) | |
super(file, "r") | |
@regexp = regexp | |
end | |
def tab_data | |
data = Array.new | |
self.each { |line| | |
line_match = @regexp.match(line) | |
if !line_match.nil? then | |
data << line_match.captures.collect { |v| v.match(/^\d+$/)? v.to_i : v } | |
end | |
} | |
return data | |
end | |
end | |
puts TabFile.new("weather.dat", /^\s+([0-9]+)\s+([0-9]+)[*]?\s+([0-9]+)[*]?/).tab_data.min {|a,b| a[1]-a[2] <=> b[1]-b[2] }[0] | |
puts TabFile.new("football.dat", /^\s+[0-9]+[.]\s([a-zA-Z_]+)\s+[0-9]+\s+[0-9]+\s+[0-9]+\s+[0-9]+\s+([0-9]+)\s+-\s+([0-9]+)/).tab_data.min { |a,b| (a[1]-a[2]).abs <=> (b[1]-b[2]).abs }[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment