Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look for Words within a Word #38

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
45 changes: 45 additions & 0 deletions look_w.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 23 13:48:31 2018

@author: karja
"""

import itertools, os

# locate your folder that contain words.txt
os.chdir("C:\\Users\\user")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this appears to be Microsoft Windows specific ... 💭


# Open words.txt
wor = open("words.txt","r")
wor = [x[:-1] for x in wor]

# Looking words that in the words list
# by number of letters and alphabets
def look_w(word,num):

# Getting words list according to number of letters
mx = [wor[x] for x in range(len(wor)) if len(wor[x]) == num]

# Create list of words that using itertools from the letters
# and listing all words that relate to the word's letters.
a = []
b = {word[c]:word.count(word[c]) for c in range(len(word))}
for ele in itertools.product(word,repeat = num):
d = {ele[c]:ele.count(ele[c]) for c in range(len(ele))}
z=[]
for i in d:
try:
v = len(d)
if d[i] == b[i]:
z.append('ok')
except:
pass
finally:
if v == len(z):
a.append("".join(ele))

# Matching the created list words with the words in words.txt
# and gather the the valid words
a = [a[i] for i in range(len(a)) if a[i] in wor or a[i].capitalize() in wor]
return a