2016-10-08 9 views
0

私はPythonのデバッグの効果的な方法とそれに伴う理解と最適化の欠如に少し苦労しています。Python 3の順列スクラブル

私は実験としてPythonでScrabbleゲームを書いていますが、私はそれについてすべて間違っていると考えています。つまり、空白のタイル(以下の疑問符)の処理方法を理解できていません。次のように私は、コードを不手際ました:

import itertools 
import string 

    rack = 'rstlena??' 
    blank_count = rack.count('?') 
    letters = rack.split(',') 
    word_set = set() 

    if blank_count > 0: 
    rack = rack.replace('?', '') 
    if blank_count == 1: 
     for l in string.ascii_lowercase: 
     letters.append(rack + l) 
    elif blank_count == 2: 
     for l in string.ascii_lowercase: 
     letters.append(rack + l + l) 

    for l_combo in rack: 
     for i in range(3, 9): 
     for permutation in itertools.permutations(l_combo, i): 
      word_to_check = ''.join(permutation) 
      if word_to_check in word_list: # word_list is some dictionary 
      word_set.add(word_to_check) 

...

を1つのブランクがあるときそれは動作しますが、2とそれだけで同じ文字を追加し、(明らかに)望ましくない結果を生成します。

私は醜いコードのために事前にお詫び申し上げます。

+0

ありがとうございました。それが私が質問している理由です。その理由で質問を投票してくれてありがとう。 – Rebuggered

+0

さて、もう1つの 'string.ascii_lowercase:for l 'をもう1つの変数名に変更するだけで、2つの異なる文字を得ることができます。私はその質問をd​​ownvoteしなかった、btw。 – Efferalgan

+0

質問を改善して、さらなる批判/投票を避けることができれば、できる限りのことはすべてやります。 – Rebuggered

答えて

0

これはうまくいくはずです。私はトリッキーな部分をコメントしようとしましたが、あなたが理解していないコードがいくつかあるかどうか尋ねるのをためらってください。

import itertools 
import string 
import numpy as np 

#takes a list of words as input ['door', 'chair', 'wall'], and returns the words alphabetically sorted [['door', 'door'], ['achir', 'chair'], ['allw', 'wall']] 
def sortWordList(word_list): 
    return np.array([["".join(sorted(word)), word] for word in word_list]) 

#recursive function to get all unordered n-letter subsets of `letters`, without repetition. NLetterSubsets(2, "fly") == ["fl", "fy", "ly"]; NLetterSubsets(2, "foo") == ["fo", "fo", "oo"] 
def NLetterSubsets(n, letters): 
    if n == len(letters): 
     return [letters] 
    if n > len(letters) or len(letters) == 0: 
     return [] 
    return [letters[0] + x for x in NLetterSubsets(n-1, letters[1:])] + NLetterSubsets(n, letters[1:]) 

#loads word_list from a newline-separated file 
def loadWordList(filename): 
    with open(filename, 'r') as f: 
     return [line.rstrip('\n') for line in f] 

word_list = loadWordList('words.txt') 
word_list = sortWordList(word_list) 
print(word_list) 

rack = 'trackable' 
blank_count = rack.count('?') 
if blank_count: 
    rack = rack.replace('?','') 
word_set = set() 

#possible letters is the list of all possible values taken by the blank(s) 
possibleLetters = [''] 
if blank_count >= 1: 
    possibleLetters += [l for l in string.ascii_lowercase] 
if blank_count == 2: 
    possibleLetters += [l+m for l in string.ascii_lowercase for m in string.ascii_lowercase] 

for blanks in possibleLetters: 
    for i in range(3-len(blanks), 9-len(blanks)): 
     #gets a subset of letters from rack, to which we will add the blank values to form a word of length in range(3,9) 
     for setOfLetter in NLetterSubsets(i-blank_count, rack): 
      sortedWordToSearchFor = ''.join(sorted(setOfLetter + blanks)) 
      #if the sorted list of letters we have is also present in the word_set, then take the associated word 
      for index in np.where(word_list[:,0] == sortedWordToSearchFor)[0]: 
       word_set.add(word_list[index][1]) 

print(word_set) 
+0

多くの義務づけられた、良い先生。 – Rebuggered

+0

ありがとうございました。 – Rebuggered

+0

私は容赦しますが、まだ動作しないコードで少し失われています。 [word_list]が多少混乱しているか、または外れている可能性があります。私が私の無知を示​​しているのであれば、お詫び申し上げます。 – Rebuggered