Created
September 9, 2016 19:26
-
-
Save jtallieu/fa59968314041558d82e5e40ef64dab9 to your computer and use it in GitHub Desktop.
Eagle Eye Networks Inc. :: Python screening sample - Duplication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
::Eagle Eye Networks Inc.::: | |
::Python screening:: | |
The 'has_dups' function checks an array for | |
duplicate strings. It's pretty inefficient. | |
Improve the 'has_dups' function and explain | |
how you improved it. | |
""" | |
import string | |
import random | |
import cProfile | |
def make_id(N): | |
"""Makes a random N length string of capital letters and numbers""" | |
return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N)) | |
def gen_ids(count): | |
return [make_id(8) for _ in xrange(0, count)] | |
def has_dups(ids): | |
"""Check for duplicates""" | |
for i, id in enumerate(ids): | |
# check only the tail for the existence of id | |
if id in ids[i+1:]: | |
return True | |
return False | |
if __name__ == "__main__": | |
ids = gen_ids(10000) | |
cProfile.run("print has_dups(ids)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