Created
February 11, 2012 01:23
-
-
Save anonymous/1794924 to your computer and use it in GitHub Desktop.
Embed.ly 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
import math | |
import re | |
############################## | |
#### Challenge 1 #### | |
############################## | |
def R(n): | |
factorial, summed = str(math.factorial(n)), 0 | |
for i in factorial: summed += int(i) | |
return summed | |
def find_min(n): | |
trial, i = R(0), 0 | |
while trial != n: | |
i += 1 | |
trial = R(i) | |
return i | |
print find_min(8001) | |
############################## | |
#### Challenge 2 #### | |
############################## | |
def get_tag_depths(filename, tag): | |
f = open(filename, 'r') | |
data = f.read() | |
f.close() | |
matches = re.findall("(<[^>]*>)", data) | |
depth, matched_depths = 0, [] | |
for match in matches: | |
if match.startswith("</"): depth -= 1 | |
else: | |
if len(match) == (len(tag) + 2) and tag in match: | |
matched_depths.append(depth) | |
if not match.endswith("/>"): | |
depth += 1 | |
return matched_depths | |
def std_dev(numbers): | |
m = float(sum(numbers)) / len(numbers) | |
d = [(x - m)**2.0 for x in numbers] | |
return (sum(d) / len(d))**(0.5) | |
print std_dev(get_tag_depths('embedly.html', 'p')) | |
############################## | |
#### Challenge 3 #### | |
############################## | |
def find_words(start, uniques, point): | |
total = 0 | |
for i in xrange(1, uniques + 1): | |
total += max(1, (float(start) / i)) | |
sofar = 0 | |
for i in xrange(1, uniques + 1): | |
sofar += max(1, (float(start) / i)) | |
if sofar >= (total / point): return i | |
return None | |
print find_words(2520, 900, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment