Created
February 10, 2012 21:19
-
-
Save papaver/1792968 to your computer and use it in GitHub Desktop.
Embedly Programming 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 -w | |
#------------------------------------------------------------------------------ | |
# requires | |
#------------------------------------------------------------------------------ | |
require 'open-uri' | |
#------------------------------------------------------------------------------ | |
# Problem #1 | |
#------------------------------------------------------------------------------ | |
def problem_1 | |
((0..1000).map{ |x| { :d => x, :v => (((1..x).inject:*).to_s.split('').map{ |d| d.to_i }.inject:+) } }).find_all{ |h| (h[:v] == 8001) }[0][:d] | |
end | |
#------------------------------------------------------------------------------ | |
# Problem #2 | |
#------------------------------------------------------------------------------ | |
# Note: standard deviation code copied from: | |
# https://www.bcg.wisc.edu/webteam/support/ruby/standard_deviation | |
# Add methods to Enumerable, which makes them available to Array | |
module Enumerable | |
# sum of an array of numbers | |
def sum | |
return self.inject(0){|acc,i|acc+i} | |
end | |
# average of an array of numbers | |
def average | |
return self.sum/self.length.to_f | |
end | |
# variance of an array of numbers | |
def sample_variance | |
avg=self.average | |
sum=self.inject(0){|acc,i|acc+(i-avg)**2} | |
return(1/self.length.to_f*sum) | |
end | |
# standard deviation of an array of numbers | |
def standard_deviation | |
return Math.sqrt(self.sample_variance) | |
end | |
end # module Enumerable | |
#------------------------------------------------------------------------------ | |
def problem_2 | |
open('http://apply.embed.ly/static/data/2.html'){ |f| f.read }.split("\n").grep(/(<article>|<\/article>|<div>|<\/div>|<p>)/).inject({:i=>0, :l=>[]}) { |h,x| h[:i] += x =~ /(<article>|<div>)/ ? 1 : x =~ /(<\/article>|<\/div>)/ ? -1 : 0; h[:l] += [h[:i]] if x =~ /<p>/; h }[:l].standard_deviation | |
end | |
#------------------------------------------------------------------------------ | |
# Problem #3 | |
#------------------------------------------------------------------------------ | |
def problem_3 | |
([900*900]*900).zip(1..900).map{ |x| x[0]/x[1] }.inject([[0, 0]]){ |l,v| l += [[v,l[-1][1]+v]] }.reject{ |x| x[1] > 2988774 }.count | |
end | |
#------------------------------------------------------------------------------ | |
# main | |
#------------------------------------------------------------------------------ | |
puts "Problem #1: #{problem_1}" | |
puts "Problem #2: #{problem_2}" | |
puts "Problem #3: #{problem_3}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment