0

I have a 2d list with random values [[#$%$],[$#$%],[#@$$],[#$%@]] This is displayed to user as:

# $ % $
$ # $ %
# @ $ $
# $ % @

The user enters a row number (0-3) and a column number (0-3). How would I check how many consecutive matching characters there are from that point. Matches can be vertical or horizontal from the selected point. must have 2 of the same character in a row to be considered a match.

For example if the user enters row 2 column 2 it would "pick" the bracketed value (it doesn't actually show this part):

# $ % $
$ # $ %
# @($)$
# $ % @

and then look for matching symbols horizontally or vertically from it. In this case it should make score =2 because there is 1 dollar sign above and 1 dollar sign to the right of the user point. After matching the 3 symbols, those symbols should be changed from '$' to 'x' Please help. This is what I have so far

 #checking matches right
    count=0
    for i in range(col, len(board)):
        if(board[row][i] == board[row][col]):
            count+=1
#checking matches left
    for i in range(col, -1, -1):
        if(board[row][i] == board[row][col]):
            count+=1
#checking matches up
    for i in range(row, -1, -1):
        if board[i][col] == board[row][col]:
            count+=1
#checking matches down
    for i in range(row, len(board)):
        if(board[i][col] == board[row][col]):
            count+= 1

    return count
4

2 回答 2

0

代码:

import numpy as np
     
lenght = np.array([[3,5,4,4,9],
               [5,1,3,6,2],
               [3,1,3,3,2],
               [3,1,3,3,2],
               [3,1,3,3,2],
               [3,1,2,5,1]])
print(lenght)
in_row = 2
in_col = 2
in_repl = 99
target_value = lenght[in_row, in_col]
print(target_value)
lenght[np.nonzero(lenght[:, in_col] == target_value), in_col] = 99
lenght[in_row, np.nonzero(lenght[in_row, :] == target_value)] = 99
print(lenght)

输出:

[[3 5 4 4 9]
 [5 1 3 6 2]
 [3 1 3 3 2]
 [3 1 3 3 2]
 [3 1 3 3 2]
 [3 1 2 5 1]]
3
[[ 3  5  4  4  9]
 [ 5  1 99  6  2]
 [99  1 99 99  2]
 [ 3  1 99  3  2]
 [ 3  1 99  3  2]
 [ 3  1  2  5  1]]
于 2020-11-10T23:01:38.567 回答
0

如果你想水平或垂直地获取每个符号,你可以这样做。

a = ["#$%$", "$#$%", "#@$$", "#$%@"]

print(a[2][2] + "\n")

# Horizontally
for i in range(4):
    print(a[2][i])

print()

# Vertically
for i in range(4):
    print(a[i][2])

除非您可以向我们展示您的代码,以便证明您已经尝试解决它,因此我们可以更具体地为您提供帮助,否则我将让您计算匹配的数量并对其进行修改。

于 2020-11-10T15:25:47.900 回答