Needed help


#1

def remove_duplicates(numbers):
if numbers == []:
return numbers
numbers = numbers.sort()
result = [numbers[0]]
for i in numbers:
for j in result:
if i != j:
result.append(i)
return numbers

I think program is correct but the following error is coming
remove_duplicates([4, 5, 5, 4]) resulted in an error: ‘NoneType’ object has no attribute ‘getitem’


#2

For starters, an empty list isn’t a special case, you would simply do 0 iterations
That’s not a bug, but it’s extra code which you can remove.

You need to consider that various operations have some particular behaviour, how exactly you get the result or how you give input to them might not be the way that first pops up in your head – so what we gotta do is find out how to use each thing. This is where documentation comes in. Whenever you use just about anything, you need to know how it behaves, or look it up. Can’t guess.
Print statements are useful for finding out where your code behaves differently from what you want/expect. You can check each operation by printing the value you’re changing before and after the change. With that method you can find out where your code goes wrong. The submission test has already told you about some input where your code misbehaves, so call your function with the same input data and then get debugging!
Also it’s important to note that just because something appears to do the right thing in one particular case, that doesn’t mean it does what you want for all cases, so even when it behaves right for your test, you still gotta be able to say “this is right because …” which again is something documentation enables you to do

# add at the bottom of your code to test with the input that it fails for:
result = remove_duplicates([4, 5, 5, 4])
print 'remove_duplicates([4, 5, 5, 4]) -> {!r}'.format(result)

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.