Created
February 11, 2012 04:38
-
-
Save malloc47/1796332 to your computer and use it in GitHub Desktop.
embedly 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
# Problem 1 | |
fact = lambda n: reduce(lambda x,y: x*y, range(1, n+1), 1) | |
convert = lambda n: sum(map(int,str(n))) | |
def p1gen(): | |
i = 0 | |
while True: | |
yield (i,convert(fact(i))) | |
i+=1 | |
p = p1gen() | |
result = (0,0) | |
while result[1] != 8001: | |
result = p.next() | |
print("Problem 1: "+str(result[0])) # 787 | |
# or | |
l = [ x for x in range(0,1000) if convert(fact(x))==8001 ] | |
print("Problem 1: "+str(min(l))) # 787 | |
# Problem 2 | |
from HTMLParser import HTMLParser | |
import numpy as np | |
class P2Parser(HTMLParser): | |
def __init__(self): | |
HTMLParser.__init__(self) | |
self.pdepths = [] | |
self.depth = -3 # account for head/body | |
def handle_starttag(self, tag, attrs): | |
# print(str(tag)+" "+str(self.depth)) | |
self.depth += 1 | |
if(tag=="p"): | |
self.pdepths.append(self.depth) | |
def handle_endtag(self, tag): | |
self.depth -= 1 | |
parser = P2Parser() | |
parser.feed(open("2.html", 'r').read()) | |
print("Problem 2: "+str(np.std(parser.pdepths))) # 1.411... | |
# Problem 3 | |
from itertools import * | |
s=2520.0 | |
l = [s/x for x in range(1,901)] | |
half_of_words = sum(l)/2.0 | |
# ugly, but I only had < 1 hr to work on these problems... | |
print("Problem 3: "+str(len(list(takewhile(lambda x: x < half_of_words, | |
np.cumsum(l))))+1)) # 22 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made the assumption that the problem wanted to overshoot the halfway point (i.e. the first element > half_of_words) rather than undershoot (and when I checked the answer against the webapp, this is indeed what was wanted). But, thanks to the lambda I passed to takewhile, it will return all the elements except the last element that puts the number of elements over the halfway point. So I added this final element. If itertools had a "takeuntil" function, I would have used that instead, but as I was coding for speed, I just used what was in the libraries.