Skip to content

Instantly share code, notes, and snippets.

@Romern
Created July 30, 2020 18:50
Show Gist options
  • Save Romern/b568a131569e0c8c5611984944118880 to your computer and use it in GitHub Desktop.
Save Romern/b568a131569e0c8c5611984944118880 to your computer and use it in GitHub Desktop.
import string
input_scrambled = open("input.txt","r").read().replace("\n"," ").split(" ")
wordlist = open("wordlist-german.txt","r").read().split("\n")
wordlist = [w.lower() for w in wordlist]
wordlist_dict = {}
for w in wordlist:
for a in w:
if a not in wordlist_dict:
wordlist_dict[a] = set()
wordlist_dict[a].add(w)
output = ""
for w in input_scrambled:
# kommas etc ans ende:
end = "".join([a for a in w if not a.isalpha()])
w = "".join([a.lower() for a in w if a.isalpha()])
# unscramble
if len(w) == 0:
output += w + end + " "
continue
possible = {}
for a in set(w):
possible[a] = {word for word in wordlist_dict[a] if len(word)==len(w)}
pos = possible[w[0]]
for a in set(w):
pos = {word for word in pos if word in possible[a]}
pos = [word for word in pos if set(word) == set(w)]
if len(pos)>0:
w = pos[0]
print(pos)
else:
print(w)
output += w + end + " "
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment