Commit 5800691
Changed files (2)
spec
fixtures
kata
spec/fixtures/football.dat
@@ -0,0 +1,22 @@
+ Team P W L D F A Pts
+ 1. Arsenal 38 26 9 3 79 - 36 87
+ 2. Liverpool 38 24 8 6 67 - 30 80
+ 3. Manchester_U 38 24 5 9 87 - 45 77
+ 4. Newcastle 38 21 8 9 74 - 52 71
+ 5. Leeds 38 18 12 8 53 - 37 66
+ 6. Chelsea 38 17 13 8 66 - 38 64
+ 7. West_Ham 38 15 8 15 48 - 57 53
+ 8. Aston_Villa 38 12 14 12 46 - 47 50
+ 9. Tottenham 38 14 8 16 49 - 53 50
+ 10. Blackburn 38 12 10 16 55 - 51 46
+ 11. Southampton 38 12 9 17 46 - 54 45
+ 12. Middlesbrough 38 12 9 17 35 - 47 45
+ 13. Fulham 38 10 14 14 36 - 44 44
+ 14. Charlton 38 10 14 14 38 - 49 44
+ 15. Everton 38 11 10 17 45 - 57 43
+ 16. Bolton 38 9 13 16 44 - 62 40
+ 17. Sunderland 38 10 10 18 29 - 51 40
+ -------------------------------------------------------
+ 18. Ipswich 38 9 9 20 41 - 64 36
+ 19. Derby 38 8 6 24 33 - 63 30
+ 20. Leicester 38 5 13 20 30 - 64 28
spec/kata/weather_data_spec.rb
@@ -5,6 +5,25 @@ require 'ostruct'
#Download this text file, then write a program to output the day number (column one)
#with the smallest temperature spread (the maximum temperature is the second column, the minimum the third column).
+class Team
+ include Comparable
+ attr_reader :spread, :name
+
+ def initialize(name:, goals_for:, goals_against:)
+ @name = name
+ @spread = (goals_for - goals_against).abs
+ end
+
+ def <=>(other)
+ @spread <=> other.spread
+ end
+
+ def self.map_from(row)
+ return nil if row[1].nil?
+ Team.new(name: row[1], goals_for: row[6].to_i, goals_against: row[8].to_i)
+ end
+end
+
class Day
include Comparable
attr_reader :day, :spread
@@ -36,7 +55,9 @@ class Spread
file.each_line.with_index do |line, index|
next if index == 0
next if line.chomp.empty?
- yield mapper.map_from(line.split)
+ row = mapper.map_from(line.split)
+ next if row.nil?
+ yield row
end
end
end
@@ -53,10 +74,22 @@ class Spread
attr_reader :file_path, :mapper
end
-describe "spread" do
+describe "weather.dat" do
subject { Spread.new('weather.dat', Day) }
it 'returns the day with the smallest temperature spread' do
expect(subject.min.day).to eql('14')
end
end
+
+# Part Two: Soccer League Table
+# The file football.dat contains the results from the English Premier League for 2001/2.
+# The columns labeled ‘F’ and ‘A’ contain the total number of goals scored for and against each team in that season (so Arsenal scored 79 goals against opponents, and had 36 goals scored against them). Write a program to print the name of the team with the smallest difference in ‘for’ and ‘against’ goals.
+#
+describe "football.dat" do
+ subject { Spread.new('football.dat', Team) }
+
+ it 'returns the day with the smallest temperature spread' do
+ expect(subject.min.name).to eql('Aston_Villa')
+ end
+end