Home > other >  creating a very specific function, want to create all the possible iterations of a type of binary ar
creating a very specific function, want to create all the possible iterations of a type of binary ar

Time:02-04

Today I am having problem building a very specific type of function in python, this function does not need to be optimized or efficient in any way just looking to implement a brute force if you will.

So the specifications of the function, I would like to build all of the possible iterations of a size 10 array that is supposed to represent different binary numbers. I would like to create it with a for loop in python with 2^10 iterations (1024).

To clarify I will give some examples of what the iterations would look like, all I need is the binary representation for the 0s and 1s arrays slowly incrementing in each loop, I just included the indexes and integer format to explain to the reader how to interpret it.

e.g 1st iteration 

values  - > [000000000] #binary representation
 
indexes - > [9876543210] # index format of values array

Integer format that the values in this iteration represent- >  0

How the integer format was calculated: (0 x 2^9   0 x 2^7   0 x 2^6, ... 0 x 2^1   0 x 2^0) = 0

e.g.  2nd iteration 

values  - > [000000001] #binary representation
 
indexes - > [9876543210] # index format of values array

Integer format that the values in this iteration represent- >  1

How the integer format was calculated: (0 x 2^9   0 x 2^7   0 x 2^6, ... 0 x 2^1   1 x 2^0) = 1

e.g.  3nd iteration 

values  - > [0000000010] #binary representation
 
indexes - > [9876543210] # index format of values array

Integer format that the values in this iteration represent- >  2 

How the integer format was calculated: (0 x 2^9   0 x 2^7   0 x 2^6, ... 1 x 2^1   0 x 2^0) = 2

e.g.  last iteration ?

values  - > [1111111111] #binary representation
 
indexes - > [9876543210] # index format of values array

Integer format that the values in this iteration represent- >  1023

How the integer format was calculated: (1 x 2^9   1 x 2^7   1 x 2^6, ... 1 x 2^1   1 x 2^0) = 1023

Tech stack for this is Python and numPy really. It might be quite simple but I am struggling to build it!

import numpy as np

def main(K):
initial = np.zeros(10) #size 10, all zeros array
for n in range(2**K):
   #need to somehow start with all zeros array and slowly increment
   #using some kind of binary logic so it changes the zeros to ones where it needs to

   #I want to create every kind of iteration this size 10 array can possibly make 

   #e.g. iteration 1 = [0000000000] #0, iteration 2 = [0000000001] #1 

   #iteration 3 = [0000000010] #2, iteration 4 = [0000000011] #3, last iteration = [1111111111]
    
main(K=10)

CodePudding user response:

You can user itertools.product:

>>> from itertools import product
>>> for elem in product([0, 1], repeat=3):
...     print(elem)
...
(0, 0, 0)
(0, 0, 1)
(0, 1, 0)
(0, 1, 1)
(1, 0, 0)
(1, 0, 1)
(1, 1, 0)
(1, 1, 1)

Substitute repeat=10 for your desired output.

CodePudding user response:

you can also get all the combinations between 0 and 200 (for example)

[list(bin(i)[2:]) for i in range(0,200)]
  • Related