Created
June 9, 2016 15:37
-
-
Save GlulkAlex/a1961c8d278a790f4639522ece0c1d48 to your computer and use it in GitHub Desktop.
filter sequence of odd integer elements using bitwise exclusive or
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lonely_Integer( | |
b: list# of int | |
) -> int: | |
so_Far = set() | |
answer = 0 | |
for n in b: | |
if n in so_Far: | |
so_Far.discard(n) | |
else: | |
so_Far.add(n) | |
if len(so_Far) > 0: | |
answer = so_Far.pop() | |
return answer | |
def lonelyinteger( | |
b: list# of int | |
) -> int: | |
""" | |
>>> 4 ^ 9 ^ 95 ^ 93 ^ 57 ^ 4 ^ 57 ^ 93 ^ 9 | |
95 | |
""" | |
answer = -1#None#0 | |
#assert len(list(b)) % 2 != 0# odd | |
for n in b: | |
assert 0 <= n <= 100 | |
if answer == -1: | |
answer = n | |
else: | |
answer = answer ^ n | |
return answer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment