如何继续这个python程序
我正在努力熟悉python。我想我会解决骆驼的难题。这是我目前掌握的代码。我现在有两个问题:如何继续这个python程序,python,Python,我正在努力熟悉python。我想我会解决骆驼的难题。这是我目前掌握的代码。我现在有两个问题: fCamel = 'F' bCamel = 'B' gap = 'G' def solution(formation): return len([i for i in formation[formation.index(fCamel) + 1:] if i == bCamel]) == 0 def heuristic(formation): fCamels, score = 0, 0
fCamel = 'F'
bCamel = 'B'
gap = 'G'
def solution(formation):
return len([i for i in formation[formation.index(fCamel) + 1:] if i == bCamel]) == 0
def heuristic(formation):
fCamels, score = 0, 0
for i in formation:
if i == fCamel:
fCamels += 1;
elif i == bCamel:
score += fCamels;
else:
pass
return score
def getneighbors (formation):
igap = formation.index(gap)
res = [[]]
# AB_CD --> A_BCD | ABC_D | B_ACD | ABD_C
def genn(i,j):
temp = list(formation)
temp[i], temp[j] = temp[j], temp[i]
res.append(temp)
if(igap > 0):
genn(igap, igap-1)
if(igap > 1):
genn(igap, igap-2)
if igap < len(formation) - 1:
genn(igap, igap+1)
if igap < len(formation) - 2:
genn(igap, igap+2)
return res
def astar (formation, heuristicf, solutionf, getneighborsf):
openlist = [].append(formation)
closedlist = []
#Example usage (I think)
#astar([fCamel, fCamel, fCamel, gap, bCamel, bCamel, bCamel], heuristic, solution, getneighbors)
fCamel='F'
B名称='B'
差距='G'
def溶液(形成):
返回len([i表示信息中的i[formation.index(fCamel)+1:]如果i==bCamel])==0
def(编队):
fCamels,得分=0,0
对于我的信息:
如果i==fCamel:
fCamels+=1;
elif i==b名称:
分数+=fCamels;
其他:
通过
回击得分
def GetNeights(编队):
igap=地层指数(间隙)
res=[]]
#AB|CD-->A|BCD | ABC|D | B|ACD | ABD|C
def genn(i,j):
临时=列表(形成)
温度[i],温度[j]=温度[j],温度[i]
资源附加(临时)
如果(igap>0):
genn(igap,igap-1)
如果(igap>1):
genn(igap,igap-2)
如果igap
我现在有两个问题
test_set = set(test_list)
if your_var in test_set:
# do something
test_set = set(test_list)
if your_var in test_set:
# do something
class Formation(object):
"""A formation of camels."""
def __init__(self, camels, parent):
self.camels = camels
self.current_distance = 0
self.parent = parent
@property
def total_distance(self):
"""The total distance."""
return self.current_distance + self.heuristic
@property
对象(称为decorator)修改以下函数,使其看起来像类的属性。这就是为什么Python不使用显式访问器方法(例如GetDistance()
和SetDistance
)的原因;您可以根据需要使方法看起来像属性,而不是使所有属性看起来像方法。所以,要得到一个编队的总距离,你只需说出编队的总距离;后面没有()
我不熟悉您试图解决的问题,但我对您的代码有一些评论:
def solution(formation):
return len([i for i in formation[formation.index(fCamel) + 1:] if i == bCamel]) == 0
这实际上更好地实现为标准循环。将其作为Formation
类的另一个属性写入:
@property
def solution(self):
for camel in self.camels[self.camels.index(fCamel) + 1:]:
if camel == bCamel:
return False
return True
如果您只是在计算项目,那么创建列表(len()
不会在生成器上运行)没有意义。这也可以成为一个属性
关于启发式
,您不需要其他:通过
,您不需要使用分号,请每行执行一项作业:
@property
def heuristic(self):
fCamels = 0
score = 0
for camel in self.camels:
if camel == fCamel:
fCamels += 1
elif camel == bCamel:
score += fCamels
return score
转到getneights
。在genn
中,list(…)
不会复制列表,它只是获取给定的内容并从中生成一个列表。如果它的参数已经是一个列表,那么它什么也不做并返回输入。如果要制作副本,需要执行“从副本导入副本”中的,然后使用复制
功能。(在copy
模块中还有一个deep\u copy
功能):
def copy_swapping_camels(self,i,j):
newCamels=复制(self.camels)
新骆驼[i],新骆驼[j]=新骆驼[j],新骆驼[i]
返回编队(新骆驼,自我)
def get_邻居(自身):
igap=自身骆驼指数(gap)
结果=[]]
如果igap>0:
result.append(self.copy\u swapping\u camels(igap,igap-1))
如果igap>1:
result.append(self.copy\u swapping\u camels(igap,igap-2))
如果igap
在这里,在一行上做两个作业是可以的,因为这是一个交换(作业是相互关联的)
我需要有3个以上的数据场和一个编队。g=当前距离,f=总值(启发值+g),p=父项。如何制作包含所有这些内容的结构
您应该使用类来表示编队:
class Formation(object):
"""A formation of camels."""
def __init__(self, camels, parent):
self.camels = camels
self.current_distance = 0
self.parent = parent
@property
def total_distance(self):
"""The total distance."""
return self.current_distance + self.heuristic
@property
对象(称为decorator)修改以下函数,使其看起来像类的属性。这就是为什么Python不使用显式访问器方法(例如GetDistance()
和SetDistance
)的原因;您可以根据需要使方法看起来像属性,而不是使所有属性看起来像方法。所以,要得到一个编队的总距离,你只需说出编队的总距离;后面没有()
我不熟悉您试图解决的问题,但我对您的代码有一些评论:
def solution(formation):
return len([i for i in formation[formation.index(fCamel) + 1:] if i == bCamel]) == 0
这实际上更好地实现为标准循环。将其作为Formation
类的另一个属性写入:
@property
def solution(self):
for camel in self.camels[self.camels.index(fCamel) + 1:]:
if camel == bCamel:
return False
return True
如果您只是在计算项目,那么创建列表(len()
不会在生成器上运行)没有意义。这也可以成为一个属性
关于启发式
,您不需要否则:pass
,您不需要使用分号,请执行o