Repository navigation
Expand file tree
/
Copy pathdeduplicator.py
More file actions
executable file
·140 lines (109 loc) · 3.81 KB
/
Copy pathdeduplicator.py
File metadata and controls
executable file
·140 lines (109 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
"""Implements different deduplication methods and tests their efficiency."""
import timeit
def main():
"""
Demonstrates the effectiveness and efficiency of deduplication methods.
This program uses an arbitrary list full of duplicate entries. The program
then demonstrates the effectiveness of various deduplication methods.
Finally, the program displays data on the efficiency of the deduplication
methods.
"""
someList = ["Humble Bundle",
"Steam",
"Origin",
"GOG.com",
"Humble Bundle",
"Steam",
"Blizzard App",
"Origin",
"Origin",
"Steam",
"Steam",
"GOG.com",
"Steam"]
# Demonstrate that all methods do remove duplicates.
deduplicatedList1 = deduplicator1(someList)
deduplicatedList2 = deduplicator2(someList)
deduplicatedList3 = deduplicator3(someList)
print("Deduplicated list (Method 1):")
print("{}\n".format(deduplicatedList1))
print("Deduplicated list (Method 2):")
print("{}\n".format(deduplicatedList2))
print("Deduplicated list (Method 3):")
print("{}\n".format(deduplicatedList3))
# Demonstrate the respective efficiencies of the methods.
efficiencyTests(someList)
def deduplicator1(someList):
"""
Create a list of the distinct elements from some other list.
Args:
someList: any abitrary list of items.
Returns:
someList with all duplicates removed.
"""
deduplicatedList = []
for item in someList:
if item not in deduplicatedList:
deduplicatedList.append(item)
return deduplicatedList
def deduplicator2(someList):
"""
Create a deduplicated list using Python's set datatype.
Args:
someList: any abitrary list of items.
Returns:
someList with all duplicates removed.
"""
deduplicatedList = list(set(someList))
return deduplicatedList
def deduplicator3(someList):
"""
Create a deduplicated list by manipulating the original list.
This method is most like the algorithm described in the first interview.
Args:
someList: any abitrary list of items.
Returns:
someList sorted with all duplicates removed.
"""
listLength = len(someList)
someList.sort()
counter = 0
while counter < listLength - 1:
if someList[counter] == someList[counter + 1]:
del someList[counter + 1]
# Update list length as you delete and avoid out of bounds errors.
listLength = len(someList)
else:
counter += 1
return someList
def efficiencyTests(someList):
"""
Test the efficiency of various deduplication functions.
Args:
someList: any arbitrary list of items.
"""
print("Testing methods by running them 1000 times.")
# Test deduplicator1 efficiency.
start = timeit.default_timer()
for i in range(0, 9999):
deduplicatedList = deduplicator1(someList)
finish = timeit.default_timer()
runtime = finish - start
print("Time for Method 1 to run 1000 times: {} seconds".format(runtime))
# Test deduplicator2 efficiency.
start = timeit.default_timer()
for i in range(0, 9999):
deduplicatedList = deduplicator2(someList)
finish = timeit.default_timer()
runtime = finish - start
print("Time for Method 2 to run 1000 times: {} seconds".format(runtime))
# Test deduplicator3 efficiency.
start = timeit.default_timer()
for i in range(0, 9999):
deduplicatedList = deduplicator3(someList)
finish = timeit.default_timer()
runtime = finish - start
print("Time for Method 3 to run 1000 times: {} seconds".format(runtime))
if __name__ == "__main__":
main()