|
-
Jan 29th, 2014, 07:48 PM
#1
Thread Starter
Frenzied Member
how to get all possible combination (programmatically)
I'm trying to figure out how to write a code that would allow me to get all the possible combination. In this case, I have 4 fruits: apple, orange, grape, mango. I want to know all the possible combination, for example:
apple (a possible combination)
apple,orange (another possible combination)
apple,grape (another possible combination)
apple,orange,grape (another possible combination)
and so on...
-
Jan 29th, 2014, 08:07 PM
#2
Re: how to get all possible combination (programmatically)
I'm assuming order doesn't matter, that is whether (apple, orange), or (orange,apple) they are the same combination.
In which case, the way I would solve it, ironically, is to maintain a strict order, as you have by keeping apple first in your example combinations.
So, treat each fruit like a number from 1 to 4, and generate combinations, where the numbers always have to be in numerical order, and you'll have all your combinations.
1
2
3
4
12
13
14
123
124
1234
See if you can figure a way to loop 4 times when you have 1 item, 3 times when you have 2 items, 2 times when you have 3 items, and 1 time when you have 4 items.
See a pattern?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|