Created
February 11, 2012 01:12
-
-
Save ckwang8128/1794876 to your computer and use it in GitHub Desktop.
A set of 3 python scripts that I used to solve embed.ly's programming challenge.
This file contains hidden or 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
| def digit_sum(num): | |
| return sum(map(int, str(num))) | |
| n = cur_num = 1 | |
| while (digit_sum(cur_num*n) != 8001): | |
| cur_num = cur_num*n | |
| n += 1 | |
| print n | |
This file contains hidden or 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
| from BeautifulSoup import BeautifulSoup, SoupStrainer | |
| import math, requests, re | |
| from collections import deque | |
| r = requests.get('http://apply.embed.ly/static/data/2.html') | |
| page_text = BeautifulSoup(r.text).article | |
| #load up our queue initially | |
| tag_queue = deque(page_text.findAll(recursive=False)) | |
| current_ps = len(page_text.findAll('p', recursive=False)) | |
| level = 1 | |
| values = [] | |
| #Perform a BFS and track the level of p tags as we find them. | |
| while tag_queue and current_ps: | |
| next_ps = 0 | |
| while current_ps != 0: | |
| elem = tag_queue.popleft() | |
| next_ps += len(elem.findAll('p', recursive=False)) | |
| for next_elem in elem.findAll(recursive=False): | |
| tag_queue.append(next_elem) | |
| if elem.name == 'p': | |
| current_ps -= 1 | |
| values.append(level) | |
| level += 1 | |
| current_ps = next_ps | |
| mean = float(sum(values)) / len(values) | |
| adjusted_vals = map(lambda num: (num-mean)**2, values) | |
| print "%.1f" % math.sqrt(float(sum(adjusted_vals)/len(adjusted_vals))) |
This file contains hidden or 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
| zipfs_vals = [float(2520)/i for i in xrange(1,901)] | |
| words_needed = sum(zipfs_vals)/2 | |
| uniques = enumerate(zipfs_vals) | |
| while words_needed > 0: | |
| unique,count = uniques.next() | |
| words_needed = words_needed - count | |
| #we need to add 1 since enumerate starts from 0 and we start unique words from 1 | |
| print unique+1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment