-
-
Save malloc47/1796332 to your computer and use it in GitHub Desktop.
# 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 |
Very possibly--there was some ambiguity as to whether the question wanted to overshoot or undershoot (by 1) the halfway point so I just picked the one that went over (and didn't thoroughly examine the consequences--since it only makes a difference of ~3 words, it's not surprising it still gives the same answer).
I wonder what is that +1 you are adding at the end?
print("Problem 3: "+str(len(list(takewhile(lambda x: x < half_of_words, np.cumsum(l))))+1)) # 22
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.
Problem 3
I think your implementation is not correct.
length of l must be 900, as the problem states "Given that the text has 900 unique words".