Re: Ruby Quiz - Challenge #3 - Read the English Premier League (EPL) Standings Table from a Web Page


(Frank J. Cameron) #1

Gerald Bauer wrote:

Challenge #3 - Read the English Premier League (EPL) Standings Table

from a Web Page

$ ruby lib/003.rb
Run options: --seed 2712
# Running:
[["1", "Man City", "10", "8", "2", "0", "27", "3", "24", "26"],
["2", "Liverpool", "10", "8", "2", "0", "20", "4", "16", "26"],
["3", "Chelsea", "10", "7", "3", "0", "24", "7", "17", "24"],
["4", "Arsenal", "10", "7", "1", "2", "24", "13", "11", "22"],
["5", "Tottenham", "10", "7", "0", "3", "16", "8", "8", "21"],
["6", "Bournemouth", "10", "6", "2", "2", "19", "12", "7", "20"], ["7",
"Watford", "10", "6", "1", "3", "16", "12", "4", "19"],
["8", "Man Utd", "10", "5", "2", "3", "17", "17", "0", "17"],
["9", "Everton", "10", "4", "3", "3", "16", "14", "2", "15"],
["10", "Wolves", "10", "4", "3", "3", "9", "9", "0", "15"],
["11", "Brighton", "10", "4", "2", "4", "11", "13", "-2", "14"],
["12", "Leicester", "10", "4", "1", "5", "16", "16", "0", "13"],
["13", "West Ham", "10", "2", "2", "6", "9", "15", "-6", "8"],
["14", "Crystal Palace", "10", "2", "2", "6", "7", "13", "-6", "8"],
["15", "Burnley", "10", "2", "2", "6", "10", "21", "-11", "8"],
["16", "Southampton", "10", "1", "4", "5", "6", "14", "-8", "7"], ["17",
"Cardiff", "10", "1", "2", "7", "9", "23", "-14", "5"],
["18", "Fulham", "10", "1", "2", "7", "11", "28", "-17", "5"],
["19", "Newcastle", "10", "0", "3", "7", "6", "14", "-8", "3"],
["20", "Huddersfield", "10", "0", "3", "7", "4", "21", "-17", "3"]]
.
Finished in 0.111217s, 8.9914 runs/s, 8.9914 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

$ cat lib/003.rb
require_relative '../003/test.rb'
require 'hpricot'
class RubyQuizTest
  def parse(html)
    Hpricot(html).search('table/tbody/tr').map do |tr|
      tr.search('td').map.each_with_index do |td, i|
        case i
        when 0, 2
          td.inner_text
        when 1
        else
          Integer(td.inner_text).to_s rescue nil
        end
      end.compact
    end
  end
end
Dir.chdir("#{__dir__}/../003")
RubyQuizTest.new('fjc')

$ cat lib/003.rb
require_relative '../003/test.rb'
require 'oga'
class RubyQuizTest
  def parse(html)
    Oga.parse_html(html).xpath('table/tbody/tr').map do |tr|
      tr.xpath('td').map.each_with_index do |td, i|
        case i
        when 0, 2
          td.text
        when 1
        else
          Integer(td.text).to_s rescue nil
        end
      end.compact
    end
  end
end
Dir.chdir("#{__dir__}/../003")
RubyQuizTest.new('fjc')


(Gerald Bauer) #2

Hello,
   Thanks for posting your code snippets and helping getting the Ruby
Quiz started. I've added all your ruby scripts for challenge #1,#2,
and #3 to the solutions [1][2][3]. Keep it up. Cheers. Prost.

PS: To everyone - all quizzes are open - send in your code snippets!

[1] https://github.com/planetruby/quiz/blob/master/001/solution.rb
[2] https://github.com/planetruby/quiz/blob/master/002/solution.rb
[3] https://github.com/planetruby/quiz/blob/master/003/solution.rb