为什么 print(foo) 显示的内容与 print(functionreturningthesame()) 不同
查看输出,在函数中打印数组显示正确答案,但打印函数的返回值没有。我可能对递归迭代感到困惑......
def AreNotOpposite(a,b):
if a == "NORTH" and b == "SOUTH":
return False
if a == "SOUTH" and b == "NORTH":
return False
if a == "WEST" and b == "EAST":
return False
if a == "EAST" and b == "WEST":
return False
return True
def canBeBetter(arr):
for i in range(len(arr)-1):
if not AreNotOpposite(arr[i],arr[i+1]):
return True
return False
def dirReduc(arr):
re = []
avoid = -1
for i in range(len(arr)):
if avoid == i:
continue
if i+1 == len(arr):
re.append(arr[i])
elif AreNotOpposite(arr[i],arr[i+1]):
re.append(arr[i])
else: #do not append neither the nextone
avoid = i+1
if canBeBetter(re): #can reduce more?
dirReduc(re)
else:
print(re)
return re
print (dirReduc(['NORTH', 'WEST', 'EAST','SOUTH', 'NORTH','SOUTH','EAST','NORTH']))
输出:
['EAST', 'NORTH']
None
慕姐4208626
米脂
相关分类