remove duplicates

Go Back home
 1 import functools
 2 import itertools
 3 from typing import List
 4 
 5 from test_framework import generic_test
 6 from test_framework.test_utils import enable_executor_hook
 7 
 8 
 9 class Name:
10     def __init__(self, first_name, last_name):
11         self.first_name, self.last_name = first_name, last_name
12 
13     def __eq__(self, other):
14         return self.first_name == other.first_name
15 
16     def __lt__(self, other):
17         return (self.first_name < other.first_name
18                 if self.first_name != other.first_name else
19                 self.last_name < other.last_name)
20 
21     def __repr__(self):
22         return '%s %s' % (self.first_name, self.last_name)
23 
24 
25 def eliminate_duplicate(A):
26 
27     A.sort()  # Makes identical elements become neighbors.
28     write_idx = 1
29     for i in range(1, len(A)):
30         if A[write_idx - 1] != A[i]:
31             A[write_idx] = A[i]
32             write_idx += 1
33     del A[write_idx:]
34 
35 
36 def eliminate_duplicate_pythonic(A):
37     A.sort()
38     write_idx = 0
39     for cand, _ in itertools.groupby(A):
40         A[write_idx] = cand
41         write_idx += 1
42     del A[write_idx:]
43 
44 
45 @enable_executor_hook
46 def eliminate_duplicate_wrapper(executor, names):
47     names = [Name(*x) for x in names]
48 
49     executor.run(functools.partial(eliminate_duplicate, names))
50 
51     return names
52 
53 
54 def comp(expected, result):
55     return all([
56         e == r.first_name for (e, r) in zip(sorted(expected), sorted(result))
57     ])
58 
59 
60 if __name__ == '__main__':
61     exit(
62         generic_test.generic_test_main('remove_duplicates.py',
63                                        'remove_duplicates.tsv',
64                                        eliminate_duplicate_wrapper, comp))