Skip to content

Instantly share code, notes, and snippets.

@jtallieu
Created September 9, 2016 19:26
Show Gist options
  • Save jtallieu/fa59968314041558d82e5e40ef64dab9 to your computer and use it in GitHub Desktop.
Save jtallieu/fa59968314041558d82e5e40ef64dab9 to your computer and use it in GitHub Desktop.
Eagle Eye Networks Inc. :: Python screening sample - Duplication
"""
::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)")
@thartley
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment