Created
February 10, 2012 20:29
-
-
Save dhm116/1792548 to your computer and use it in GitHub Desktop.
Python-based solvers for the embed.ly challenge
This file contains hidden or 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
from __future__ import division | |
import urllib2 | |
from BeautifulSoup import BeautifulSoup | |
import numpy as np | |
def notN(num): | |
result = num | |
while num > 1: | |
result = result * (num - 1) | |
num -= 1 | |
return result | |
def R(num): | |
val = str(notN(num)) | |
sum = 0 | |
for digit in val: | |
sum += int(digit) | |
return sum | |
def getP(node, level): | |
results = [] | |
for tag in node: | |
if hasattr(tag, 'name'): | |
if tag.name == 'p': | |
results.append(level) | |
child_results = getP(tag, level + 1) | |
results.extend(child_results) | |
return results | |
i = 10 | |
val = R(i) | |
while val < 8001: | |
i += 1 | |
val = R(i) | |
print 'Answer = {}'.format(i) | |
s = BeautifulSoup(urllib2.urlopen('http://apply.embed.ly/static/data/2.html')) | |
vals = getP(s, 0) | |
print vals | |
print 'Answer = {:.1f}'.format(np.std(vals)) | |
unique = 900 | |
num1 = 2520 | |
total_words = 0 | |
for x in xrange(unique): | |
x += 1 | |
total_words += num1/x | |
print total_words | |
half = 0 | |
for x in xrange(unique): | |
x += 1 | |
half += num1/x | |
if half / total_words >= 0.5: | |
print half / total_words | |
print 'Answer = {}'.format(x) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apparently it took me 36 minutes to throw this together