简体   繁体   English

Python 3排列拼字游戏

[英]Python 3 permutations Scrabble

I'm struggling a bit with an effective way of debugging Python, and the consequent lack of understanding and optimization. 我正在努力尝试一种有效的调试Python的方法,因此缺乏理解和优化。

I'm in the process of writing a Scrabble game in Python as an experiment, and I suspect I'm going about it all wrong. 我正在用Python编写Scrabble游戏作为实验,但我怀疑自己做错了。 Namely, I'm failing to understand how to handle the blank tiles (represented by question marks below). 即,我无法理解如何处理空白图块(由下面的问号表示)。 I've bungled the code as follows: 我将代码如下:

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)

... ...

It works when there's one blank, but with two it just adds the same letter and produces undesired results (obviously). 当有一个空格时,它可以工作,但是有两个空格时,它只添加相同的字母并产生不希望的结果(显然)。

I apologize in advance for the ugly code I've subjected you to cringe at. 对于我让您畏缩的丑陋代码,我事先表示歉意。

This should work, and be acceptably fast. 这应该起作用,并且速度可以接受。 I tried to comment the tricky parts, do not hesitate to ask if there are some bits of the code you do not understand. 我试图评论棘手的部分,不要犹豫,询问是否有一些您不理解的代码。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM