Mayank Bhatt
Mayank Bhatt

Reputation: 17

How to find all the Combinations of an integer number?

Let's say we have given the number n = 212. We need to make all the possible 1 digit, 2 digits, 3 digits combination to the length of a number.

In this example, the length is 3.
So combinations will be [2, 1, 2, 21, 22, 12, 212].
Tell me how to do it?
Is there any method or function in python that can help me ease the problem?
Another Example:
n = 2345
combinations = [2, 3, 4, 5, 23, 24, 25, 34, 35, 45, 234, 235, 345, 2345]

Upvotes: 0

Views: 814

Answers (1)

Sam Varghese
Sam Varghese

Reputation: 468

Maybe this can help you

from itertools import combinations

number = list(input("Kindly enter the number: "))

counter = 1

answer = []

while counter < len(number)+1:
    comb = combinations(number, counter)
    for i in list(comb):
        num = "".join(i)

        answer.append(int(num))

    counter += 1

print(list(set(answer)))


Upvotes: 2

Related Questions