Skip to content

Instantly share code, notes, and snippets.

@dhm116
Created February 10, 2012 20:29
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 dhm116/1792548 to your computer and use it in GitHub Desktop.
Save dhm116/1792548 to your computer and use it in GitHub Desktop.
Python-based solvers for the embed.ly challenge
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
@dhm116
Copy link
Author

dhm116 commented Feb 10, 2012

Apparently it took me 36 minutes to throw this together

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment