Is there an efficient way to find minimum xor value between a key and elements of a list?

Question:

Assume that I have a list of 10 thousand random numbers chosen in this way:

random_num = list(np.random.choice(2**16, 10000, replace=False))

Now, I have a key in the same range (e.g., 1024), and I need to find 10 numbers from the sorted list that have minimum value with the key based on the xor distance:

xor_dist = key ^ random_num[i]

Any suggestion?

I tried to sort the random_num list and then find the xor distance. Because of the xor nature, it doesn’t work for all keys.

Also, I was thinking to use banary search to find the key in the list and then check with the neighbors, but again, it doesn’t make the correct result for all keys and depends on where the key is in the range.

Asked By: Vahid Heidaripour

||

Answers:

Sorting is overkill, because it sorts all the numbers in O(n lg n) time, but you don’t care about the ordering of 9,990 of the elements.

Instead, use the heapq module to build a heap (in O(n) time) from which you can extract the 10 smallest in O(lg n) time.

import numpy as np
import heapq

random_num = np.random.choice(2**16, 10000, replace=False)
q = [(x ^ 1024, x) for x in random_num]
heapq.heapify(q)
result = [v for _, v in heapq.nsmallest(10, q)]
Answered By: chepner

sort will work with the right key parameter:

import random

random_num = random.choices(range(2**16), k = 10000)
my_key = 1024
random_num.sort(key = lambda x: x^my_key)
print(random_num[:10])

Typical output:

[1030, 1037, 1042, 1057, 1068, 1077, 1088, 1090, 1108, 1109]
Answered By: John Coleman
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.