Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have got it to produce adjacency list in the current format:
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)]}

But this format will not work for my longest path algorithm, It gives the following error
TypeError: object of type 'NoneType' has no len()

but it will work in the following format for some reason:
 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)']}

So how do i convert from the current format to the required format

What I have tried:

grid = [[0,1,1,1],
        [0,0,1,0],
        [0,0,1,0]]
        
lst = []
for rows, row in enumerate(grid) :
    for cols, col in enumerate(row) :
        if grid[rows][cols] in [1] :
            lst.append((rows, cols))
            
       
print (lst)
        

adjacencylist = []

def adjacentnode(nodea,nodeb):
    if nodea[0] == nodeb[0] and nodea[1] == nodeb[1] + 1:
        adjacent = True
    elif nodea[0] == nodeb[0] and nodea[1] == nodeb[1]-1:
        adjacent = True
    elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]+1:
        adjacent = True
    elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]-1:
        adjacent = True
    else:
        adjacent = False
    return adjacent

#Below is where conversion happens
carbongraph = {}
for node in range(len(lst)):
    adjacencylist.append((lst[node],[]))
    for neighbour in range(len(lst)):
        adjacentnodes = (adjacentnode(lst[node],lst[neighbour]))
        
        if adjacentnodes == True:
            adjacencylist[node][1].append(lst[neighbour])
        
for item in adjacencylist:
    carbongraph[str(item[0])] = (item[(1)])
    for adj in item[1]:
        adj = str(adj)
    
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)#error occurs here
      print (currentpath)
      chainlist.append(carbonchain)
      
longestchain = max(chainlist)
print (longestchain)

def Prfix():
  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())
Posted
Updated 29-Apr-19 11:01am
v4

You need to look at lists and list comprehensions for suggestions. See 5. Data Structures — Python 3.7.3 documentation[^].
 
Share this answer
 
Comments
Maciej Los 28-Apr-19 3:46am    
5ed!
Quote:
Convert from list to dictionary

There is no dictionary here, you need to convert a string from given format to another format.
I would use RegEx, 2 times, on to replace the external chars ("[]"), and another for each elements.
Quote:
This is all i need to complete my coursework, please help,

We are more than willing to help you, but not doing your homework for you. And the only problem you stated is that you have homework.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
 
Share this answer
 
v3
Comments
Richard MacCutchan 29-Apr-19 4:20am    
Not correct; there are various features of Python that makes this possible,
Member 14194093 29-Apr-19 13:24pm    
I have edited it, Its just a question about coding i am asking, Thank you
Patrice T 29-Apr-19 13:52pm    
Do you have some code that do the conversion? or at least try to.
Patrice T 29-Apr-19 13:53pm    
Where is the code that give error?
Member 14194093 29-Apr-19 16:07pm    
Below is mainly where the conversion happens

carbongraph = {}
for node in range(len(lst)):
adjacencylist.append((lst[node],[]))
for neighbour in range(len(lst)):
adjacentnodes = (adjacentnode(lst[node],lst[neighbour]))

if adjacentnodes == True:
adjacencylist[node][1].append(lst[neighbour])

for item in adjacencylist:
carbongraph[str(item[0])] = (item[(1)])
for adj in item[1]:
adj = str(adj)

Below is where the error occurs
carbonchain = len(currentpath)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900