i am trying to solve a python algorithm problem but having trouble i will explain the question below ( I couldn't copy it here)
Q : in a N*N array, each square consisting of random numbers, (every number is different)
You are at a random number, you need to move to a neighboring number that is smaller than you. You can only move in 4 directions(up, down, left, right). When you move, if there are more than one place you can move to, you should only move to the smallest number possible.
example: 19 57 74 73 94
26 27 32 98 61
40 88 49 38 25
21 66 53 95 46
80 23 58 39 89
if you start at 49, you move like this: 49 -> 32 -> 27 -> 26 -> 19, so you move 4 times.
Keep moving until you cannot move anymore. Return the maximal times you can move in each test case.
my code:
import sys
sys.stdin = open('input.txt', 'r')
T = int(input())
for test_case in range(1, T + 1):
N = int(input())
arr = [list(map(int, input().split())) for _ in range(N)]
# directions
di = [-1, 1, 0, 0]
dj = [0, 0, -1, 1]
# max move
# max(count_list)
count_list = []
count = 0 # count every time you move
for i in range(N):
for j in range(N):
for d in range(4):
ni = i + di[d]
nj = j + dj[d]
small_list_arr = [] # smallest of arr[ni][nj]
if 0 <= ni < N and 0 <= nj < N: # inside N*N
if arr[ni][nj] < arr[i][j]: # if there is smaller neighboring num
small_list_arr.append(arr[ni][nj])
# if there are multiple smaller neighboring nums
if arr[ni][nj] == min(small_list_arr):
count += 1 # move count
ni, i = i, ni # new i, j
nj, j = j, nj
count_list.append(count)
print(f'#{test_case} {max(count_list)}')
my problem is, I think I am okay with finding the new (i, j) (smallest neighboring number's location) but when I use the debugger I find that <for d in range(4)> does not start over from 0 again, for example if I found new (i, j) when d = 2, the debugger shows that when you go back up to <for d in range(4)> the d starts from 2. how do i solve this? maybe there is a problem with how i allocate ni, nj to i, j variables?
I tried break but didn't work, I am new to python so need help sorry for my english
i am trying to solve a python algorithm problem but having trouble i will explain the question below ( I couldn't copy it here)
Q : in a N*N array, each square consisting of random numbers, (every number is different)
You are at a random number, you need to move to a neighboring number that is smaller than you. You can only move in 4 directions(up, down, left, right). When you move, if there are more than one place you can move to, you should only move to the smallest number possible.
example: 19 57 74 73 94
26 27 32 98 61
40 88 49 38 25
21 66 53 95 46
80 23 58 39 89
if you start at 49, you move like this: 49 -> 32 -> 27 -> 26 -> 19, so you move 4 times.
Keep moving until you cannot move anymore. Return the maximal times you can move in each test case.
my code:
import sys
sys.stdin = open('input.txt', 'r')
T = int(input())
for test_case in range(1, T + 1):
N = int(input())
arr = [list(map(int, input().split())) for _ in range(N)]
# directions
di = [-1, 1, 0, 0]
dj = [0, 0, -1, 1]
# max move
# max(count_list)
count_list = []
count = 0 # count every time you move
for i in range(N):
for j in range(N):
for d in range(4):
ni = i + di[d]
nj = j + dj[d]
small_list_arr = [] # smallest of arr[ni][nj]
if 0 <= ni < N and 0 <= nj < N: # inside N*N
if arr[ni][nj] < arr[i][j]: # if there is smaller neighboring num
small_list_arr.append(arr[ni][nj])
# if there are multiple smaller neighboring nums
if arr[ni][nj] == min(small_list_arr):
count += 1 # move count
ni, i = i, ni # new i, j
nj, j = j, nj
count_list.append(count)
print(f'#{test_case} {max(count_list)}')
my problem is, I think I am okay with finding the new (i, j) (smallest neighboring number's location) but when I use the debugger I find that <for d in range(4)> does not start over from 0 again, for example if I found new (i, j) when d = 2, the debugger shows that when you go back up to <for d in range(4)> the d starts from 2. how do i solve this? maybe there is a problem with how i allocate ni, nj to i, j variables?
I tried break but didn't work, I am new to python so need help sorry for my english
Share Improve this question edited Feb 18 at 3:44 ccccc asked Feb 18 at 3:43 cccccccccc 11 bronze badge New contributor ccccc is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.2 Answers
Reset to default 3Unlike C, Python's for loop effectively assigns the iterated value every time. So your assignment on i
, j
in your code doesn’t affect the loop.
You may instead use a while loop to achieve the same effect as for loop in C.
I have generated a solution, based on a different approach,
arr = [
[ 19, 57, 74, 73, 94 ],
[ 26, 27, 32, 98, 61 ],
[ 40, 88, 49, 38, 25 ],
[ 21, 66, 53, 95, 46 ],
[ 80, 23, 58, 39, 89 ]
]
initial = [ 2, 3 ]
steps = 0
# returns the value contained in the matrix
def getValue( point ):
return arr[ point[ 0 ] ][ point[ 1 ]]
# if the row and column values are within the appropriate range and the value
# returned by **getValue()** is less than **value** it returns “true”,
# otherwise it returns “false”.
def evaluate( row, col, value ):
if( row > -1 and row < len( arr ) and col > -1 and col < len( arr[ 0 ] ) ):
if arr[ row ][ col ] < value:
return True
return False
# calls "evaluate" four times, passing as parameters, the supposed neighbor
# cells of the current cell and "value", if any call returns true, it updates
# the value of "value" and "newPoint", finally returns "newPoint"
def getMinNeighbor( point, value ):
newPoint = [ -1, -1 ]
if evaluate( point[ 0 ] - 1, point[ 1 ], value ):
newPoint = [ point[ 0 ] - 1, point[ 1 ] ]
value = getValue( newPoint )
print( f" ^ {value}" )
if evaluate( point[ 0 ] , point[ 1 ] - 1, value ):
newPoint = [ point[ 0 ], point[ 1 ] - 1 ]
value = getValue( newPoint )
print( f" < {value}" )
if evaluate( point[ 0 ] + 1, point[ 1 ], value ):
newPoint = [ point[ 0 ] + 1, point[ 1 ] ]
value = getValue( newPoint )
print( f" v {value}" )
if evaluate( point[ 0 ], point[ 1 ] + 1, value ):
newPoint = [ point[ 0 ], point[ 1 ] + 1 ]
value = getValue( newPoint )
print( f" > {value}" )
return newPoint
# this method calls “getMinNeighbor” assigning to “newPoint” the value returned
# by it, if it is a valid value, it increments “steps” and resorts to recursion,
# to reach the result, if it is not, it returns “steps”.
def explore( point ):
global steps
value = getValue( point )
newPoint = getMinNeighbor( point, value )
if newPoint != [ -1, -1 ]:
steps += 1
return explore( newPoint )
else:
return steps
print( explore( initial ))