Last active
October 17, 2017 05:02
-
-
Save h2rashee/3ad592d40f633c5565225aad7b7b7b33 to your computer and use it in GitHub Desktop.
Given list of entries "hits, domain", determine the total hits of each subdomain
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
// Given a list of strings (int, string) where int is the number of hits and the string is a domain name, | |
// tally a domain and each of its subdomains' total hits. | |
hit_list = ["100,yahoo.com", | |
"20,mail.yahoo.com"] | |
def calculate_domain_hits(): | |
domain_counts = {} | |
for entry in hit_list: | |
# Parse each input entry | |
entry_list = entry.split(',') | |
domain = entry_list[1] | |
hits = int(entry_list[0]) | |
# Let's bootstrap the domain so we can start looking at each part | |
d_list = domain.split('.') | |
for i in range(0, len(d_list)): | |
# Join what we need and use it as our key | |
cur_domain = '.'.join(d_list[i:]) | |
if cur_domain in domain_counts: | |
domain_counts[cur_domain] = domain_counts[cur_domain] + hits | |
else: | |
domain_counts[cur_domain] = hits | |
return domain_counts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment