Bit Vectors/Bitmaps
A bitmap is one way of efficiently representing sets of unique integers using single bits.
To see how this works, we can represent a set of unique integers between `0` and `< 20` using a vector/array of 20 bits:
```
var set = [3, 14, 2, 11, 16, 4, 6];```
```
var bitmap = [0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0];
```
As you can see, with a bitmap, the length of the vector represents the range of unique values in the set (in this case `0-20`), and the `0/1` represents whether or not the current index is equal to a value in the set.

Task:
Your task is to write a function `toBits` that will take in a set of uniqe integers and output a bit vector/bitmap (an array in javascript) using `1`s and `0`s to represent present and non-present values.
Input:
The function will be passed a set of unique integers in string form, in a random order, separated by line breaks.
Each integer can be expected to have a unique value `>= 0` and `< 5000`.
The input will look like this:
`let exampleInput = '3\n14\n5\n19\n18\n1\n8\n11\n2...'`
Output:
The function will be expected to output a 5000 bit vector (array) of bits, either `0` or `1`, for example:
`let exampleOutput = [0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0,...]`
More in-depth bitmap kata coming very soon, happy coding!
To learn more about bitmapping and to see the inspiration for making these kata, checkout the book Programming Pearls by Jon Bently.  It's a powerful resource for any programmer!

Source: codewars | Difficulty: EASY
Tags: Bit manipulation

Data structures

Complete search

Starter code: def to_bits(string):
	

Solutions:
def to_bits(s):
	lst = [0] * 5000
	for i in map(int, s.split()):
		lst[i] = 1
	return lst


def to_bits(s):
	return [1 if i in set(map(int, s.split('\n'))) else 0 for i in range(5000)]


def to_bits(string):
	bitmap = [0] * 5000
	for x in map(int, string.split()):
		bitmap[x] = 1
	return bitmap


to_bits = lambda s: list(map(set(map(int, s.split('\n'))).__contains__, range(5000)))


def to_bits(string):
	input = sorted([int(x) for x in string.split('\n')])
	bitmap = [0] * 5000
	for i in input:
		bitmap[i] = 1
	return bitmap


import numpy as np

def to_bits(string):
	arr = [int(s) for s in string.split('\n')]
	bm = np.zeros(5000)
	for i in arr:
		bm[i] += 1 * (1 - bm[i])
	return list(bm)


def to_bits(string):
	numbers = set((int(s) for s in string.split()))
	return [(0, 1)[n in numbers] for n in range(5000)]


def to_bits(string):
	ret = [0] * 5000
	for i in string.split('\n'):
		ret[int(i)] = 1
	return ret


def to_bits(string):
	a = list(string)
	words = [w.replace('\n', ' ') for w in a]
	b = ''.join(words)
	lst = b.split(' ')
	lst2 = []
	for x in lst:
		if x.isdigit():
			lst2.append(x)
	lst3 = [int(i) for i in lst2]
	lst3.sort()
	max(lst3)
	print(len(lst3))
	print(lst3)
	a = []
	for x in range(5000):
		a.append(0)
	len(a)
	for i in range(len(lst3)):
		a[lst3[i]] = 1
	print(sum(a))
	return a