Skip to content

Instantly share code, notes, and snippets.

@h2rashee
Last active October 17, 2017 05:02
Show Gist options
  • Save h2rashee/3ad592d40f633c5565225aad7b7b7b33 to your computer and use it in GitHub Desktop.
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
// 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