vendredi 8 juillet 2016

flip the entry in a list without looping in Python

In Python, I have a list of zeros and ones (could be very long), e.g.,

L = [0,1,0,1,1,0,1,0,0,1]

I want to flip every entry in the list without use loop, e.g., make 0 to 1 and make 1 to 0.

My idea is the following: this L can be viewed as a binary representation of a real number. So I wish to make L, becomes 0101101001, then I think there is some bit operation could directly do the flip job. And eventually, I need to make the flipped term back to list, i.e., 1010010110 to [1,0,1,0,0,1,0,1,1,0]

Is there a way to implement this idea in Python (without using numpy)? Will this method significantly faster than loop? Will this method be the fastest way to achieve this goal?

It is an interview question. I couldn't answer on site. I am not allow to use library like numpy.

Aucun commentaire:

Enregistrer un commentaire