Created
December 2, 2016 20:27
-
-
Save tbttfox/3d44fd4c9cda9c18b0a2a40da9a8fea5 to your computer and use it in GitHub Desktop.
Python no-import fuzzy finder
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
def fuzzyfinder(pattern, collection): | |
results = [] | |
for item in collection: | |
key = [] | |
# Start the find at the beginning of the string | |
index = -1 | |
for x in pattern: | |
# Find the next letter in the pattern after the prevous match | |
index = item.find(x, index + 1) | |
if index == -1: | |
# Not Found. Skips the else for this iteration | |
break | |
key.append(index) | |
else: | |
# If all are found | |
results.append((key, item)) | |
return (y for _, y in sorted(results)) | |
collection = ['django_migrations.py', | |
'django_admin_log.py', | |
'main_generator.py', | |
'migrations.py', | |
'api_user.doc', | |
'user_group.doc', | |
'accounts.txt', | |
] | |
print list(fuzzyfinder('migg', collection)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment