Search This Blog

Sunday, September 12, 2010

Given list, subset of list: return bitmask (bit vector same length as list) corresponding to the subset.

Programmer Question

For example,



bitmask({1, 2, 3, 4}, {2, 4})


returns



{False, True, False, True}


The naive way to do this is to map a membership function over the list:



map(lambda(x, member(x, subset)), list)


but that's O(n*m) and it can be done in O(n+m).



What's the best way to implement bitmask() in your favorite language?



PS: It doesn't actually matter if the second list is a subset of the first. However you implement it, any elements of the second list that are not in the first will simply be ignored.



Find the answer here

No comments:

Post a Comment

Related Posts with Thumbnails