Remove_duplicates WHY DOES THIS WORK?


#1

I've been working on the remove_duplicates exercise and took a peak at enavarro33's solution and can't figure out why this works. In particular, under what circumstances would if i != i ever be true? And why return i?

def remove_duplicates (x):
    p = []
    for i in x:
        if i != i:
            p.append(i)
    return i

i'm curious because I've been reading up on big-o and wanted to approach this looking for the shortest approximate runtime. Not sure if this is it, but I'm missing something here.

I'm new to all of this, but I'll try my best to follow your responses.

Thanks.


#2

Well first off, what do you mean by "works"?
I'd decide on what correct behaviour is and then test for that. I wouldn't have proof of correctness that way but if the tests are extensive enough I'd call it "appears to work"
I think you did something else in deciding it works.

If something does nothing, then go ahead and remove it.

Best possible solution is O(N) because all elements have to be considered and testing if something has been seen before can be done in constant time with a hashmap.

What's in there is O(N2), but isn't doing its job very well and so can't be considered a solution to the problem.


#3

Thanks, ionatan. I'm still working out a solution for myself, and I understand the hint it gave, I'm just not understanding with what's going on in the code I copied here.


#4

Do you know what each part of it does? Execute it in your head or otherwise reason about what it does. It's just written instructions, you can very much reason about what their effect is.

If some particular part eludes you then ask about it. But as for what it does as a whole, it's very short and you may as well just practice reading code on this.


#5

I have, im just not getting the "if i != i". isn't that like saying "if 1 != 1"? Any takers?


#6

It is.


#7

Correction, it is O(N), but if it was "corrected" (as in least possible change for it to do what it should) then it looks like it's trying to be O(N2)


#8

just ran this through pycharm with some print statements and i didn't do a thing. thanks for the help. im gonna chalk this up to some fluke.


#9

Just personal curiosity from a sceptic, what does pycharm do that is helpful to you?


#10

I ran this in pycharm and nothing was being appended to p and i was returned as 2. looks like it doesn't really do anything.

x = [1, 1, 2, 2]

def remove_duplicates(x):
    p = []
    for i in x:
        print i
        if i != i:
            print i
            p.append(i)
        print p
    print i
    return i

remove_duplicates(x)

#11

No I mean pycharm as opposed to just running the same code in a terminal, I guess it can remain a mystery for a while longer


#12

it's just easier for me rather than using the terminal


#13

Code you provided, is indeed not working.

As I tried on online python compiler, output was wrong.

So don't waste your time trying to understand an wrong solution :slight_smile:


#14

@okala It actually works!


#15

good code , which work !

it works because "i" catch current value until will get next value from "x" list and renew next step value in "for" code block loop. And we append "i" value only next (it is current) after previous "i" value . If current "i" not equal next "i" in "x" we must to ignore appending "i" in new list "p"? and just jump to next "i" which will compare to previous "i".

:slight_smile:


#16

def remove_duplicates(x):
    p = []
    for i in x:
        if i != i:
            print i
            p.append(i)
    return i
print remove_duplicates([1,1,1,2,3,4,4,7,10,6])

This does not work. It reports success from Codecademy, but the output is only 6, which is wrong.


#17

Yeah it doesn't work. The i's will iterate together. And thus, you're gonna end up with an empty list when return is p (the list that is supposed to contain no duplicates).
Also the function returns i, which is the item of the last index....which is 6.
It may pass on Codecademy but it's output/behaviour is not what is expected.

i!=i will always be false. That's is why print i puts out nothing.


#18

Yes it is, that is different code


#19

Original code you replied about:

def remove_duplicates (x):
    p = []
    for i in x:
        if i != i:
            p.append(i)
    return i

My code:

def remove_duplicates(x):
    p = []
    for i in x:
        if i != i:
            print i
            p.append(i)
    return i
print remove_duplicates([1,1,1,2,3,4,4,7,10,6])

They are functionally equivalent, I just added print statements to see what's going on. If I change "return i" to "return p", then I get another error altogether.


#20

I just got an empty list which proves that i != i never became True.