Skip to content

Instantly share code, notes, and snippets.

Created February 13, 2012 14:05
Show Gist options
  • Save anonymous/1817153 to your computer and use it in GitHub Desktop.
Save anonymous/1817153 to your computer and use it in GitHub Desktop.
### Problem 1
def fact(number)
(1..number).inject(1) { |product, n| product * n }
end
def getR(number)
fact(number).to_s.split('').inject(0) { |sum, n| sum + n.to_i }
end
def find_lowest_value
value = 0
loop do
getR(value) == 8001 ? break : value += 1
end
value
end
puts find_lowest_value # 787
### Problem 2
def standard_dev(values)
length = values.length
mean = (values.inject(0) { |sum, v| sum + v }) / length
diferences = values.map { |v| (v - mean) ** 2 }
Math.sqrt((diferences.inject(0) { |sum, v| sum + v }) / length)
end
require 'nokogiri'
require 'open-uri'
def get_paragraph_depths
url = "http://apply.embed.ly/static/data/2.html"
doc = Nokogiri.HTML(open(url))
diference = 0
values = []
doc.css('article').each do |article|
diference = article.at_css('p').ancestors.size
article.css('p').each do |paragraph|
values << paragraph.ancestors.size - (diference - 1)
end
end
values
end
values = get_paragraph_depths
p standard_dev(values) # 1.4142135623730951
### Problem 3
# x is the frequency of the most frequently used word
# x = 900 / (1 + 1/2 + 1/3 + ... + 1/900)
def getX
900 / (1..900).inject(0) { |sum, n| sum + 1.0 / n }
end
def getCount
x = getX
sum, count = 0, 1
loop do
sum += x / count
break if sum >= 450
count += 1
end
count
end
p getCount # 22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment