Skip to content

Instantly share code, notes, and snippets.

@daharon
Created February 11, 2012 22:49
Show Gist options
  • Save daharon/1804849 to your computer and use it in GitHub Desktop.
Save daharon/1804849 to your computer and use it in GitHub Desktop.
Embedly challenge. Man, my math is rusty...
#!/usr/bin/env python
import math
for n in xrange(0, 100000000):
f = math.factorial(n)
print n
digits = map(lambda s: int(s), str(f))
if sum(digits) == 8001:
print str(n) + ' is the answer!'
break
#!/usr/bin/env python
num_unique = 900
total_words = 10 ** 5
print 'Total Words: ' + str(total_words)
for most_freq in xrange(num_unique, int(total_words / 2)):
freq_set = []
for word in xrange(1, num_unique):
if len(freq_set) == 0:
freq = most_freq
else:
freq = int(freq_set[0][1] / word)
freq_set.append( (word, freq) )
if total_words - 1000 < sum( [f[1] for f in freq_set] ) < total_words + 1000:
s = 0
for word in freq_set:
s += word[1]
if s > total_words / 2:
print 'Most Freq: ' + str(most_freq)
print ' Word count to half: ' + str(word[0])
break
#!/usr/bin/env python
import math
import numpy
from HTMLParser import HTMLParser
class EmbedlyTestParser(HTMLParser):
def init(self):
self._go = False
self.p_depths = []
def handle_starttag(self, tag, attrs):
if tag == 'article':
self._go = True
elif tag == 'p' and self._go:
_, depth = self.getpos()
self.p_depths.append(depth)
def handle_endtag(self, tag):
if tag == 'article':
self._go = False
if __name__ == '__main__':
parser = EmbedlyTestParser()
parser.init()
with open('./2.html', 'r') as html:
parser.feed(html.read())
depths = map(lambda n: int(n / 2), parser.p_depths)
print str(depths)
avg = float(sum(depths)) / float(len(depths))
print 'Avg: ' + str(avg)
step_one = map(lambda n: (float(n) - avg) ** 2, depths)
print step_one
std_dev = math.sqrt(float(sum(step_one)) / float(len(step_one)))
print 'Standard deviation: ' + str(std_dev)
print 'Numpy Std Dev: ' + str(numpy.std(depths))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment