7.8 Help searching a TypeError

Hello!

Iam curious about Final 7.8 . When i hit run, it says everything is correct, but when submitting I get an TypeError and I cant figure out why.
Maybe someone can point it out to me?

Thanks in advance

My procedure is:

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
    if (is_list(a) and is_list(b)):
        if (len(a) != len(b)):
            return False
    counter = 0
    check = True;
    while check and counter < len(a)-1:
        if (not is_list(a[counter]) and not is_list(b[counter]) ):
            counter = counter + 1
        if (is_list(a[counter]) != is_list(b[counter])):
            check = False
        if (is_list(a[counter]) and is_list(b[counter])):
            if (len(a) != len(b)):
                check = False
            else:
                check = same_structure(a[counter],b[counter] )
                counter = counter +1
    return check

asked 14 Apr '12, 12:56

kai's gravatar image

kai
63
accept rate: 200%

retagged 14 Apr '12, 13:07

malckwan's gravatar image

malckwan
4.7k102894


2 Answers:

Your code assumes that all elements within lists will be a list. A list may contain just numbers. So in a case where, a = [ [2, 3], 4, [3] ] and b = [ [3, 5], [5, 6], [4] ], your code will attempt to evaluate the length of the second element in a (which is an integer 4 and has no length).

Your procedure needs a third conditional that checks if only one of the two inputs is a list. Something like :

if is_list(a) != is_list(b):
      return False
link
This answer is marked "community wiki".

answered 14 Apr '12, 13:03

malckwan's gravatar image

malckwan
4.7k102894

edited 14 Apr '12, 13:06

Thanks for the answer but I my Idea (eclipse, pydev) says it runs just fine. (Evaluate to false)

if __name__ == '__main__':
    d = [ [2, 3], 4, [3] ] 
    e = [ [3, 5], [5, 6], [4] ]
    print same_structure(d,e)
    print same_structure(e, d)
(14 Apr '12, 13:10) kai kai's gravatar image
1

Try this :

print same_structure(1, [2, 1, 2])

Your code throws a TypeError.

Also, as @cthompson says below, if d = [1] and e = 7, your code evaluates to True, which is clearly wrong.

Sorry, the example in my answer above was an example I just made up but didn't test. If you compare a number and a list, at the top level, you'll get the TypeError.

(14 Apr '12, 13:14) malckwan malckwan's gravatar image
1

You have made me realise that I shouldn't be lazy and I should write my own test cases. I just used ones I found on the forums and clearly they're not exhaustive enough. Thanks!

(14 Apr '12, 13:18) Chris Thompson Chris%20Thompson's gravatar image

Stupid me. Thanks for the help!

(14 Apr '12, 13:31) kai kai's gravatar image

I tested your code on several test cases and none of them produced an error but one returned the wrong value.

print same_structure([1], 7) returns True

For reference, these were the test cases I ran:

print same_structure([ [2, 3], 4, [3] ] ,[ [3, 5], [5, 6], [4] ]) == False
print same_structure(3, 7) == True
print same_structure([1, 0, 1], [2, 1, 2]) == 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
print same_structure([],[]) == True
print same_structure('','') == True
print same_structure([],['']) == False
print same_structure('',3) == True
print same_structure(3,'audacity') == True
print same_structure([1, [], 1], [2, [5], 3]) == False
print same_structure([1,2,3,[1,[1],2], [[1,2]]], [1,2,3,[1,[],2], [[1,2]]]) == False
print same_structure([1,2,3,[1,1,2], [[1,2]]], [1,2,3,[1,[],2], [[1,2]]]) == False
print same_structure([1], 7) == False
print same_structure([[1,2,3,['d']],4,[1,['g','g',[]]]], [['h',1,'f',[8]],'y',['k',[4,7,8]]]) == False
print same_structure ([1,[1]],[1,[1,2,3]]) == False
link

answered 14 Apr '12, 13:11

Chris%20Thompson's gravatar image

Chris Thompson
2.3k31238

Ok, I should have made my own test cases. Thanks for the help!

(14 Apr '12, 13:30) kai kai'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,320
×683
×172
×112
×35
×19

Asked: 14 Apr '12, 12:56

Seen: 151 times

Last updated: 14 Apr '12, 13:31