我在试着熟悉蟒蛇。我想我会解决那个骆驼难题。这是我到目前为止掌握的密码。我现在有几个问题:
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)我现在有几个问题。
,
发布于 2010-11-27 14:41:22
--我需要多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 (称为装饰器)修改以下函数,使其看起来像类的一个属性。这就是为什么Python不使用显式访问器方法的原因(比如GetDistance()和SetDistance);与其使所有属性看起来像方法,不如根据需要使方法看起来像属性。所以,要得到一个编队的总距离,你只需在它之后说theFormation.total_distance;没有()。
我不太熟悉您试图解决的问题,但我对您的代码有几点评论:
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()不会对生成器进行操作)是没有意义的。这也可以成为一项财产。
关于heuristic,您不需要else: pass,也不需要ned分号,请每行执行一次赋值:
@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转到getneighbors。在genn中,list(...)不复制列表,它只获取给定的任何内容并从中列出列表。如果它的参数已经是一个列表,那么它将不执行任何操作并返回输入。如果要复制,则需要执行from copy import copy,然后使用copy函数。(在deep_copy模块中也有一个copy函数):
def copy_swapping_camels(self, i, j):
newCamels = copy(self.camels)
newCamels[i], newCamels[j] = newCamels[j], newCamels[i]
return Formation(newCamels, self)
def get_neighbors(self):
igap = self.camels.index(gap)
result = [[]]
if igap > 0:
result.append(self.copy_swapping_camels(igap, igap - 1))
if igap > 1:
result.append(self.copy_swapping_camels(igap, igap - 2))
if igap < len(self.camels) - 1:
result.append(self.copy_swapping_camels(igap, igap + 1))
if igap < len(self.camels) - 2:
result.append(self.copy_swapping_camels(igap, igap + 2))
return result在这里,在一行上执行两个任务是可以的,因为这是一个交换(分配是相互关联的)。
发布于 2010-11-27 13:35:52
test_set = set(test_list)如果your_var在test_set:#中做某事
但是,如果要测试序列是否在列表中有效,则需要实现一些算法,如字符串搜索算法。
https://stackoverflow.com/questions/4291843
复制相似问题