Skip to content

Instantly share code, notes, and snippets.

@agaelebe
Created August 8, 2008 23:34
Show Gist options
  • Save agaelebe/4639 to your computer and use it in GitHub Desktop.
Save agaelebe/4639 to your computer and use it in GitHub Desktop.
calcula a idade media das pessoas que responderam no post do Carlos Brando
require 'rubygems'
require 'hpricot'
require 'open-uri'
doc = Hpricot(open("http://www.nomedojogo.com/2008/08/08/pesquisa-quantos-anos-voce-tem/"))
class Array
def sum
inject(0){ |sum,x| sum ? sum+x : x }
end
def mean
(size > 0) ? sum.to_f / size : 0
end
end
ages = []
(doc/"div.commenttext//p").each do |elem|
match_age = /(\s|\b|\t)\d{2}(\s|\b|\t)/.match(elem.to_s)
ages << match_age[0].to_i unless match_age.nil?
end
puts "Total de pessoas: #{ages.size}"
printf "Idade media: %.2f" , ages.mean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment