Search code examples
c++pythoncmathfactorial

Print all of the possible combinations with numbers


So, my idea is quite simple: print all of the possible combinations given X numbers...

For example, I have two numbers, 1 and 0, so the program print:

(0,0)
(0,1)
(1,0)
(1,1)

... Any idea to do this with C, C++ or Python? (If you know how to do this with other lenguage, please help me anyway).

Thanks.


Solution

  • Use itertools.product. Use the below example and extend as per your need

    >>> [x for x in itertools.product("01",repeat=2)]
    [('0', '0'), ('0', '1'), ('1', '0'), ('1', '1')]
    >>>