My Code measures the longest path, not allowing cycles. For the example input below, the longest chain would be 4

Please can you review my code?

`import numpy as np inputgrid = np.array([['.','.','.','.','.'], ['C','-','C','-','C'], ['.','.','|','.','.'], ['.','.','C','.','.'], ['.','.','|','.','.'], ['.','.','C','.','.'], ['.','.','.','.','.']])# This Is Input inputgrid = np.where(inputgrid=='.', 0, inputgrid) inputgrid = np.where(inputgrid=='C', 1, inputgrid) inputgrid = np.where(inputgrid=='-', 9, inputgrid) inputgrid = np.where(inputgrid=='|', 9, inputgrid) np.array(inputgrid).tolist() grid = [[int(item) for item in row] for row in inputgrid] def display(grid): for row in grid: print(row) display(grid) lst = [] for rows, row in enumerate(grid): for cols, col in enumerate(row): if grid[rows][cols] in [1]: lst.append((rows, cols)) bondlst = [] for rows, row in enumerate(grid): for cols, col in enumerate(row): if grid[rows][cols] in [9]: bondlst.append((rows, cols)) print(lst) print(bondlst) bondx = [] bondy = [] for item in bondlst: (bondx).append(item[0]) (bondy).append(item[1]) print(bondx) print(bondy) adjacencylist = [] def adjacentnode(nodea ,nodeb): if nodea[0] == nodeb[0] and nodea[1] == nodeb[1]+2: adjacent = True elif nodea[0] == nodeb[0] and nodea[1] == nodeb[1]-2: adjacent = True elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]+2: adjacent = True elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]-2: adjacent = True else: adjacent = False return adjacent print (adjacentnode((1,0),(1,2))) count = 0 tempgraph = {} for node in range(len(lst)): print (node) adjacencylist.append((lst[node] ,[])) for neighbour in range(len(lst)): adjacentnodes = (adjacentnode(lst[node] ,lst[neighbour])) print(adjacentnodes) if adjacentnodes == True: count = count +1 adjacencylist[node][1].append(lst[neighbour]) # adjacencylist.append((lst[node],[])) # adjacencylist.append(lst[node]) # adjacencylist.append(lst[neighbour]) print (count) print (adjacencylist) for item in adjacencylist: tempgraph[str(item[0])] = (item[(1)]) carbongraph = {} for i in tempgraph: carbongraph[i] = [str(k) for k in tempgraph[i]] print(carbongraph) #print(adjacencylist) #print (carbongraph) ''' carbongraph = {'(0, 1)' :['(0, 2)'], '(0, 2)' :['(0, 1)' ,'(0, 3)' ,'(1, 2)'], '(0, 3)' :['(0, 2)'], '(1, 2)' :['(0, 2)', '(2, 2)'], '(2, 2)': ['(1, 2)']} WEIGHTS = 1 ''' def shortestpath(graph, start, end, path=[]): path = path + [start] if start == end: return path if start not in graph: return None for node in graph[start]: if node not in path: newpath = shortestpath(graph, node, end, path) if newpath: return newpath return None LeafArray = [] for leaf in carbongraph: degree = (len(carbongraph[leaf])) if degree == 1: LeafArray.append(leaf) print(LeafArray) chainlist = [] for node in LeafArray: for neighbour in LeafArray: currentpath = (shortestpath(carbongraph, node, neighbour)) carbonchain = len(currentpath) print(currentpath) chainlist.append(carbonchain) longestchain = max(chainlist) print(longestchain) def Prfix(): global prefix if longestchain == 4: prefix = "But" elif longestchain == 5: prefix = "Pent" elif longestchain == 6: prefix = "Hex" elif longestchain == 7: prefix = "Hept" elif longestchain == 8: prefix = "Oct" return prefix print(Prfix()) ``` `