【发布时间】:2016-12-25 23:53:23
【问题描述】:
我正在尝试完成一个 python 项目,该项目基本上接受输入并遍历有效的拼字游戏单词列表,并确定在给定输入的情况下可以生成哪些单词。
第一部分有点简单,但真正重要的部分是给我带来问题。
这是我目前所拥有的:
import argparse
import sys
"""
Step 1: Get input from the user
"""
parser = argparse.ArgumentParser()
parser.add_argument("rack", type=str, help = "letters on the rack (no spaces)")
args = parser.parse_args()
rack = args.rack
rack = rack.upper()
rack = sorted(rack)
"""
Step 2: Open the sowpods.txt file, read the contents and turn it into a list
"""
def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
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}
file = "sowpods.txt"
length = file_len(file)
file = open("sowpods.txt", 'r')
file_list = list(file)
for i in range(length):
value = file_list[i]
value = value.rstrip('\n')
file_list[i] = value
"""
Step 3: Find valid words
"""
#for x in range(len(file_list)):
for x in range(82980,83000):
tmp = rack
test = file_list[x]
pos = []
if len(test) > len(tmp):
break
else:
for y in range(len(test)):
letter = test[y]
if letter in tmp[y:(len(tmp))]:
pos.append(letter)
print(pos)
我确定这很混乱,因为我有一段时间没有编程了,但我只是想弄清楚程序检查有效性的部分。现在,循环经过一个范围,我知道可以从机架上制作单词,但我被卡住了。我查看了this 的一些帮助帖子,但老实说,我不太确定发生了什么。
我在这里可能有点过头了,但我仍然想弄清楚这一点。
【问题讨论】:
-
trie 数据结构通常对于以紧凑的方式表示字典很有用。
标签: python python-3.x