Created
February 11, 2012 12:22
-
-
Save rohit89/1799181 to your computer and use it in GitHub Desktop.
Embedly Challenge #2
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 'rubygems' | |
require 'hpricot' | |
require 'net/http' | |
def standard_deviation arr | |
mean = arr.each.inject(0) {|sum, i| sum += i; sum} / arr.length.to_f | |
new_arr = arr.each.map{|i| (i - mean) ** 2} | |
(new_arr.each.inject(0) {|sum, i| sum += i; sum} / new_arr.length.to_f) ** 0.5 | |
end | |
@depth = [] | |
def find element, level | |
element.search("/").each do |r| | |
if r.name == "p" | |
@depth << level | |
else | |
find r, level + 1 | |
end | |
end | |
end | |
page = Net::HTTP.get_response(URI.parse("http://apply.embed.ly/static/data/2.html")).body | |
hp = Hpricot(page) | |
hp.search("article").each do |row| | |
find row, 1 | |
end | |
puts standard_deviation @depth |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment