fork(1) download
  1. from functools import reduce
  2. from itertools import zip_longest
  3.  
  4. def grouper(iterable, n, fillvalue=None):
  5. "Collect data into fixed-length chunks or blocks"
  6. # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
  7. args = [iter(iterable)] * n
  8. return zip_longest(*args, fillvalue=fillvalue)
  9.  
  10.  
  11. import ast, sys
  12.  
  13. for bits in map(ast.literal_eval, sys.stdin):
  14. bytes = [reduce(lambda byte, bit: byte << 1 | bit, eight_bits)
  15. for eight_bits in grouper(bits, 8, fillvalue=0)]
  16. print("{bits} -> {bytes}".format(**vars()))
Success #stdin #stdout 0.03s 9328KB
stdin
[]
[1]
[1,1]
[1,0,0,0,0,0,0,0,1]
stdout
[] -> []
[1] -> [128]
[1, 1] -> [192]
[1, 0, 0, 0, 0, 0, 0, 0, 1] -> [128, 128]