Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Divide list into 2 subsets until fixed size – Python

I have a string consisting with bits 0/1 of 128 length I want to divide the string into 2 subsets until chunks of 16 as length
example of input

01111111001101100110111100100100010001100001111010001010010011110011001011101100111111001100100101101011111110001111010111000110

expected output

round 2 (2 set 64)
0111111100110110011011110010010001000110000111101000101001001111
0011001011101100111111001100100101101011111110001111010111000110

round 3 (4 set 32)
01111111001101100110111100100100
01000110000111101000101001001111
00110010111011001111110011001001
01101011111110001111010111000110

round 4 (8 set 16)
0111111100110110
0110111100100100
0100011000011110
1000101001001111
0011001011101100
1111110011001001
0110101111111000
1111010111000110

if there is any fast way without using

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

n = len(chunk)//2
chunks = [chunk[i:i+n] for i in range(0, len(chunk), n)]

and for each chunk I have to do a loop do the same thing

>Solution :

Here is what are you trying to do.

chunk = "01111111001101100110111100100100010001100001111010001010010011110011001011101100111111001100100101101011111110001111010111000110"
chunks = [chunk[j:j+i] for i in [64,32, 16] for j in range(0, len(chunk), i)]
for i in chunks:
    print(i)

Output

0011001011101100111111001100100101101011111110001111010111000110
01111111001101100110111100100100
01000110000111101000101001001111
00110010111011001111110011001001
01101011111110001111010111000110
0111111100110110
0110111100100100
0100011000011110
1000101001001111
0011001011101100
1111110011001001
0110101111111000
1111010111000110
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading