def max_value(): maxvalue=0 maxlist=[] for i in range(len(gg(grid_number))): for j in range(len(gg(grid_number)[0])): if int(gg(grid_number)[i][j])>maxvalue: maxvalue=gg(grid_number)[i][j] maxlist.append('('+str(i)+', '+str(j)+')') return (maxlist[-1],maxvalue)
def get_nbrs(x): nbrslist=[] if x[0]>0 and x[1]>-1: nbrslist.append((x[0]-1,x[1])) if x[0]>-1 and x[1]>0: nbrslist.append((x[0],x[1]-1)) if x[0]>-1 and x[1]<len(gg(grid_number)[0])-2: nbrslist.append((x[0],x[1]+1)) if x[0]<len(gg(grid_number))-2 and x[1]>-1: nbrslist.append((x[0]+1,x[1])) return nbrslist
def min_nbrs(x): min1value= 1000000000 endpoint = 0 min1="" i=-1 while i <len(get_nbrs(x)): if gg(grid_number)[get_nbrs(x)[i][0]] [get_nbrs(x)[i][1]] < min1value and 0 < gg(grid_number)[get_nbrs(x)[i][0]] [get_nbrs(x)[i][1]] - gg(grid_number)[x[0]] [x[1]] <= high: min1value = gg(grid_number)[get_nbrs(x)[i][0]] [get_nbrs(x)[i][1]] min1= (get_nbrs(x)[i][0] , get_nbrs(x)[i][1]) if gg(grid_number)[get_nbrs(x)[i][0]] [get_nbrs(x)[i][1]] < min1value and gg(grid_number)[get_nbrs(x)[i][0]] [get_nbrs(x)[i][1]] - gg(grid_number)[x[0]] [x[1]] > high: endpoint = -1 i+=1 if min1=="" and min1value == 1000000000 and endpoint ==-1: return (-1,-1) if min1=="" and min1value == 1000000000 and endpoint !=-1: return (-1,0) return (min1,min1value)
def max_nbrs(x): max1="" max1value= 0 end = 0 i=-1 while i<len(get_nbrs(x)): if gg(grid_number)[get_nbrs(x)[i][0]] [get_nbrs(x)[i][1]] > max1value and 0 <gg(grid_number)[get_nbrs(x)[i][0]] [get_nbrs(x)[i][1]] - gg(grid_number)[x[0]] [x[1]] <= high: max1value = gg(grid_number)[get_nbrs(x)[i][0]] [get_nbrs(x)[i][1]] max1= (get_nbrs(x)[i][0] , get_nbrs(x)[i][1]) if gg(grid_number)[get_nbrs(x)[i][0]] [get_nbrs(x)[i][1]] > max1value and gg(grid_number)[get_nbrs(x)[i][0]] [get_nbrs(x)[i][1]] - gg(grid_number)[x[0]] [x[1]] > high: end = -1 i+=1 if max1=="" and max1value == 0 and end ==-1: return (-1,-1) if max1=="" and max1value == 0 and end!=-1: return (-1,0) return (max1,max1value)
#main program if __name__== '__main__': total=ng grid_number=int(input('Enter a grid index less than or equal to 3 (0 to end): '))#ask user to input the value print(grid_number) grid=str(input('Should the grid be printed (Y or N): '))#ask user to input the value print(grid) grid=grid.lower()#low case can match the condition we give gridbox='' i=-1 j=0#initial if grid=='y': while i< len(gg(grid_number))-1:#use while function to get the string of grid and print it out at the end gridbox+='\n' i+=1 j=0 while j<len(gg(grid_number)[0]) and i< len(gg(grid_number)): gridbox += " "*(4-len(str(gg(grid_number)[i][j]))) gridbox += str(gg(grid_number)[i][j]) j+=1 print("Grid {}{}".format(grid_number,gridbox)) print("Grid has {} rows and {} columns".format(len(gg(grid_number)),len(gg(grid_number)[0])))#print out the value we need #use for loop to get all the neighbor value and print it out startpoint=gl(grid_number)
return (maxlist[-1],maxvalue) if __name__== '__main__': total=ng grid_number=int(input('Enter a grid index less than or equal to 3 (0 to end): '))#ask user to input the value print(grid_number) high = int (input( "Enter the maximum step height: "))#ask user to input the value print(high) grid=str(input('Should the path grid be printed (Y or N): '))#ask user to input the value print(grid) grid=grid.lower()#low case can match the condition we give print("Grid has {} rows and {} columns".format(len(gg(grid_number)),len(gg(grid_number)[0])))#print out the value we need max_value() print("global max: {} {}".format(max_value()[0],max_value()[1]))#print out the value base on input the value startpoint = gl(grid_number)#ask user to input the value, and use this value to find the start point total =[] k=0 ''' In the start point we need to calculation the steep path and the gradual path which we nedd to print out the value of location for each start point ''' while k<len(startpoint): print("===") print("steepest path") steepest=str(startpoint[k])+ " " location = startpoint[k] boxlenth1= 0