我有一个数组10x10,我需要解决两点之间对角线上的所有点,并检查列表中是否有。我这样做但我不知道它为什么不工作:
让你进入上下文:
a = [i,j] b = [i,j],i和j =范围(11)
例如,下面的代码适用于a = [5,4] b = [8,7]。
...
elif (b[1] - a[1]) == (b[0] - a[0]):
#to southeast, code for the other 3 cases are almost the same
if b[0] > a[0] and b[1] > a[1]:
n = a[0]
m = a[1]
while (n != b[0]) and (m != b[1]):
n +=1
m +=1
#don't think this part below is relevant
if board[n][m] in somelist:
mov_inv += 1
else:
mov_inv += 0
这是在函数内部,如果mov_inv>返回False。如果mov_inv = 0,则为1,如果不是那样,则为True。希望你明白我的意思。感谢
答案 0 :(得分:0)
def diag(pointa, pointb):
""" pointa and pointb are tuples
returns a generator that yields values along the diagonal
from pointa to pointb"""
if abs(pointa[0] - pointb[0]) != abs(pointa[1] - pointb[1]):
#Sanity check. Diagonal are equal along x and y
raise ValueError("Points {} and {} are not diagonal".format(pointa, pointb))
x_dir = 1 if pointa[0] < pointb[0] else -1
y_dir = 1 if pointa[1] < pointb[1] else -1
while pointa != pointb:
pointa = (pointa[0] + x_dir, pointa[1] + y_dir)
yield pointa
以上不会产生pointb
,因为您已经知道了
答案 1 :(得分:0)
board=xrange(10*10)
diagonal1=[board[C:(10-C)*10:10+1] for C in xrange(10)]
diagonal2=[board[C:(C*10)+1:10-1] for C in xrange(10)]
print 'diagonal ASC ',diagonal1
print 'diagonal DESC ',diagonal2
然后你只需检查a和b是否在同一对角线
assume a,b= [5,4] , [8,7]
def Square(m,n):
return 10*(m-1)+n;
m,n=a
A=Square(m,n)
m,n=b
B=Square(m,n)
**print ('ASCENDENT',[diagonal1[x] for x in xrange(10) if A in diagonal[x] and B in diagonal[x]])**