Anton Shestakov <engored@ya.ru>, Tue, 15 Jan 2013 22:33:09 +0900
Rename bitarray example.
sets-to-strings-via-bitarrays/example.py
Permissions: -rwxr-xr-x
from bitarray import bitarray PARTLEN = 5 # 5 bits per character CHARS = (string.ascii_lowercase + string.digits)[:2 ** PARTLEN] # 'abcdefghijklmnopqrstuvwxyz012345' for i, char in enumerate(CHARS): PARTS[char] = bitarray(bin(i).lstrip('-0b').rjust(PARTLEN, '0'), endian='big') # 'a': bitarray('00000'), times, more = divmod(max(ids) + 1, PARTLEN) length = PARTLEN * (times + bool(more)) ba = bitarray(length, endian='big') return ''.join(ba.decode(PARTS)) result = bitarray(endian='big') result.encode(PARTS, string) return set(result.search(bitarray('1'))) print(encode_set({1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 15, 16})) # Buttons in an elevator in Shanghai print(encode_set({1, 4, 11, 16, 24, 29, 33, 35, 39, 45, 47, 51, 56, 58, 62, 64})) # Aronson's sequence print(decode_set('choices')) # {3, 7, 8, 9, 11, 12, 13, 16, 23, 27, 30, 33}