1

I am writing a python script, that looks like this so far:

import sys
fname = sys.argv[1]
letters = str.lower(sys.argv[2])
letaray = list(letters)
charcount = int(letters.count("")) - 1
lines = tuple(open(fname, 'r'))
wordlist = map(str.lower,open(fname).read().splitlines())
scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, "x": 8, "z": 10}
# char count
if charcount != 7:
    print "Not 7 characters"
    exit()
def file_len(fname):
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    return i + 1
file_len(fname)

The program is ran by:

python main.py wordlist.txt XXXXXXX

Where X is whatever the letters for the scrabble board for example: AGJBTGE.

These letters are then parsed into an array like so:

['a', 'g', 'j', 'b', 't', 'g', 'e']

The textfile is then thrown line-by-line into the variable wordlist.

How can I create another list with all the possible combos of words that can be made from the letters? for example: age, jab etc.

Note: is there is only 1 G, then only 1 G can be used, if there are 2 G's, only 2 can be used, and so on. I may have not provided everything needed, please let me know.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Skyler Spaeth
  • 193
  • 1
  • 1
  • 11
  • 2
    The code you posted is just the loading of the data. What have you tried so far, towards the actual solving of your problem? – spectras Nov 11 '15 at 02:44
  • I don't know what to try, that's why I'm asking. @spectras – Skyler Spaeth Nov 11 '15 at 02:47
  • 1
    There are many ways to approach the problem, and coming up with one is the point of the exercice I believe (otherwise a specific way would be outlined). Did you find a way to test whether a specific word matches your rules? You should probably try to isolate your problem to that, like write a function `def is_word_good(word, letters):` and make it work separately. Only then introduce it in the whole program. – spectras Nov 11 '15 at 02:58
  • What would be inside of the function? Is there a compare module, or something along those lines? @spectras – Skyler Spaeth Nov 11 '15 at 03:04
  • Nope, you'd have to think of a method to do it. If you are stuck, a good way to do it is to take a piece of paper, write down a word and a set of letters, and figure out if the word would be valid. As you do so, try to examine how you proceed, and say it aloud, like "I look for this letter here", "I cross out this one", etc. If you do it properly, you should end up with an algorithm, written in English. It will then be much, much easier to translate that English into Python than trying to write it directly in python if you're not already fluent in Python. – spectras Nov 11 '15 at 03:18

3 Answers3

2

In order to do this you need a corpora of words and a function to compare whether a given list of characters is contained in another list of characters.

Since Testing if a list contains another list with Python already exists on S.O I won't be answering that, but, I will give you some help with getting and processing some words.

You can find a list of words in many places on the web (and on your machine if you're using Unix) or you can use some of your own (like wordlist.txt which you have) and the following function should work on any newline seperated file.

For this demo I'm using /usr/share/dict/british-english as the file containing the words:

# enter the file name you want to use e.x wordlist.txt
filename = "/usr/share/dict/british-english"

Since these files are usually large, you can use this generator function to yield words which you can then process (don't worry about what generators are, just use them for now :-) ):

def yield_words():
    with open(filename, "r") as f:
        for word in f:
            yield word

So, let say you have a list of characters:

l = list("HelloWorld")

You can then iterate through this generator and for each word compare and evaluate if the word word is contained in l:

valid_words = []
for word in yield_words():
    # define a contains function
    # that returns True if l contains word
    if contains(l, word):
        valid_words.append(word)

This should be a good starting point, and as much as I'm willing to assist, use the links I added to find a good set of (additional) words to use and a function with which you can compare the lists, then you're set to go.

Community
  • 1
  • 1
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • 1
    That's an excellent answer for a homework assignment question. Gets some boilerplate out of the way while providing good hints and relevant links for the key parts. I think it's much better than the here-is-the-solution we often see. +1. – spectras Nov 11 '15 at 03:40
  • Guess who's been upvoting your comments in this question :-). Either way, it's up to the OP to attempt this if he really wants to learn. Let's just hope that if he doesn't, someone else with a similar issue might bump into this. – Dimitris Fasarakis Hilliard Nov 11 '15 at 03:50
  • What should the content of contains be? – Skyler Spaeth Nov 11 '15 at 03:54
  • @SkylerSpaeth, I ***explicitly linked*** another question for this. – Dimitris Fasarakis Hilliard Nov 11 '15 at 03:56
  • Sorry, I didn't see it at first. @DimitrisJim – Skyler Spaeth Nov 11 '15 at 04:35
  • 1
    It's fine, the linked question has the answer for a contains function. Study it and try and integrate it in your program. If you run in any other issues and can't find a similar question on StackOverflow, come back and create another question. :-) – Dimitris Fasarakis Hilliard Nov 11 '15 at 05:30
  • So I looked through the first answer, it doesn't seem to be able to check words, rather two integers? I am missing something, or should I post a new post? @DimitrisJim Thanks! – Skyler Spaeth Nov 12 '15 at 00:49
0
from itertools import permutations

string ='AGJBTGE'

string = string.lower()

for r in range(1,len(string)+1):
    for word in permutations(string,r):
        print(''.join(word))

r is a option input for permutations function which define the length of output.
you could add if statement to check the word.

galaxyan
  • 5,944
  • 2
  • 19
  • 43
  • 1
    That does not solve his problem, which is about validating a set of words against the specified letters. – spectras Nov 11 '15 at 03:02
  • @spectras if the OP don't have standard, we could always grab the valid English words as validation. – galaxyan Nov 11 '15 at 03:06
  • 1
    And generating all permutations? That's factorial complexity. Give a handful of letters and your program will take years to complete. For instance, the string `ABCDEFGHIJKLMNO` will generate a bit more than 1307 billion permutations. – spectras Nov 11 '15 at 03:08
  • @spectras agree. I am waiting for better answer too. – galaxyan Nov 11 '15 at 03:13
  • Doing it the other way is much more efficient. Checking whether a specific word passes is not very hard (you could count the occurences of each letter of the word, and if all letters of the word appear at most as many times as they appear in the input letters, the word passes). – spectras Nov 11 '15 at 03:21
0

You can use combinations from itertools and then using chain to club all of them to a single list

In [1]: from itertools import combinations, chain

In [2]: word = "AGJBTGE"

In [3]: word_combos = [list(combinations(word, r)) for r in range(1, len(word)+1)]

In [4]: word_combos_chained = [''.join(wl) for wl in list(chain(*word_combos))]

In [5]: word_combos_chained[25:35]
Out[5]: ['TG', 'TE', 'GE', 'AGJ', 'AGB', 'AGT', 'AGG', 'AGE', 'AJB', 'AJT']
Gurupad Hegde
  • 2,155
  • 15
  • 30
  • So how would I go about running the entire outcome of word_combos_chained[beginning:end] against my text file, line by line? – Skyler Spaeth Nov 11 '15 at 03:15