Skip to content

Instantly share code, notes, and snippets.

@rohit89
Created February 11, 2012 12:22
Show Gist options
  • Save rohit89/1799181 to your computer and use it in GitHub Desktop.
Save rohit89/1799181 to your computer and use it in GitHub Desktop.
Embedly Challenge #2
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