Skip to content

Instantly share code, notes, and snippets.

@rogerbraun
Created February 10, 2012 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerbraun/1791212 to your computer and use it in GitHub Desktop.
Save rogerbraun/1791212 to your computer and use it in GitHub Desktop.
Embed.ly challenge
def check_number(n)
n_f = 1.upto(n).inject(&:*)
sum_of_digits = n_f.to_s.each_char.inject(0) do |res, v|
res += v.to_i
end
[n_f, sum_of_digits]
end
found = false
i = 1
while not found do
res = check_number(i)
puts "Checking #{i}!, is #{res[0]}, sum of digits #{res[1]}"
i += 1
found = true if res[1] == 8001
end
require "open-uri"
require "nokogiri"
url = "http://apply.embed.ly/static/data/2.html"
doc = Nokogiri::HTML(open(url))
article = doc.css("article").first
def mark_depth(start, n)
if start.name == "p" then
res = n
else
res = nil
end
children = start.children.map do |child|
mark_depth(child, n+1)
end
[res, children].flatten.compact
end
res = mark_depth(article, 1)
mean = res.inject(&:+) / res.count.to_f
deviations = res.map{|n| (n - mean) * (n - mean)}
deviations_avg = deviations.inject(&:+) / deviations.count.to_f
std_dev = Math.sqrt(deviations_avg)
puts std_dev
require "pry"
nums = 1.upto(900)
multiplier = nums.inject(&:+)
frequencies = nums.each_with_index.map{|n, index|
multiplier / (index + 1)
}
all = frequencies.inject(&:+)
half = all / 2
frequencies.each_with_index.inject(0) do |result, (value, index)|
result += value
if result >= half
puts index + 1
break
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment