You are given information about users of your website. The information includes username, a phone number and/or an email. Write a program that takes in a list of tuples where each tuple represents information for a particular user and returns a list of lists where each sublist contains the indices of tuples containing information about the same person. For example:
Input:
[("MLGuy42", "[email protected]", "123-4567"), ("CS229DungeonMaster", "123-4567", "[email protected]"), ("Doomguy", "[email protected]", "[email protected]"), ("andrew26", "[email protected]", "[email protected]")]
Output:
[[0, 1, 3], [2]]
Since "MLGuy42", "CS229DungeonMaster" and "andrew26" are all the same person.
Each sublist in the output should be sorted and the outer list should be sorted by the first element in the sublist.
Below is the code snippet that I did for this problem. It seems to work fine, but I'm wondering if there is a better/optimized solution.
def find_duplicates(user_info):
results = list()
seen = dict()
for i, user in enumerate(user_info):
first_seen = True
key_info = None
for info in user:
if info in seen:
first_seen = False
key_info = info
break
if first_seen:
results.append([i])
pos = len(results) - 1
else:
index = seen[key_info]
results[index].append(i)
pos = index
for info in user:
seen[info] = pos
return results