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

  • Home
  • SEARCH
  • 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 7769251
In Process

The Archive Base Latest Questions

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

Define a procedure, same_structure, that takes two inputs. It should output True if the

  • 0

Define a procedure, same_structure, that takes two inputs. It should output
True if the lists have the same structure, and False
otherwise. Two values, p and q have the same structure if:

Neither p or q is a list.

Both p and q are lists, they have the same number of elements, and each
element of p has the same structure as the corresponding element of q.

EDIT: To make the picture clear the following are the expected output

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

I thought recursion would be best to solve this problem in python I have come up with the following code but its not working.

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

 def same_structure(a,b):
    if not is_list(a) and not is_list(b):
        return True
    elif is_list(a) and is_list(b):
        if len(a) == len(b):
            same_structure(a[1:],b[1:])
    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-01T16:06:47+00:00Added an answer on June 1, 2026 at 4:06 pm

    Recursion would be a good idea, but not the way you’ve suggested it. First off (and this may be only a typo), you don’t actually return anything here:

    if len(a) == len(b):
        same_structure(a[1:],b[1:])
    

    Second, you should recursively deal with each element, not each sublist. ie.:

    if len(a) == len(b):
        for i in range(len(a)):
            if not same_structure(a[i], b[i]):
                return False
        return True
    else:
        return False
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SqlCommand which runs a stored procedure that contains two integer output
I have several stored procedures that all use the same set of parameters. Is
I have a stored procedure that works correctly when I execute the stored procedure
I want to define record with procedure or function. Can you help with syntax?
In Chapter 16 of The Seasoned Schemer, the authors define a recursive procedure depth,
I defined the Scheme procedure to return another procedure with 2 parameters : (define
So I have a stored procedure called Spr_EventLogCreate defined in my database. I have
I have a stored procedure in which i want to create a user defined
I have a reporting module in an ASP.NET webforms app (C#) that uses dynamic
I looked for the name of a procedure, which applies a tree structure of

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.