Hello,

I try to generate output (all posibilities) for logical AND. Nb inputs is 10 and that means there are 1024 combination.

example:

n i1 i2 i3 i4 i5 i6 i7 i8 i9 i10 output
1 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 1 0
3 0 0 0 0 0 0 0 0 1 0 0
.
.
.
etc

How can I make a simple script to generate this output (for all posibilities) as is shown in the example above (of course which is not complete...)

Thank you for hint or help

What have you tried until now?

itertools.permutations could be handy.

I got an idea to make conversion from number to binary. Do you know any py statement?

thank you

Good idea if it is allowed, it would probably be simplist way. You can convert number to binary by bin function an you can remove the 0b sign from beginning and zerofill it with zfill method.

yes!

I have just trie this script:

def test():
    k=range(10)
    for x in k:
        f=bin(x) # x is type int
        print f

test()

result is :

0b0
0b1
0b10
0b11
0b100
0b101
0b110
0b111
0b1000
0b1001

I don't know how to get rid of 0b and add 0 to get lenght 10 numbers for instance
0b0 => 0000000000
0b1 => 0000000001

etc

can you help me pls?

thank you

Just as tonyjv suggested: str(bin(13))[2:].zfill(10)

yes, that's it! thank you

I got a small problem. Message error is 'int' object is not callable...
I don't understand why I can't generate output.

def test(n_input):
    bin=2
    r=bin**n_input
##    print r
##    print type(r) # r type int
    for x in range(n_input): # n_input type int
##        print x
##        print type(n_input)
        f=str(bin(x))[2:].zfill(10) # x is type int
        print f

test(10)

you are redefining the bin function to mean integer 2 and then use it as function.

I don't understand, can you give me an example pls?

thank you

You must not use bin as variable.

If I don't use bin, I don't get result I need.

def test(n_input):
    bin=2
    r=bin**n_input
    l=[]
##    print type(r)
    for x in range(r):
        print type(x)


##        print bin(x)
##        f=str(bin(x))[2:].zfill(10) # x is type int
##        print f

x is type ineger. I the documentation is writen:
bin(x)
Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.

I don't understant why int can't convert to bin.

Because of this line:

bin=2

it should be something like:

bin_n = bin(2)
r = bin_n ** n_input

Because of this line:
[it should be something like:

bin_n = bin(2)

Thanks for trying to clarify, but bin produces string representation of number. For arithmetic there is only one binary format and you do not need to worry about that.

You do not need to put every value in variable if it does not clarify functionality.

Here two alternatives to find the 1024 for input 10 (2**10):

Use power of two. As you use bin function later you can not use variable name bin. It is not making clearer the code to use variable instead of constant 2.

r=2**n_input

Move 1 left n_input bits bringing in zeroes (shift left)

r=1<<n_input

The point is, you had this line: bin=2 ,
before that line, bin was a function.
That line overrides that function so that bin is no longer a function that you can call, but an int.
The easy fix is just to rename "bin" to "bin_num" or something, anything that isn't built-in.

yes, it works , now the result is :

0000000000
0000000001
0000000010
0000000011
0000000100
0000000101
.
.
.etc

but I'd like to have output as a list : [0,0,0,0,0,0,1,0,1], is it posible to do it? I 've tried to use split but it doesn't work.
thank you for advice

list(binstr)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.