Hi, I need to create a function using recursion to find out if two lists have the same shape. For example, if nest1 = [4, [ [3,6], [] ,7] ,[8] ]
nest2 = [ [ [3] ] , 7 , [ [5 , [9 , 2] , [ [ [ [ [4] ] , 2 ] ] ] ] ] ]
nest3 = [ 5, [ [3,9], [] ,7] ,[2]

then

isomorphic ( nest1 , nest2 ) should yield False
isomorphic ( nest1 , nest3 ) should yield True
isomorphic ( nest2 , nest3 ) should yield False

How would you set this up?

Thanks!

Recommended Answers

All 3 Replies

Compute isomorphic(nestA, nestB) by hand without a computer and note carefully every single step of your reasonning. This will give you pseudo code. Then write the pseudo code, then implement.

My implementation is cute two two branch ifs and this is the trace of running the function with trace decorator two of you examples as input.

|   isomorphic ([4, [[3, 6], [], 7], [8]], [[[3]], 7, [[5, [9, 2], [[[[[4]], 2]]]]]])
|      isomorphic (4, [[3]]) = False
|       = False
|    False


|   isomorphic ([4, [[3, 6], [], 7], [8]], [5, [[3, 9], [], 7], [2]])
|      isomorphic (4, 5) = True
|      
|      isomorphic ([[[3, 6], [], 7], [8]], [[[3, 9], [], 7], [2]])
|         isomorphic ([[3, 6], [], 7], [[3, 9], [], 7])
|            isomorphic ([3, 6], [3, 9])
|               isomorphic (3, 3) = True
|               
|               isomorphic ([6], [9])
|                  isomorphic (6, 9) = True
|                  
|                  isomorphic ([], []) = True
|                   = True
|                = True
|            
|            isomorphic ([[], 7], [[], 7]) = True
|             = True
|         
|         isomorphic ([[8]], [[2]])
|            isomorphic ([8], [2])
|               isomorphic (8, 2) = True
|               
|               isomorphic ([], []) = True
|                = True
|            
|            isomorphic ([], []) = True
|             = True
|          = True
|       = True
|    True

thanks, I think I finally got it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.