Created
February 20, 2012 19:29
-
-
Save jah2488/1870886 to your computer and use it in GitHub Desktop.
Embed.ly Code Challenge
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
#! /usr/bin/env ruby | |
# Part 1 | |
found = false | |
$num = 0 | |
def calc_num(num) | |
i = num - 1 | |
new_num = num | |
while i > 0 do | |
new_num = new_num * i | |
i -= 1 | |
end | |
return new_num | |
end | |
def digits_summed(calc) | |
i = 0 | |
calc = calc.to_s | |
calc_sum = 0 | |
puts "calc = #{calc}" | |
while i < calc.length do | |
calc_sum += calc[i].to_i | |
i += 1 | |
end | |
puts "R(n) of n! of #{calc} = #{calc_sum} (n = #{$num})" | |
return calc_sum.to_i | |
end | |
until found | |
calc = calc_num($num) | |
puts "n = #{$num} n! = #{calc}" | |
sum = digits_summed(calc) | |
found = true if sum == 8001 | |
$num += 1 | |
end |
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
#! /usr/bin/env ruby | |
# Part 2 | |
require 'open-uri' | |
require 'nokogiri' | |
require 'pry' | |
doc = Nokogiri::HTML(open('http://apply.embed.ly/static/data/2.html')) | |
article = doc.css('article').to_html | |
$p_array = [] | |
def get_depth(node, depth=1) | |
if node.parent.name == "article" | |
depth += 1 | |
return depth | |
else | |
get_depth(node.parent, (depth += 1)) | |
end | |
end | |
def check_children(node) | |
if node.children.length > 0 | |
for children in node.children | |
traverse_tree(children) | |
end | |
end | |
end | |
def traverse_tree(node) | |
if node.name == "p" | |
if node.parent.name == "article" | |
$p_array << 1 | |
else | |
$p_array.push(get_depth(node)) | |
end | |
else | |
check_children(node) | |
end | |
end | |
doc.css('article').each do |p| | |
for node in p.children do | |
traverse_tree(node) | |
end | |
end | |
def get_SD(data) | |
#Calculate The Mean | |
sum = data.inject(:+) | |
mean = sum / data.length | |
#Copy Array against Mean | |
calc_array = data.each {|n| (n - mean)**2 } | |
#Average the new array | |
nsum = calc_array.inject(:+) | |
nmean = nsum / calc_array.length | |
#Take the Square root | |
return Math.sqrt(nmean) | |
end | |
print $p_array | |
puts "" | |
puts get_SD($p_array) |
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
#! /usr/bin/env ruby | |
# Part 3 | |
given_text = {unique_words: 900, highest_word: 2520} | |
x = 2520 | |
i = 1 | |
$frq = [] | |
while i < given_text[:unique_words] do | |
$frq.push(x/i) | |
i+=1 | |
end | |
text_length = $frq.inject(:+) | |
puts text_length | |
word_count = 0 | |
pos = 0 | |
print $frq | |
half_length = (text_length/2) | |
puts half_length | |
until word_count > half_length do | |
word_count += $frq[pos] | |
puts "word count #{word_count} with array pos #{pos}" | |
pos += 1 | |
end | |
puts "Half of Text Lenght: #{text_length/2}, word_cound: #{word_count}, used #{pos} words." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment