2017-12-12 15 views
0

私はファイルを読み込んで、ファイル内の文字列をワード検索ボードに変換して検索する単語検索プログラムに取り組んでいます。私のプログラムはほとんどの場合、しかし、私はどのように私のリストの単語の終わりの場所を見つけるか分からない作業です。ワード検索プログラム(Python)

マイコード:

def print_it_out(lines, words): 
    for direction, tuple in lines.items(): 
     string = ''.join([i[0] for i in tuple]) 
     for word in words: 
      if word in string: 
       coordinates = tuple[string.index(word)][1] 
       #print(word, 'starts at', coordinates[0], 'and column', coordinates[1], direction + ".") 
       print(word, 'starts at (%d, %d)' % (coordinates[0], coordinates[1])) 


def find_words(file_input, words): 
    with open(file_input) as file: 
     for line in file: 
      line = line.replace(' ', '') 
      line = line.strip() 
      if len(line) == 0: 
       for line in file: 
        line = line.replace('\n', '') 
        line = line.lower() 
        words.append(line) 
    words = [x.upper() for x in words] 
    words = sorted(words) 
    print(words) 


def get_search_board(file_input, search_board): 
    with open(file_input) as file: 
     for line in file: 
      if len(line.strip()) == 0: 
       break 
      elif len(line) > 6: 
       # line = line.replace('\n', '') 
       line = line.lower() 
       search_board += line 
    search_board = search_board.rstrip() 
    search_board = search_board.replace(' ', '') 
    length = search_board.index('\n') + 1 
    return search_board, length 


def main(): 
    words = [] 
    search_board = '' 
    file_input = input('Enter the name of the file that contains the word search: ') 
    find_words(file_input, words) 
    search_board, length = get_search_board(file_input, search_board) 

    letters = [(letter, divmod(index, length)) 
       for index, letter in enumerate(search_board)] 
    # Reorder the list to represent each reading direction, 
    # and add them all to a dictionary 
    lines = {} 
    offsets = {'down': 0, 'right down': -1, 'left down': 1} 
    for direction, offset in offsets.items(): 
     lines[direction] = [] 
     for i in range(length): 
      for j in range(i, len(letters), length + offset): 
       lines[direction].append(letters[j]) 
      lines[direction].append('\n') 
    lines['left'] = letters 
    lines['right'] = [i for i in reversed(letters)] 
    lines['up'] = [i for i in reversed(lines['down'])] 
    lines['left up'] = [i for i in reversed(lines['right down'])] 
    lines['right up'] = [i for i in reversed(lines['left down'])] 
    # Make strings from the letters, find the words in them and retrieve 
    # their original locations 

    print_it_out(lines, words) 


main() 

私が使用している特定のファイル:明確にするため

15 15 
J H C J B E H U C Y M J A L Q 
N Q Q H K C B V T E X U F A E 
W O T A X E J K G N B P V M D 
C H C O U I R D P O F X X Z B 
Q M F R C P L P B H X K S L S 
H L I E A R F C Q V O H M D D 
K V F V P E A S Y Q P Z O J L 
H K Z N L T V G P C N G H D L 
R R Z V B S H D S M X Y T L B 
N A A R D A G G Q S I S D N A 
O Y F W O M W E I L P X J R Z 
Y D F C R W O S A U Z Y O T W 
K H M E E A L N C C G X L F B 
L Z F F S K Q I E L A R S S B 
X Z O H P D M J W Y C V D P A 

BOBCAT 
CAKE 
cat 
easy 
HONEY 
MASTERPIECE 
MONEY 
PEASY 

は、私のプログラムは、座標によって与えられた単語とその開始位置のそれぞれを見つけることができしかし、私はまた、各単語の終わり/最後の座標を見つけることができるようにしたい。これに加えて、各単語を対応する座標をアルファベット順に印刷したい。アドバイスがありましたら、感謝しています!ありがとう!

+0

単語全体を見つけた場合、一致する単語の長さは検索した単語の長さですか、いいえ? – tripleee

答えて

0

これはそれを行う必要があります。