#๐ unzip bin int
19 messages ยท Page 1 of 1 (latest)
@chilly socket
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
@languid jacinth have an idea how to reverse it ._.?
I use the exact code you show
depends on how you want to do it
you can just reverse the bitset list
or write your own object that works like a reverse enumerate
wha
you need to know how many bitsets there were
oh yeah that too. you could add some metadata at the front
like the first byte is how many bits are interleaved
oh wait you can also just reverse a range and index that way
!e
def pack(bitsets: list[int]) -> int:
width = max(bitsets).bit_length() or 1
binaries = [f"{bitset:0{width}b}" for bitset in bitsets]
return int(''.join(binary[idx] for idx in range(width) for binary in binaries), 2)
from math import ceil
def unpack(count: int, packed: int) -> list[int]:
binary = bin(packed)[2:]
width = ceil(len(binary) / count)
binary = binary.zfill(width * count)
return [int(''.join(
binary[(bit_idx * count) + bitset_idx]
for bit_idx in range(width)
), 2) for bitset_idx in range(count)]
from random import randint
for count in range(2, 10):
for _ in range(100):
bitsets = [randint(0, 2**randint(0, 16)) for _ in range(count)]
packed = pack(bitsets)
unpacked = unpack(count, packed)
assert unpacked == bitsets
binary math fans vs string enjoyers
:warning: Your 3.12 eval job has completed with return code 0.
[No output]
strings are slower 
dont care + developed faster + ratio + python slow
-# /hj
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
