Ctrl K

Domain Visit Rollup

Medium
A domain is made of parts separated by dots, like shop.example.net. Its parent domains come from dropping parts on the left: example.net, and then net. A one-part domain has no parents. A visit to a domain also counts as a visit to each of its parents. You are given a log log where each entry looks like "<count> <domain>": a visit count, a space and the domain. For example, "300 shop.example.net" means 300 visits to shop.example.net. Return a summary in the same "<count> <domain>" format: one line for every domain that appears in the log, either directly or as a parent, with its total number of visits. The lines may come in any order.
Example 1
log=['40 shop.example.net'] → ['40 shop.example.net', '40 example.net', '40 net']

40 visits to shop.example.net are also 40 visits to example.net and 40 to net.

Example 2
log=['5 api.cloud.io', '7 docs.cloud.io', '2 news.ru'] → ['5 api.cloud.io', '12 cloud.io', '12 io', '7 docs.cloud.io', '2 news.ru', '2 ru']

cloud.io and io collect 5 + 7 = 12 visits, news.ru and ru get 2 each, and api.cloud.io and docs.cloud.io keep their own 5 and 7.

Constraints
  • 1 ≤ len(log) ≤ 100
  • 1 ≤ len(log[i]) ≤ 100
  • Each entry is an integer from 1 to 10⁴, a space and a domain
  • A domain is one or more non-empty parts of lowercase English letters and digits separated by dots
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMost Searched Words MediumRanking Finalists by Jury Ballots MediumTwo Pieces Glued Into a Code MediumPalindrome From Two-Letter Cards MediumMost Talkative Sender Medium
loading Python…
Input
log=['40 shop.example.net']
Expected
['40 shop.example.net', '40 example.net', '40 net']

Run checks the first 3 tests in your browser. Submit checks all 69 tests on the server, hidden ones included.