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!

3
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by compscihelp

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.

Edited by Gribouillis

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
``````

Edited by pyTony

thanks, I think I finally got it!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.