#๐Ÿ”’ unzip bin int

19 messages ยท Page 1 of 1 (latest)

chilly socket
drowsy geyserBOT
#

@chilly socket

Python help channel opened

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.

chilly socket
#

@languid jacinth have an idea how to reverse it ._.?

#

I use the exact code you show

languid jacinth
#

you can just reverse the bitset list

#

or write your own object that works like a reverse enumerate

chilly socket
#

wha

burnt blade
#

you need to know how many bitsets there were

languid jacinth
#

like the first byte is how many bits are interleaved

languid jacinth
burnt blade
#

!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

drowsy geyserBOT
burnt blade
#

dont care + developed faster + ratio + python slow
-# /hj

drowsy geyserBOT
#
Python help channel closed for inactivity

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.