Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7765161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:06:06+00:00 2026-06-01T15:06:06+00:00

guys, I wrote a function to test if two inputs (a and b) have

  • 0

guys, I wrote a function to test if two inputs (a and b) have the same data structure, this way:

print same_structure([1, 0, 1], [a, b, c])
#>>> True
#print same_structure([1, [0], 1], [2, 5, 3])
>>> False
#print same_structure([1, [2, [3, [4, 5]]]], ['a', ['b', ['c', ['d', 'e']]]])
>>> True  
#print same_structure([1, [2, [3, [4, 5]]]], ['a', ['b', ['c', ['de']]]])
>>> False

This function has (in my implementation) to use recursion. I’m very beginner in recursion and I still have difficulty on thinking recursively. In addition, to avoid cheating the answer, I want to use my own (messy) code and through it learn to use the recursive call (using this code line: ‘same_structure return (a [i], b [e])’ properly). Could someone show how to properly use the recursion in code below?
Thanks in advance for any help!!!

def is_list(p):
return isinstance(p, list)

def same_structure(a,b):
    if not is_list(a) and not is_list(b):
        print '#1'
        return True
    else:
        if is_list(a) and is_list(b):
            print '#2'
            if len(a) != len(b):
                print '#3'
                return False
            if len(a) == len(b):
                print '#4'
                for e in range(len(a)):
                    print 'e = ', e, 'a[e]= ', a[e], 'b[e]=', b[e]
                    return same_structure(a[e], b[e])           
        else:
            return False
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:06:07+00:00Added an answer on June 1, 2026 at 3:06 pm

    The following works:

    def same_structure(a, b):
        if isinstance(a, list) and isinstance(b, list) and len(a) == len(b):
            return all(same_structure(A, B) for A, B in zip(a, b))
        return (not isinstance(a, list) and not isinstance(b, list))
    

    When writing recursive functions, you first need to come up with the base case, where you just return a value instead of calling any recursion. The base case here is one of the following conditions:

    • a is a list and b isn’t, or vice-versa: return False
    • a and b are both lists, but they have different lengths: return False
    • neither a or b are lists: return True

    If a and b are both lists and they have the same length, we need to now check each element of these lists recursively. zip(a, b) provides a convenient way to iterate over the elements from each list together, and if the result of same_structure() is False for any two sub-elements, we want the entire function to return False. This is why all() is used, if you are unfamiliar with all() it is equivalent (but more efficient) to the following loop:

    match = True
    for A, B in zip(a, b):
        if not same_structure(A, B):
            match = False
            break
    return match
    

    Here is how you could rewrite your function without changing too much, the logic is actually very similar to my solution, but just below the print '#4' you were returning from that loop too early:

    def same_structure(a,b):
        if not is_list(a) and not is_list(b):
            print '#1'
            return True
        else:
            if is_list(a) and is_list(b):
                print '#2'
                if len(a) != len(b):
                    print '#3'
                    return False
                if len(a) == len(b):
                    print '#4'
                    for e in range(len(a)):
                        print 'e = ', e, 'a[e]= ', a[e], 'b[e]=', b[e]
                        if not same_structure(a[e], b[e]):
                            return False
                    return True        
            else:
                return False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi guys I wrote this code and i have two errors. Invalid rank specifier:
Hey guys I have a weird error I wrote this code against setbubblepopup $(document).ready(function
Guys, I am very new to c++. I have just wrote this class: class
Hey guys i wrote a quick test. I want delete to call deleteMe which
I hope you guys can help me with this one. So, I have a
Hey I guys I wrote a function to force out a json file, but
Hey guys, I have a problem (again). This time I am trying to use
Hello guys I have a DIV that I got passed trought a js function
I wrote this function but can't get on the problem that gives me 'segmentation
hey guys, i wrote a little custom animated scroll function... function scroll(selector, animate, viewOffset)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.