Python 用不同三角形寻找最大路径
我需要找到三角形的最大路径。我之前发布了这篇文章,我正在实现它,这样每次都可以用不同的值进行多次尝试。我好像有个错误。。需要一些帮助。。。我正在使用Python 3.2.2 代码:Python 用不同三角形寻找最大路径,python,Python,我需要找到三角形的最大路径。我之前发布了这篇文章,我正在实现它,这样每次都可以用不同的值进行多次尝试。我好像有个错误。。需要一些帮助。。。我正在使用Python 3.2.2 代码: numOfTries = raw_input("Please enter the number of tries") Tries = int(numOfTries) for count in range(Tries): numstr= raw_input("Please enter the height:")
numOfTries = raw_input("Please enter the number of tries")
Tries = int(numOfTries)
for count in range(Tries):
numstr= raw_input("Please enter the height:")
rows = int(numstr)
triangle(rows)
routes(triangle)
max(routes(triangle),key=sum)
def triangle(rows):
for rownum in range (rows):
PrintingList = list()
print ("#%d row") % rownum
for iteration in range (rownum):
newValue = raw_input("Please enter the %d number:") %iteration
PrintingList.append(int(newValue))
print()
def routes(rows,current_row=0,start=0):
for i,num in enumerate(rows[current_row]): #gets the index and number of each number in the row
if abs(i-start) > 1: # Checks if it is within 1 number radius, if not it skips this one. Use if not (0 <= (i-start) < 2) to check in pyramid
continue
if current_row == len(rows) - 1: # We are iterating through the last row so simply yield the number as it has no children
yield [num]
else:
for child in routes(rows,current_row+1,i): #This is not the last row so get all children of this number and yield them
yield [num] + child
numotries=raw\u输入(“请输入尝试次数”)
Tries=int(numOfTries)
对于范围内的计数(尝试):
numstr=原始输入(“请输入高度:”)
行=int(numstr)
三角形(行)
路线(三角形)
最大(路线(三角形),关键点=总和)
def三角形(行):
对于范围内的rownum(行):
PrintingList=list()
打印(“#%d行”)%rownum
对于范围内的迭代(rownum):
newValue=原始输入(“请输入%d编号:)%
PrintingList.append(int(newValue))
打印()
def路由(行,当前行=0,开始=0):
对于i,枚举中的num(rows[current_row]):#获取该行中每个数字的索引和编号
如果abs(i-start)>1:#检查它是否在1个数字半径范围内,如果不在1个数字半径范围内,则跳过这一个。如果不使用(0在Python3中,raw\u input()
(Python2版本)现在只是input()
在Python3中,raw\u input()
(Python2版本)现在只是input()
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
numOfTries = raw_input("Please enter the number of tries")
NameError: name 'raw_input' is not defined