Discussion:
Ruby Quiz - Challenge #3 - Read the English Premier League (EPL) Standings Table from a Web Page
Gerald Bauer
2018-11-02 18:44:13 UTC
Permalink
Hello,

Let's honor the tradition and post a new Ruby Quiz [1] every Friday.
Here we go:

Challenge #3 - Read the English Premier League (EPL) Standings Table
from a Web Page [2]

Let's read the English Premier League (EPL) Standings Table
from a web page. Let's use the British Broadcasting Corp (BBC) page @
bbc.com/sport/football/premier-league/table [3]

The challenge: Code a parse method that passes the RubyQuizTest :-).

def parse( html )
# ...
end

Start from scratch or, yes, use any library / gem you can find.
Scrap the standings table (as an array of array of string values) e.g.:

[["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"]]

from the html table that you can find inside the web page's source.


To qualify for solving the code challenge / puzzle you must pass the test:

require 'minitest/autorun'


class RubyQuizTest < MiniTest::Test

def test_parse

#######
# Pos Team P W D L F A GD Pts

lines =<<TXT
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
TXT

table = []
lines.each_line {|line| table << line.split }
pp table

assert_equal table, parse( File.read( "./pages/bbc.html" ))

end # method test_parse
end # class RubyQuizTest

Post your code snippets on the "official" Ruby Quiz Channel,
that is, the ruby-talk mailing list.

Note: For easy reference and testing you can use the "local"
page, see pages/bbc.html in the ruby quiz repo.


Bonus: You're welcome to use a different web page
or a different football league.

[1] https://github.com/planetruby/quiz
[2] https://github.com/planetruby/quiz/tree/master/003
[3] https://www.bbc.com/sport/football/premier-league/table

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Frank J. Cameron
2018-11-03 01:08:45 UTC
Permalink
Post by Gerald Bauer
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')



Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Gerald Bauer
2018-11-04 22:20:32 UTC
Permalink
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

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Loading...