Function 保持变量';递归函数中的s值,python 3.3
我设法用另一种方法做到了。 但我有一个问题,我以前有过这个代码Function 保持变量';递归函数中的s值,python 3.3,function,variables,recursion,Function,Variables,Recursion,我设法用另一种方法做到了。 但我有一个问题,我以前有过这个代码 def jumphunt(start, mylist, count = 0): if count < len(mylist): place = mylist[start] print(place) if place == 0: return True elif start >= len(mylist) or start <
def jumphunt(start, mylist, count = 0):
if count < len(mylist):
place = mylist[start]
print(place)
if place == 0:
return True
elif start >= len(mylist) or start < 0:
return False
move_left = (start - place)
move_right = (start + place)
return jumphunt(move_right, mylist, count+1) or jumphunt(move_left, mylist, count+1)
else:
return False
def jumphunt(开始,mylist,计数=0):
如果计数=len(mylist)或start<0:
返回错误
向左移动=(起始位置)
向右移动=(开始+放置)
返回jumphunt(向右移动,我的列表,计数+1)或jumphunt(向左移动,我的列表,计数+1)
其他:
返回错误
但出于某种原因,它并没有尝试两种方法
进入列表中的最后一项
例如:[1,2,2,3,4,5,3,2,1,7,0]和start=mylist[0]
它应该像这样跳(从1-2-4-1-左到2-左到5-右到0)
但它一直试图向右移动,然后索引超出范围等等
我想如果你使用return of或this或that,它会尝试这两种方法,直到它变成真的,为什么它在这里不起作用
谢谢 包括要保留作为方法默认参数的值,如下所示:
def my_func(int, list, i=0):
a = (i + int)
if int == 0:
return True
elif a > len(list):
i -= int
else:
i += int
int = list[i]
my_func(int, list, i)
def branching_search(list, start):
marks = [0]*len(list)
pos = start
while list[pos]!=0:
marks[pos]++
if marks[pos] % 2 == 0 and pos + list[pos] < len(list):
pos += list[pos]
elif marks[pos] % 2 == 1 and pos - list[pos] >= 0:
pos -= list[pos]
else:
return False
if all(item == 0 or item > 1 for item in list)
return False
return True
请记住,按照您描述的跳转模式到达列表末尾可能并不总是可能的,即使可能,此方法也可能选择错误的分支
更好的算法如下所示:
def my_func(int, list, i=0):
a = (i + int)
if int == 0:
return True
elif a > len(list):
i -= int
else:
i += int
int = list[i]
my_func(int, list, i)
def branching_search(list, start):
marks = [0]*len(list)
pos = start
while list[pos]!=0:
marks[pos]++
if marks[pos] % 2 == 0 and pos + list[pos] < len(list):
pos += list[pos]
elif marks[pos] % 2 == 1 and pos - list[pos] >= 0:
pos -= list[pos]
else:
return False
if all(item == 0 or item > 1 for item in list)
return False
return True
前两个元素只会得到一个标记(因此循环检查条件将不起作用),但它仍然会在两个5之间循环
以下是一种始终有效的方法:
def flood_search(list):
marks = [[]]*len(list)
marks[0] = [0]
still_moving = True
while still_moving:
still_moving = False
for pos in range(0,len(list)):
if marks[pos]:
if pos + list[pos] < len(list) and not marks[pos + list[pos]]:
marks[pos + list[pos]] = marks[pos] + [list[pos]];
pos += list[pos]
still_moving = True
if pos - list[pos] >= 0 and not marks[pos - list[pos]]:
marks[pos - list[pos]] = marks[pos] + [-list[pos]];
pos -= list[pos]
still_moving = True
return marks[-1]
def flood_搜索(列表):
标记=[[]]*len(列表)
标记[0]=[0]
仍在移动=正确
仍在移动时:
仍在移动=错误
对于范围(0,长度(列表))内的位置:
如果标记[pos]:
如果pos+列表[pos]=0且未标记[pos-列表[pos]]:
标记[pos-列表[pos]]=标记[pos]+[-列表[pos]];
pos-=列表[pos]
仍在移动=正确
返回标记[-1]
这是通过同时获取每个可能的分支来实现的
您还可以使用该方法获取到达终点的实际路线。它仍然可以用作条件,因为如果未找到路径(falsy值),它将返回空列表;如果找到路径,它将返回包含路径的列表(truthy值)
但是,您始终可以使用
list[-1]
获取最后一项。什么是int
?你不应该使用这样的变量名,因为它在许多语言中都是一个类型关键字,会让人感到困惑。我只使用int,比如这里它可以是任何int。@dav_sap,这是因为你没有用I
作为参数调用my_func
。是的,我让它工作了,但它没有给我确切的需要,但这可能是我的代码中的一些东西,我试图理解这行的位置,如果它指向一个它已经访问过的项目,这意味着没有出路并返回False。@AJ是这一行吗?如果全部(列表中的项目==0 | | item>2)