Skip to content

Instantly share code, notes, and snippets.

@mrmekon
Created February 10, 2012 21:23
Show Gist options
  • Save mrmekon/1792990 to your computer and use it in GitHub Desktop.
Save mrmekon/1792990 to your computer and use it in GitHub Desktop.
Embedly application puzzle
import urllib2
from xml.etree.ElementTree import XMLParser
import numpy
class DepthCounter:
depthdict = {}
def __init__(self):
self.classdepth = 0
self.depth = 0
self.depthdict = {}
def start(self, tag, attrib):
self.classdepth += 1
def end(self, tag):
self.classdepth -= 1
def data(self, data):
if self.classdepth not in self.depthdict:
self.depthdict[self.classdepth] = 0
self.depthdict[self.classdepth] += 1
def close(self):
return self.depthdict
page = urllib2.urlopen("http://apply.embed.ly/static/data/2.html")
contents = page.read()
cntr = DepthCounter()
parser = XMLParser(target=cntr)
parser.feed(contents)
cntdict = parser.close()
depthlisttree = [[x[0]]*x[1] for x in cntdict.iteritems() if x[1] > 0]
depthlist = [x for sublist in depthlisttree for x in sublist]
depthlistzero = [x-1 for x in depthlist]
print numpy.std(depthlistzero)
from math import factorial
def sum_factorial_digits(n):
return sum([int(x) for x in str(factorial(n))])
print min([x for x in xrange(1000) if sum_factorial_digits(x) == 8001])
import sys
def wordfreq(n):
startn = n
div = 1
result = []
while (n > 0):
div += 1
result.append(n)
if (n < 1.0):
break
n = float(startn)/div
return result
def count_words_up_to_limit(list, limit):
cum = 0
idx = 0
while cum < limit:
cum += list[idx]
idx += 1
return idx
unique_words = [x for x in xrange(1000) if len(wordfreq(x)) == 900]
if not unique_words:
sys.exit(1)
unique_words = unique_words[0]
count_list = wordfreq(unique_words)
totalwords = sum(count_list)
halfwords = totalwords / 2
print count_words_up_to_limit(count_list, halfwords)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment