Created
September 9, 2018 14:33
-
-
Save lucasp90/bd11d8d558f89c9a81d29e63e6787b06 to your computer and use it in GitHub Desktop.
Coding challenges
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 get_subdomains(domain): | |
tokens = domain.split('.') | |
subdomains = [] | |
for i in range(len(tokens)): | |
subdomains.append(".".join(tokens[i:])) | |
return subdomains | |
def main(): | |
results = {} | |
visits_data = [ "900,google.com", | |
"60,mail.yahoo.com", | |
"10,mobile.sports.yahoo.com", | |
"40,sports.yahoo.com", | |
"300,yahoo.com", | |
"10,stackoverflow.com", | |
"2,en.wikipedia.org", | |
"1,es.wikipedia.org" ] | |
visits_data = map(lambda v: v.split(','), visits_data) | |
for visits, domain in visits_data: | |
subdomains = get_subdomains(domain) | |
for subdomain in subdomains: | |
results[subdomain] = results.get(subdomain, 0) + int(visits) | |
print(results) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment