在对角线矩阵中查找单词字母

时间:2019-02-12 19:18:42

标签: python numpy-ndarray

我有一个5 * 6的矩阵,所有值都是英文字母。我必须在矩阵中从左到右,从右到左,上下,上下和对角线找到特定的单词。这实际上是一个难题。

我可以找到左右,左右,上下左右的单词。当要对角找到单词时,事情会变得更加混乱。我提供了从左到右和从右到左搜索的示例代码。

#the word I am looking for
word_list = ["CRAM", "ROTLQ", "TDML", "COOI"]

# create the matrix array or puzzle
letter_array = np.matrix([['C','R','A','M','B','A' ],
 ['R','O','T','L','Q','C' ],
 ['E','O','O','A','U','A'],
 ['I','T','E','I','A','L' ],
 ['I','A','L','M','D','T']])

#itenarate through word list
for word in word_list:

    #flatten the array, convert it to list, 
    #finally join everythig to make a long string
    left_right = ''.join(letter_array.flatten().tolist()[0])

    # flip the string to search from right to left
    right_left = left_right[::-1]


    # if the word is there, it gives the index otherwise it gives -1
    if left_right.find(word)>-1:
        row, col = divmod(left_right.find(word), 6)
        print ("The word is in left to right combination, row and column = ", row, col)

    # look from right to left
    elif right_left.find(word)>-1:
        row, col = divmod(right_left.find(word), 6)
        print ("The word is in right to left combination, row and column = ", row, col)

    else:
        print ("The word is in up down or diagonally located")

结果

The word is in left to right combination, row and column =  0 0
The word is in left to right combination, row and column =  1 0
The word is in right to left combination, row and column =  0 0
The word is in up down or diagonally located

但是,通过转置矩阵,也可以完成上下搜索。但是,我不确定如何对角搜索。有没有办法对角搜索?或者,是否有其他简单的解决方案可以解决整个问题?

2 个答案:

答案 0 :(得分:2)

您已经知道如何搜索字符串中的单词(但请阅读我的评论),在此我提供了一种有关提取对角线的方法。

def diagelem(ll, a, b):
    try:
        if a < 0 or b < 0:
            raise IndexError
        return ll[a, b]
    except IndexError:
        return None

我需要定义此函数以提取矩阵的单个对角线元素。为了避免矩阵周围的圆度,如果索引小于0,我添加了一个raise IndexError语句。我不认为您想要这样做,但是如果我错了,则删除该函数内的if语句应该圆度。

dd = []
for j in range(-letter_array.shape[0]+1, letter_array.shape[0]):
    dd.append([diagelem(letter_array, i, i+j) for i in range(letter_array.shape[1])])

for j in range(0, 2*letter_array.shape[0]):
    dd.append([diagelem(letter_array, i, -i+j) for i in range(letter_array.shape[1])])

diagonals = []
for diag in dd:
    diagword = ''.join([letter for letter in diag if letter is not None])
    if len(diagword) > 0:
        diagonals.append(diagword)

print(diagonals)

前两个循环从对角线构建单词。第一个循环从左上到右下对角线构建单词,第二个循环从左下到右上角构建单词。 dd是一个列表列表,其中有多个None。第三个循环将内部列表连接在一起以构建单词。最后的diagonals是从对角线构建的单词列表。您可以使用发布的相同逻辑搜索word_list中的单词是否在diagonal中的任何单词之内。

答案 1 :(得分:1)

如果您的矩阵总是那么小,我建议您完全不使用矩阵,而是通过间接列表处理字母。

# setup indirection lists for each axis
# -------------------------------------
# (only need to do this once)
#
# 00 01 02 03 04 05   <-- list positions corresponding
# 06 07 08 09 10 11       to letter coordinates
# 12 13 14 15 16 17
# 18 19 20 21 22 23
# 24 25 26 27 28 29

horizontal    = [ [0,1,2,3,4,5],[6,7,8,9,10,11],[12,13,14,15,16,17],[18,19,20,21,22,23],[24,25,26,27,28,29] ]
vertical      = [ [0,6,12,18,24], [1,7,13,19,25], [2,8,14,20,26], [3,9,15,21,27], [4,10,16,22,28], [5,11,17,23,29] ]
diagonal      = [ [6,1], [12,7,2], [18,13,8,3], [24,19,14,9,4], [25,20,15,10,5], [26,21,16,11], [27,22,17], [28,23],
                  [4,11], [3,10,17], [2,9,16,23], [1,8,15,22,29], [0,7,14,21,28], [6,13,20,27], [12,29,26], [18,25] ]

horizontal = horizontal + [ list(reversed(positions)) for positions in horizontal ]
vertical   = vertical   + [ list(reversed(positions)) for positions in vertical ]
diagonal   = diagonal   + [ list(reversed(positions)) for positions in diagonal ]

# Generate the letter matrix as a simple list:

letter_array  = ['C','R','A','M','B','A',
                 'R','O','T','L','Q','C',
                 'E','O','O','A','U','A',
                 'I','T','E','I','A','L',
                 'I','A','L','M','D','T' ]
word_list     = ["CRAM", "ROTLQ", "TDML", "COOI"]

# transpose letter matrix into list of strings for each axis segment

horizontalStrings = [ "".join(map(lambda i:letter_array[i],positions)) for positions in horizontal]
verticalStrings   = [ "".join(map(lambda i:letter_array[i],positions)) for positions in vertical]
diagonalStrings   = [ "".join(map(lambda i:letter_array[i],positions)) for positions in diagonal]

# check for words ...

for word in word_list:
    if   any(filter(lambda string:word in string, horizontalStrings)):
        print(word, " found horizontally")
    elif any(filter(lambda string:word in string, verticalStrings)): 
        print(word, " found vertically")
    elif any(filter(lambda string:word in string, diagonalStrings)): 
        print(word, " found diagonally")
    else:
        print(word, " not found")

[EDIT]要针对任何网格大小通用化该方法,您可以像这样初始化间接列表:

rowCount = 5
colCount = 6
goRight     = [ [ row*colCount+col        for col in range(colCount) ] for row in range(rowCount) ]
goLeft      = [ list(reversed(positions)) for positions in goRight ]
goDown      = [ [ row*colCount+col        for row in range(rowCount) ] for col in range(colCount) ]
goUp        = [ list(reversed(positions)) for positions in goDown ]
goDownRight = [ [ row*colCount+row+col    for row in range(min(rowCount,colCount-col))] for col in range(colCount-1) ] \
            + [ [ (row+col)*colCount+col  for col in range(min(rowCount-row,colCount))] for row in range(1,rowCount-1) ]
goUpLeft    = [ list(reversed(positions)) for positions in goDownRight ]
goDownLeft  = [ [ row*colCount-row+col    for row in range(min(rowCount,col+1))] for col in range(1,colCount) ] \
            + [ [ (row+1+col)*colCount-1-col for col in range(min(rowCount-row,colCount))] for row in range(1,rowCount-1) ]
goUpRight   = [ list(reversed(positions)) for positions in goDownLeft ]

segments    = [ ("horizontally going right",    segment) for segment in goRight ] \
            + [ ("horizontally going left",     segment) for segment in goLeft  ] \
            + [ ("vertically going down",       segment) for segment in goDown  ] \
            + [ ("vertically going up",         segment) for segment in goUp    ] \
            + [ ("diagonally going down-right", segment) for segment in goDownRight ] \
            + [ ("diagonally going up-left",    segment) for segment in goUpLeft    ] \
            + [ ("diagonally going down-left",  segment) for segment in goDownLeft  ] \
            + [ ("diagonally going up-right",   segment) for segment in goUpRight   ]

使用线段间接列表,所有方向均以通用方式进行管理,使您的搜索逻辑可以将重点放在单词上,而不是坐标计算上:

# Generate the letter matrix as a simple list:

letter_array  = ['C','R','A','M','B','A',
                 'R','O','T','L','Q','C',
                 'E','O','O','A','U','A',
                 'I','T','E','I','A','L',
                 'I','A','L','M','D','T' ]
word_list     = ["CRAM", "ROTLQ", "TDML", "COOI", "ROOT", "BLOT", "ALM", "ACA"]

# transpose letter matrix into list of strings for each axis segment

segmentStrings = [ (direction,positions,"".join(map(lambda i:letter_array[i],positions))) for direction,positions in segments ]

# check for words ...

for word in word_list:
    for direction,positions,segmentString in segmentStrings:
        startPos = segmentString.find(word) # see note below
        if startPos < 0: continue
        wordPositions = positions[startPos:][:len(word)]
        gridPositions = [ (position // colCount, position % colCount) for position in wordPositions ]
        print(word,"found\t starting at",wordPositions[0],direction,gridPositions)
        break # don't break here if you want to find all matches

使用(position // colCount,position%colCount)可以获取2D网格坐标。这将产生以下结果:

CRAM found   starting at 0 horizontally going right [(0, 0), (0, 1), (0, 2), (0, 3)]
ROTLQ found  starting at 6 horizontally going right [(1, 0), (1, 1), (1, 2), (1, 3), (1, 4)]
TDML found   starting at 29 horizontally going left [(4, 5), (4, 4), (4, 3), (4, 2)]
COOI found   starting at 0 diagonally going down-right [(0, 0), (1, 1), (2, 2), (3, 3)]
ROOT found   starting at 1 vertically going down [(0, 1), (1, 1), (2, 1), (3, 1)]
BLOT found   starting at 4 diagonally going down-left [(0, 4), (1, 3), (2, 2), (3, 1)]
ALM found    starting at 25 horizontally going right [(4, 1), (4, 2), (4, 3)]
ACA found    starting at 5 vertically going down [(0, 5), (1, 5), (2, 5)]

注意:如果要查找每个单词的所有匹配项(例如,解决隐藏的单词难题),则需要在segmentStrings循环内添加其他逻辑以查找每个段中单词的所有实例(相对于到此示例将产生的第一个)。您可能还需要回文的特殊情况(例如ACA),该情况会出现两次(在每个相反的方向一次)