Skip to content

Instantly share code, notes, and snippets.

@Xevion
Created June 14, 2019 04:26
Show Gist options
  • Save Xevion/05c15ffd422e6eae2b9ba0af0dae0257 to your computer and use it in GitHub Desktop.
Save Xevion/05c15ffd422e6eae2b9ba0af0dae0257 to your computer and use it in GitHub Desktop.
import os, sys, string, pprint
pp = pprint.PrettyPrinter()
# Display large wordlist in nice format
def display(split=10):
data = '\n'.join([' '.join(data[i * split:min((i * split) + split, len(data))]) for i in range(int(len(data) / split)+1)])
# OWL 3 Wordlist
"""
import re
path = os.path.join(sys.path[0], 'OWL3.txt')
with open(path, 'r') as file:
data = re.split('\s+', file.read())
print(data)
"""
# OTCWL2014 Wordlist
path = os.path.join(sys.path[0], 'twl3.txt')
with open(path, 'r') as file:
data = file.read().split('\n') # Data does not need stripping, works fine AS IS
# Determines hamming distance with length difference, can determine indexes
def hammingDistance(source, destination, indexes=False):
hamming = [] if indexes else 0
if len(source) != len(destination):
if len(source) > len(destination):
source, destination = list(destination), list(source)
if not indexes:
hamming += abs(len(source) - len(destination))
else:
source, destination = list(source), list(destination)
# If dealing with indexes
if indexes:
for index, char in enumerate(source):
if destination[index] != char:
hamming.append(index)
else:
for index, char in enumerate(source):
if destination[index] != char:
hamming += 1
return hamming
# Identifies the number of character changes that are positive from source -> new relating to destination
# Does not account for length changes!
def poschange(source, new, destination):
changes = hammingDistance(source, new, indexes=True)
score = 0
for change in changes:
if new[change] == destination[change]:
score += 1
else:
score -= 1
return len(changes), score
def identifier(wordlist, source, destination, lengthChange=False, maxChange=1):
if not lengthChange:
wordlist = list(filter(lambda x : len(x) == len(source), wordlist))
possible = [(index, poschange(source, word, destination)) for index, word in enumerate(wordlist)]
changeFilter = (lambda n : n > 0) if maxChange == 0 else (lambda n : n == maxChange)
possible = [wordlist[i] for (i, (tot, pos)) in possible if pos >= 0]
# possible = [identifier(wordlist, word, destination, lengthChange=lengthChange, maxChange=maxChange) for word in possible]
return possible
x = identifier(data, 'four', 'five', maxChange=1)
pp.pprint(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment