Hello, I have made a programme for finding cominations of a number from the user, it is designed to take any length and then tell the user how many combinations there are. However im not sure how to find how many combernations there are. I have googled it and there are very complicated formula's or formula's which arn't relavant. What would i need to put in my for loop? I have marked it out in the code. This is what i have:

class getNum:

    def __init__(self): # Runs when class is called
        self.word = int(input('Enter a number'))

    def workOutCombinations(self):
        emptyList = []
        self.string = str(self.word)
        self.length = len(self.string)
        for combLength in range(1, self.length + 1):

        for finalAnswer in emptyList: # Too work out combo's
            Ans = emptyList[finalAnswer] * emptyList[finalAnswer + 1]

    def printNum(self): # returns the word

getNumObject = getNum() # Makes the class object

print(getNumObject.printNum()) # prints out the word


Recommended Answers

All 8 Replies

What? Why are people on DaniWeb so bad at defining their problems?

Do you wish to make a program that takes a variable amount of integers, and upon asked, tell the user how many combinations there are to sort them?
Or do you wish to make a program that takes one digit, (eg. 3078) and find out how many combinations
[3] [0] [7] [8] can pose?

Be specific, proof-check the question you're asking, and read it through thinking: "Does this make any sense?".

You do not need loop to find number of combinations if you use math.factorial function:

If you want to do more efficient way, you can do loop upto the r value (see that both numerator and denominator have r values)

I want the user to input any number and the programme to respond with how many possible combinations there are, Example,

if the user enters - 123

Python does this in the background:

123 # 1st Combo
132 # 2nd Combo
231 # 3rd Combo
213 # 4th Combo
321 # 5th Combo
312 # 5th Combo

So in this case the answer would be 6.

That would be just a simple
1. compare the first number with the remaining (2 in this case)
2. swap the first and second number and do again

You would continue swapping if there were more digits. You have not included enough actual code to really comment on so it is difficult to move on from where your code starts. Note that the input "self.word" is not being used anywhere so whatever result you get it will not include the digits from the input. Also note that 123 and 321 are sometimes condsidered the same combination. Present a simple example in a few lines where you combine and swap and perhaps we can help more.

That is all the code, also self.word is used in self.string.

A combination with the maximum sample size is called a permutation.

You can use itertools permutations for this.
If this is a school task,follow woooee explanation.
And try to figure a way to get permutation without using build in tools like itertools.

Can simplify class to you dont need a method(printNum),class already has that info as you see a take out in obj.word.
And Capital letters is for class name only,work_out_combinations read PEP-8.

import itertools

class getNum:
    def __init__(self):
        self.word = input('Enter a number')

    def work_out_combinations(self):
        '''Return permutation for entred number'''
        return list(itertools.permutations(int(i) for i in self.word))

Test class.

>>> obj = getNum()
>>> obj.word
>>> obj.work_out_combinations()
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

>>> obj.work_out_combinations.__doc__
'Return permutation for entred number'
>>> help(obj.work_out_combinations)
Help on method workOutCombinations in module __main__:

workOutCombinations(self) method of __main__.getNum instance
    Return permutation for entred number
commented: Very helpful & made a working programme, Thanks! +1

in any forum you will have people that don't know how to ask a proper question. I share with you the destain of folks that make their problem a guessing game for the helpers, slowly revealing what they actually want.

Be a part of the DaniWeb community

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