Final Exam: Question 8: same structure

Sir, you have said in the question that "if lists contain the same elements in the same structure ...", but sample outputs show that the two given input lists p and q need not contain "same elements", but same number of elements. Ex: 1st sample output (i agree with all other statements you have specified).

This is what i have interpreted from the question. correct me if i am wrong sir.

asked 02 Apr '12, 09:31

niranjan-1's gravatar image

niranjan-1
4122
accept rate: 0%


4 Answers:

Given the examples given in the homework description, I would agree with your interpretation.

[1, 2] has not the same structure as [1,2,3] or ['ab'] or [1, [2]] but has the same structure as ['a', 'b'].

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
link

answered 02 Apr '12, 09:37

Elise's gravatar image

Elise
3.7k51656

edited 02 Apr '12, 09:41

Try to create the tree for both the inputs - they should match in the shape but the leaf values may be different. So, they have same structure but the values may not be the same.

link

answered 02 Apr '12, 09:59

rakesh%20kumar's gravatar image

rakesh kumar
1.5k51444

In the case:

print same_structure([1, [2, [3, [4, 5]]]], ['a', ['b', ['c', ['d', 'exam']]]])

Have they the same structure????

link

answered 02 Apr '12, 15:05

Pedro%20Montero-4's gravatar image

Pedro Montero-4
4014

@Pedro Yes, they do.

(02 Apr '12, 15:11) fnenu-1 ♦♦ fnenu-1's gravatar image

Yes they have the same structure.

(03 Apr '12, 08:45) Nilesh K Mol... Nilesh%20K%20Molankar's gravatar image
-1

I have a deliciously clever way to do this, but as I'm not allowed to tell, I won't! Mwahahaha!

link

answered 02 Apr '12, 15:17

Charles%20Lin's gravatar image

Charles Lin
9.2k4294135

how does one evaluate [] element in the list to compare structure? For example if value of a is integer and value of b is [] structurally they are not the same. In this case how does evaluate b. I tried a == [] or b == []. For some reason it doesn't work.

(06 Apr '12, 01:36) Parikh Jagdi... Parikh%20Jagdish-3's gravatar image
Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15,218
×664
×637
×369
×19
×9

Asked: 02 Apr '12, 09:31

Seen: 544 times

Last updated: 06 Apr '12, 01:36