Skip to content

Instantly share code, notes, and snippets.

@harshvb7
Created May 13, 2018 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harshvb7/95ebb2e69996f1c234d3b1bc3134f801 to your computer and use it in GitHub Desktop.
Save harshvb7/95ebb2e69996f1c234d3b1bc3134f801 to your computer and use it in GitHub Desktop.
class Solution(object):
def __init__(self):
self.arr = []
def add_product(self, name, locations):
self.arr.append((name, locations))
def get_locations(self):
data = {}
for item in self.arr:
for location in item[1]:
if location not in data:
data[location] = set()
data[location].add(item[0])
return [(x[0], list(x[1])) for x in data.items()]
s = Solution()
s.add_product('ABC', ["Paris", "France", "Europe"])
s.add_product('DEF', ["Dubai", "Abu Dhabi", "UAE"])
s.add_product('XYZ', ["Germany", "Italy", "Paris", "Europe"])
print s.get_locations()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment