2014-04-22 153 views
2

我正在嘗試編寫一個程序,允許用戶輸入單詞,然後查找隱藏在單詞文本文件中的單詞長度爲4或更大的所有單詞。到目前爲止,我的代碼可以檢測用戶輸入的單詞中沒有混雜的單詞。例如,如果我輸入「房屋」,輸出將顯示「房屋,房屋,鎬,我們,使用和使用」。它還應該承認'軟管,軟管,鞋子,鞋子,色調等'。查找單詞遊戲中的單詞

我知道itertools是最簡單的解決方案,但我想使用僅使用循環,字典和列表的不同方法。

這是到目前爲止我的代碼:

def main(): 
    filename = open('dictionary.txt').readlines() 
    word_list = [] 
    for line in filename: 
     word_list.append(line.strip()) 

    print 'Lets Play Words within a Word!\n' 
    word = raw_input('Enter a word: ') 
    words_left = 0 
    for words in word_list: 
     letters = list(words) 
     if words in word: 
      print words 
      words_left += 1 
     else: 
      False 

我試圖創建看起來應該像這樣的輸出格式:

Lets play Words within a Word! 

Enter a word: exams 

exams --- 6 words are remaining 
> same #user types in guess 
Found! # prints 'Found!' if above word is found in the dictionary.txt file 

exams --- 5 words are remaining 
> exam 
Found! 

exams --- 4 words are remaining 
> mesa 
Found! 

exams --- 3 words are remaining 
> quit() #if they type this command in the game will end 

我也想加入遊戲概要用於跟蹤用戶的分數基於Scrabble的信評分,但是這是後來的事情。

+0

仍在尋找一些幫助 – user3521614

回答

0

似乎是itertools

import itertools 

word = "house" 

arrangements = [] 

# get the combinations 
for i in range(len(word)+1): 
    arrangements += list(itertools.permutations(word,i)) 

# turn the tuples into strings 
for i in range(len(arrangements)): 
    arrangements[i] = ''.join(arrangements[i]) 

print(arrangements) 

看哪一份工作,在安排是字「家」的每一個可能的排列的一個巨大的列表。隨意過濾通過它尋找真正的話。

輸出:

['', 'h', 'o', 'u', 's', 'e', 'ho', 'hu', 'hs', 'he', 'oh', 'ou', 'os', 'oe', 'uh', 'uo', 'us', 'ue', 'sh', 'so', 'su', 'se', 'eh', 'eo', 'eu', 'es', 'hou', 'hos', 'hoe', 'huo', 'hus', 'hue', 'hso', 'hsu', 'hse', 'heo', 'heu', 'hes', 'ohu', 'ohs', 'ohe', 'ouh', 'ous', 'oue', 'osh', 'osu', 'ose', 'oeh', 'oeu', 'oes', 'uho', 'uhs', 'uhe', 'uoh', 'uos', 'uoe', 'ush', 'uso', 'use', 'ueh', 'ueo', 'ues', 'sho', 'shu', 'she', 'soh', 'sou', 'soe', 'suh', 'suo', 'sue', 'seh', 'seo', 'seu', 'eho', 'ehu', 'ehs', 'eoh', 'eou', 'eos', 'euh', 'euo', 'eus', 'esh', 'eso', 'esu', 'hous', 'houe', 'hosu', 'hose', 'hoeu', 'hoes', 'huos', 'huoe', 'huso', 'huse', 'hueo', 'hues', 'hsou', 'hsoe', 'hsuo', 'hsue', 'hseo', 'hseu', 'heou', 'heos', 'heuo', 'heus', 'heso', 'hesu', 'ohus', 'ohue', 'ohsu', 'ohse', 'oheu', 'ohes', 'ouhs', 'ouhe', 'oush', 'ouse', 'oueh', 'oues', 'oshu', 'oshe', 'osuh', 'osue', 'oseh', 'oseu', 'oehu', 'oehs', 'oeuh', 'oeus', 'oesh', 'oesu', 'uhos', 'uhoe', 'uhso', 'uhse', 'uheo', 'uhes', 'uohs', 'uohe', 'uosh', 'uose', 'uoeh', 'uoes', 'usho', 'ushe', 'usoh', 'usoe', 'useh', 'useo', 'ueho', 'uehs', 'ueoh', 'ueos', 'uesh', 'ueso', 'shou', 'shoe', 'shuo', 'shue', 'sheo', 'sheu', 'sohu', 'sohe', 'souh', 'soue', 'soeh', 'soeu', 'suho', 'suhe', 'suoh', 'suoe', 'sueh', 'sueo', 'seho', 'sehu', 'seoh', 'seou', 'seuh', 'seuo', 'ehou', 'ehos', 'ehuo', 'ehus', 'ehso', 'ehsu', 'eohu', 'eohs', 'eouh', 'eous', 'eosh', 'eosu', 'euho', 'euhs', 'euoh', 'euos', 'eush', 'euso', 'esho', 'eshu', 'esoh', 'esou', 'esuh', 'esuo', 'house', 'houes', 'hosue', 'hoseu', 'hoeus', 'hoesu', 'huose', 'huoes', 'husoe', 'huseo', 'hueos', 'hueso', 'hsoue', 'hsoeu', 'hsuoe', 'hsueo', 'hseou', 'hseuo', 'heous', 'heosu', 'heuos', 'heuso', 'hesou', 'hesuo', 'ohuse', 'ohues', 'ohsue', 'ohseu', 'oheus', 'ohesu', 'ouhse', 'ouhes', 'oushe', 'ouseh', 'ouehs', 'ouesh', 'oshue', 'osheu', 'osuhe', 'osueh', 'osehu', 'oseuh', 'oehus', 'oehsu', 'oeuhs', 'oeush', 'oeshu', 'oesuh', 'uhose', 'uhoes', 'uhsoe', 'uhseo', 'uheos', 'uheso', 'uohse', 'uohes', 'uoshe', 'uoseh', 'uoehs', 'uoesh', 'ushoe', 'usheo', 'usohe', 'usoeh', 'useho', 'useoh', 'uehos', 'uehso', 'ueohs', 'ueosh', 'uesho', 'uesoh', 'shoue', 'shoeu', 'shuoe', 'shueo', 'sheou', 'sheuo', 'sohue', 'soheu', 'souhe', 'soueh', 'soehu', 'soeuh', 'suhoe', 'suheo', 'suohe', 'suoeh', 'sueho', 'sueoh', 'sehou', 'sehuo', 'seohu', 'seouh', 'seuho', 'seuoh', 'ehous', 'ehosu', 'ehuos', 'ehuso', 'ehsou', 'ehsuo', 'eohus', 'eohsu', 'eouhs', 'eoush', 'eoshu', 'eosuh', 'euhos', 'euhso', 'euohs', 'euosh', 'eusho', 'eusoh', 'eshou', 'eshuo', 'esohu', 'esouh', 'esuho', 'esuoh'] 
2

的東西,可以真正幫助您的itertools.permutations。這個方便的函數接受一個序列(比如一個字符串)並給出所有指定長度的排列。

這裏是我會建議你使用它:

import itertools 

def main(): 
    with open('dictionary.txt') as file: 
     word_list = set(line.strip() for line in file) # Use a set instead of a list for faster lookups 

    print 'Lets Play Words within a Word!\n' 
    word = raw_input('Enter a word: ') 

    subwords = set() # In case a word can be made in multiple ways 
    for i in range(4, len(word)+1): 
     for permutation in itertools.permutations(word, i): 
      word_to_check = ''.join(permutation) 
      if word_to_check in word_list: 
       subwords.add(word_to_check) 

此檢查單詞的所有可能的排列,看看哪些是詞,並保留所有單詞一組(無重複)內發現的字。然後,當用戶猜你可以只檢查

if user_guess in subwords and user_guess not in already_found_words: 
+0

有沒有辦法只使用列表,字典和列表索引,不導入itertools? – user3521614

+0

Itertools是一個內置模塊(不需要下載或安裝任何東西)。如果你真的不想使用它,我鏈接到的文檔具有相同的Python代碼。請注意,使用itertools可以讓代碼更乾淨,運行速度更快。 –

0

您可以使用permutations()itertools模塊和set刪除重複的字符串。

>>> from itertools import permutations 
>>> set((''.join(p) for i in range(1, len('exams') +1) for p in permutations('exams', i))) 

這給你

