简体   繁体   中英

How can I iterate through the wordsearch in python?

I am trying to make a wordsearch that return the starting row, starting column, ending row and ending column of the word that has been found in the wordsearch where the word is extracted from my list of strings where I call it 'words' here. I'm trying to find the horizontal match first, this is what I've tried but I'm stuck.

  def horizontal(labels, words):
            puzzle_word = []
            rows = labels.shape[0] # 15
            cols = labels.shape[1] # 15
        
            length = len(words)
            word = 0 # initialize word where word will be extracted from words 
    
        # replacing all the whitespaces:
            for line in labels:
                line = line.replace(' ', '')
                line = line.strip()
                if len(line) = 0:
                    for line in labels:
                        line = line.replace('\n', '')
                        line = line.lower()
                        words.append(line)
                        words = [x.upper() for x in words]
                        words = sorted(words)
    
        # iterate through the entire words list & extracting the words 1 by 1
            while word < length:
                for row in range(rows):
                    for column in range(columns):
                        character = labels[row][col] 

        # if the starting character matches with a word in the 2-D grid/array puzzle (called labels)
        # and width of all the columns of the grid - the character column to make sure the word will fit and not go over the column size
                        if word[0] == character and len(word) <= (cols-col):

        # how can I implement then look for the next character of the word and see if it matches the next character in the grid on the next column since we are finding horizontally now.

Like maybe:

  for index in word:
          while index < len(word):
           index += 1
                    ......................

Example of given file I'm using, where the grid is supposed to be in a form of a 2-D numpy grid array:

    J H C A T E H U C Y M J A L Q # cat is here
    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 E L E P H A N T I S D N A # elephant is here
    O Y F D O G W E I L P X J R Z # dog is here
    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
   
words: ['cat', 'dog', 'elephant']

Split the string. The common delimiter between words in a string is space. The split returns an array. Each element in the array is a word. Use for loop to iterate over the words present in the array.

A simple solution that might help you get started.

import numpy as np

labels = """
J H C A T 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 E L E P H A N T I S D N A
O Y F D O G 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
""".strip()

lines = np.array([line.split(' ') for line in labels.split('\n')])
words = ['cat', 'dog', 'elephant']

# horizontal matches
matches = []
for row, line in enumerate(lines):

    line = ''.join(line).lower()
    for word in words:
        if  (col := line.find(word)) >= 0:
           matches.append([word, (row, row), (col, col + len(word) - 1)])
    
print(matches) # word, (start row, end row), (start col, end col)

The idea is to view each line as a string and then do substring matching. For vertical matches, you can just transpose the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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