word search puzzle python

Solutions on MaxInterview for word search puzzle python by the best coders in the world

showing results for - "word search puzzle python"
Antonia
21 Jan 2020
1An improved (shorter, more efficient) version of the above:
2 
3#!/usr/bin/python
4# -*- coding: utf-8 -*-
5 
6puzzle='''F Y Y H N R D
7R L J C I N U
8A A W A A H R
9N T K L P N E
10C I L F S A P
11E O G O T P N
12H P O L A N D
13'''
14clues = ['ITALY', 'HOLLAND', 'POLAND', 'SPAIN',
15            'FRANCE', 'JAPAN', 'TOGO', 'PERU']
16   
17puzzle = puzzle.replace(' ','')            
18length = puzzle.index('\n')+1
19#Make a list of tuples containing each letter and its row and column
20letters = [(letter, divmod(index, length))
21            for  index, letter in enumerate (puzzle)]
22#Reorder the list to represent each reading direction,
23#and add them all to a dictionary
24lines = {}
25offsets = {'down':0, 'right down':-1, 'left down':1}
26for direction, offset in offsets.items():
27    lines[direction] = []
28    for i in range(length):
29        for j in range(i, len(letters), length + offset):
30            lines[direction].append(letters[j])
31        lines[direction].append('\n')
32lines['left']  = letters
33lines['right'] = [i for i in reversed(letters)]
34lines['up'] = [i for i in reversed(lines['down'])]
35lines['left up'] = [i for i in reversed(lines['right down'])]
36lines['right up'] = [i for i in reversed(lines['left down'])]
37#Make strings from the letters, find the words in them and retrieve
38#their original locations
39for direction, tup in lines.items():
40    string = ''.join([i[0] for i in tup])
41    for word in clues:
42        if word in string:
43            location = tup[string.index(word)][1]
44            print word, 'row', location[0]+1, 'column', location[1]+1, direction
45