set(['eaxs', 'xea', 'xmsea', 'emsax', 'xem', 'seam', 'mxse', 'amsxe', 'msxe', 'esma', 'mxsa', 'xsma', 'sexma', 'samex', 'xsme', 'xes', 'maxse', 'mexas', 'eaxm', 'mexsa', 'easm', 'axems', 'asmex', 'sxaem', 'xme', 'emasx', 'exsa', 'mxeas', 'xma', 'amxse', 'amexs', 'asxm', 'xaems', 'xsem', 'xmesa', 'easx', 'sxame', 'xsea', 'maesx', 'xms', 'eamsx', 'exsm', 'exmsa', 'axse', 'axmes', 'sxme', 'ame', 'mxs', 'sxema', 'axsm', 'amx', 'amesx', 'xemsa', 'measx', 'smea', 'xesma', 'axmse', 'ams', 'mxe', 'esm', 'mxa', 'seamx', 'aes', 'mxas', 'ema', 'meax', 'aex', 'mxea', 'xmae', 'mexs', 'sema', 'mesxa', 'sxeam', 'saemx', 'xsa', 'msax', 'mxae', 'ems', 'xse', 'x', 'aexms', 'sxem', 'aem', 'emx', 'xsm', 'em', 'mxsae', 'emas', 'axms', 'samxe', 'msea', 'sxmae', 'emax', 'esmx', 'ea', 'emxs', 'sex', 'emaxs', 'mesx', 'ex', 'sea', 'xase', 'exsma', 'sem', 'xmea', 'esmxa', 'es', 'emxa', 'smx', 'axes', 'semx', 'smaxe', 'esx', 'xaesm', 'asxme', 'axem', 'esa', 'aexs', 'sme', 'esxma', 'maex', 'sma', 'sexa', 'smeax', 'xemas', 'mea', 'exam', 'sxmea', 'semxa', 'asx', 'sexm', 'aexsm', 'xsmae', 'amex', 'esamx', 'emxsa', 'mes', 'aesxm', 'smax', 'saxm', 'ames', 'exas', 'mex', 'asm', 'xmsae', 'esxam', 'emsx', 'saxe', 'xems', 'eaxsm', 'xsema', 'xmas', 'saexm', 'meaxs', 's', 'exams', 'esmax', 'emsa', 'xema', 'xames', 'aemsx', 'amxs', 'aesmx', 'ase', 'mesax', 'xm', 'aesm', 'xa', 'asemx', 'msaxe', 'xe', 'xasm', 'seax', 'mexa', 'smae', 'amxe', 'xs', 'asexm', 'aesx', 'esax', 'masx', 'msx', 'xmaes', 'msxea', 'amse', 'xas', 'xsame', 'msxae', 'xsae', 'maes', 'emsxa', 'xam', 'esam', 'xmes', 'xsam', 'mse', 'xae', 'msa', 'exasm', 'aemx', 'smxea', 'xame', 'eam', 'axsem', 'mase', 'aems', 'amsx', 'xaes', 'smxe', 'eax', 'xaem', 'msexa', 'xesm', 'asmxe', 'asxe', 'masex', 'xesa', 'eas', 'amxes', 'esaxm', 'mxaes', 'me', 'esxa', 'mseax', 'ma', 'sxae', 'emxas', 'esxm', 'sxam', 'eamxs', 'msxa', 'ms', 'amsex', 'xasme', 'aemxs', 'xesam', 'mx', 'xmase', 'axme', 'sxa', 'msaex', 'xmeas', 'ae', 'sxe', 'sam', 'maxes', 'maexs', 'mxsea', 'xeasm', 'am', 'sxm', 'sae', 'easmx', 'xasem', 'as', 'sax', 'meas', 'msex', 'xsmea', 'ax', 'asxem', 'sxea', 'sexam', 'mxase', 'xseam', 'exsam', 'saex', 'axesm', 'saem', 'maxs', 'masxe', 'asmx', 'same', 'smexa', 'samx', 'saxem', 'asme', 'saxme', 'xsaem', 'exmas', 'msae', 'maxe', 'seaxm', 'axsme', 'mas', 'asex', 'xams', 'eaxms', 'max', 'xeams', 'exms', 'smxae', 'xmsa', 'smaex', 'mae', 'xmse', 'asem', 'aexm', 'smxa', 'mesa', 'sxma', 'exma', 'mxes', 'a', 'axs', 'eams', 'sx', 'e', 'xeas', 'mxesa', 'exa', 'semax', 'eamx', 'exm', 'm', 'easxm', 'xamse', 'exs', 'sm', 'axe', 'smex', 'sa', 'xeam', 'se', 'axm']) 

注意,可能是得到較大的字符串效率非常低!

0

使用itertools,以幫助你在這裏:

from itertools import permutations 
print '\nLets play Words within a Word!\n' 
filename = raw_input('Enter the path to the file: ') 
wordlist = open('/usr/share/dict/words', 'r') 
wordlist = [word for word in wordlist.read().split() if len(word) >= 4] 
word = raw_input('Enter a word: ') 
possible_words = list(set([words for words in [''.join(p) for i in range(1, len(word)+1) for p in permutations(word, i)] if len(words) >= 4 and words in wordlist])) 
guessed = [] 
while len(guessed) < len(possible_words): 
     left = len(possible_words)-len(guessed) 
     print 'The number of words left to guess is %s!' %(str(left)) 
     guess = raw_input('\nEnter your guess: ') 
     if guess in possible_words: 
       print 'Correct!\n' 
       guessed.append(guess) 
     else: 
       if guess == '': 
         for k in guess: 
           print k, 
       else: 
         print 'Incorrect!\n' 

print 'You won!' 

這種形式運行:

bash-3.2$ python everyword.py 

Lets play Words within a Word! 

Enter the path to the file: /usr/share/dict/words 
Enter a word: hello 
The number of words left to guess is 4! 

Enter your guess: incorrect 
Incorrect! 

The number of words left to guess is 4! 

Enter your guess: hole 
Correct! 

The number of words left to guess is 3! 

Enter your guess: hello 
Correct! 

The number of words left to guess is 2! 

Enter your guess: holl 
Correct! 

The number of words left to guess is 1! 

Enter your guess: blah 
Incorrect! 

The number of words left to guess is 1! 

Enter your guess: hell 
Correct! 

You won! 
bash-3.2$