Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to list all combinations.
Message
De
07/05/2008 16:53:51
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivie
 
 
À
07/05/2008 16:22:11
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Versions des environnements
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
Visual FoxPro
Divers
Thread ID:
01315740
Message ID:
01315746
Vues:
13
>I'm trying to write a formula to work out all the possible combinations of say 2 numbers from a group of 30 numbers ranging from (1 - 50). This formula would need to also work with combinations of 3 or 4 or even 5 numbers as well. The group of numbers may increase from 30 or decrease. I know how to calculate the actual number of combinations I just can't figure how to write the code to list all the combinations. Any help would be greatly appreciated.
>
>An example of the group of 30 numbers... 1,4,6,9,10,11,14,15,18,19,20,21,22,24,27,29,30,31,33,34,35,38,39,41,42,43,45,47,48,50

Since the number of levels is variable, this looks like an ideal case for the use of a recursive function. The function contains the list of numbers - for example, separated by spaces. Then, it uses the word counting and extraction functions to extract a single number (or "word"). A version of the string should where the number extracted is eliminated should then be passed as a parameter to THE SAME FUNCTION. (That's what "recursion" means - just in case you don't know - a function calls itself.) You also need to pass some additional parameters, one to accumulate the string, and one to count the number of levels (how often the function has called itself); this latter must be incremented at each level. (This is how the recursion eventually ends; otherwise, you'll get something like an endless loop - the function calling itself over and over again, until VFP runs out of stack space.) When the function reaches the last level, it should accumulate the last number and output the result; perhaps write it to a text file, a DBF, or an array.

HTH,

Hilmar.
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform