Skip to content

Instantly share code, notes, and snippets.

@agamdua
Created November 11, 2015 12:23
Show Gist options
  • Save agamdua/00e4f927f9186115853f to your computer and use it in GitHub Desktop.
Save agamdua/00e4f927f9186115853f to your computer and use it in GitHub Desktop.
"""
This finds the duplicates in the matrix (list of lists) and returns
a list of all items "visited" and a list of all "duplicate".
This has been run like:
% python matrix.py
These items have been visited at least once: [[1, 2, 3], [3, 4, 5]]
These are the duplicates: [[1, 2, 3]]
"""
matrix = [
[1, 2, 3], [3, 4, 5], [1, 2, 3]
]
def find_duplicates(matrix):
visited = []
duplicates = []
for vector in matrix:
if vector in visited:
duplicates.append(vector)
else:
visited.append(vector)
return visited, duplicates
if __name__ == '__main__':
visited, duplicates = find_duplicates(matrix)
print "These items have been visited at least once: {}".format(visited)
print "These are the duplicates: {}".format(duplicates)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment